java - 尝试设置break语句

标签 java

我有为类(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 语句需要位于循环或某种类型的内部(forwhiledo),您无法中断一个 if 条件。

如果您将代码放入其中:

while(true) {
  ...
}

那么你的休息就会很好。

另外 if 语句的格式为:

if (condition) {
  ...
} else {

}

您的代码中似乎有一个 if { (condition)...

哦,= 是给变量赋值,== 是相等检查,你把它们搞混了。

关于java - 尝试设置break语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15468659/

相关文章:

java - UI图布局

java - 非公共(public)静态嵌套类?

java - Activiti - 如何获取自定义表单类型来保存多个值?

java - 从 Java 到 C# - 泛型转换为 BaseClass

java - RequestDispatcher.forward 到媒体文件?

java - cup 解析器允许我在语法 LALR 中进行空转换吗?

java - 错误的 ApplicationContext 中 session 范围的 bean 泄漏

java - 套接字编程Java : How to receive multiple files from outputstream?

java - 为什么 PackageInfo.signatures 字段是一个数组,什么时候这里只有一个值?

java - 如何不断要求用户选择有效选项?