Here is my code. The idea is that the grades go from 0-5 and it cannot go higher, thus the if command.
Highlighted in the code is the part that apparently is not allowed anymore (grade > 5)- I get a syntax error that says: “This construct is not allowed under -new-syntax.”
I have just started learning scala and have tried googling the answer but havent found anything
def overallGrade(project: Int, eBonus: Int, part: Int) =
var grade = project + eBonus + part
if (*grade > 5*) {
grade = 5
}
grade
end overallGrade
What should I do to fix it?
No answers on google that I could find
1
You don’t need the parentheses in your if statement:
object GradeCalculator:
def overallGrade(project: Int, eBonus: Int, part: Int): Int =
var grade = project + eBonus + part
if grade > 5 then
grade = 5
grade
def main(args: Array[String]): Unit =
val projectScore = 4
val eBonusScore = 1
val participationScore = 2
val totalGrade = overallGrade(projectScore, eBonusScore, participationScore)
println(s"Final Grade: $totalGrade")
end GradeCalculator
Output:
Final Grade: 5
Try on scastie.scala-lang.org
2