我有为类(class)创建的 java 代码。我尝试过执行break语句,但仍然失败。
有人能指出我正确的方向吗?
import java.util.Scanner;
public class gradeAverage {
public static void main( String[] args ){
Scanner input = new Scanner(System.in);
int input1;
int input2;
int input3;
//Calculate
int studentAvg;
//Prompt for first user input or break if input = q
System.out.print( "Enter first student grade or q to quit: ");
if {
(input1=input.nextInt() = q)
break;
else
input1=input.nextInt() =;
}
//Prompt for second user input or break if input = q
System.out.print( "Enter second student grade or q to quit: ");
input2=input.nextInt();
//Prompt for third user input or break if input = q
System.out.print( "Enter third student grade or q to quit: ");
input3=input.nextInt();
studentAvg=(input1+input2+input3)/3;
System.out.printf( "The Student's grade average is %d\n" , studentAvg);
}
}
最佳答案
break 语句需要位于循环或某种类型的内部(for
、while
或 do
),您无法中断一个 if
条件。
如果您将代码放入其中:
while(true) {
...
}
那么你的休息就会很好。
另外 if 语句的格式为:
if (condition) {
...
} else {
}
您的代码中似乎有一个 if { (condition)
...
哦,=
是给变量赋值,==
是相等检查,你把它们搞混了。
关于java - 尝试设置break语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15468659/