我的问题出现在if语句中-如果变量"hours"大于10,我希望将额外的小时数乘以2.00并将9.95添加到结果中,但我在编译时收到错误,我不明白为什么。非常感谢任何帮助。
String choice;
double hours;
Scanner input = new Scanner(System.in);
System.out.print("Enter which package you are using A, B or C? ");
choice = input.nextLine();
choice = choice.toUpperCase();
System.out.print("Enter the amount of hours used: ");
hours = input.nextDouble();
switch ( choice )
{
case "A":
if ( hours > 10 ){
(( hours - 10) * 2 ) + 9.95; << ERROR: Not a statement!
}
else
System.out.println("Total: $9.95");
break;
为参考起见,这已被回答并编辑为:
case "A":
if ( hours > 10 ){
total = (( hours - 10) * 2 ) + 9.95; // Initialised to total
System.out.println("Total: $" + total);
}
else
System.out.println("Total: $9.95");
break;
您需要分配从该行生成的结果值,如:
double value = (( hours - 10) * 2 ) + 9.95;
阅读Java中有效语句
您没有将此表达式值(( hours - 10) * 2 ) + 9.95;
设置为任何变量。将值设置为任意变量,如下所示:
double total = (( hours - 10) * 2 ) + 9.95;
这一行不是Java语句,根据Oracle文档,语句应该是一个完整的执行单元
语句
语句大致相当于自然语言中的句子。一个语句构成一个完整的执行单元。以下类型的可以通过终止表达式来将表达式变成语句用分号(;)表示
<
- 赋值表达式/gh>
- 任何使用++或——
- 方法调用
- 对象创建表达式
您必须像这样将结果分配给hours变量:
hours = (your expression here);