Swift, variable type assignment
-
import Foundation let carSpeed = readLine() let townSpeed = 50 let fineFor1to10 = 15 let fineFor11to15 = 25 let fineFor16to20 = 35 let fineFor21to25 = 80 let fineFor26to30 = 100 let fineFor31to40 = 160 let fineFor41to50 = 200 let fineFor51to60 = 280 let fineFor61to70 = 480 let fineFor71andMore = 680 let overSpeed = carSpeed - townSpeed if overSpeed < 1 { print("Скорость не превышена или превышена не значительно"); } else if overSpeed >= 1 && overSpeed < 10 { print("Штраф: " + String(fineFor1to10)) } else if overSpeed >= 11 && overSpeed < 15 { print("Штраф: " + String(fineFor11to15)) } else if overSpeed >= 16 && overSpeed < 20 { print("Штраф: " + String(fineFor16to20)) } else if overSpeed >= 21 && overSpeed < 25 { print("Штраф: " + String(fineFor21to25)) } else if overSpeed >= 26 && overSpeed < 30 { print("Штраф: " + String(fineFor26to30)) } else if overSpeed >= 31 && overSpeed < 40 { print("Штраф: " + String(fineFor31to40)) } else if overSpeed >= 41 && overSpeed < 50 { print("Штраф: " + String(fineFor41to50)) } else if overSpeed >= 51 && overSpeed < 60 { print("Штраф: " + String(fineFor51to60)) } else if overSpeed >= 61 && overSpeed < 70 { print("Штраф: " + String(fineFor61to70)) } else { print("Штраф: " + String(fineFor71andMore)) }
Makes a mistake:
main.swift:3:1: error: use of unresolved identifier 'carSpeed'
carSpeed = readLine()
^~~~~~~~
main.swift:18:17: error: use of unresolved identifier 'carSpeed'
let overSpeed = carSpeed - townSpeed
^~~~~~~~
exit status 1
There were attempts to change the variable type, but it didn't work anyway.
-
I've got another mistake, so I'm just gonna get some of the code here so you can work. If you don't understand anything, ask.
Instead of yours:
let carSpeed = readLine() let townSpeed = 50
Write it up (though I added an error check if the letters were inserted, not the numbers. If it's complicated or not necessary, I'll write how to just make an entry without this check.
var carSpeed = 0
print("Введите скорость:")
if let speed = Int(readLine()!) {
carSpeed = speed
} else {
print("Вы ввели не число")
}let townSpeed = 50
The idea is that we have to make sure there's no further speed check if there's a mistake of entry, but I didn't do it because I don't know if you need it or if you want it.
And in lieu of a bunch of "if else if," I suggest you use a switch to overtake conditions, but you can still use the semi-enclosed number range (from that to that, including one). Here's an example from another code:
switch myValue {
case 0:
print("no")
case 1..<5:
print("a few")
case 5..<12:
print("several")
case 12..<100:
print("dozens of")
case 100..<1000:
print("hundreds of")
default:
print("many")
}