简体   繁体   English

不是声明错误,非法启动类型

[英]not a statement error, illegal start of type

import java.util.*;

public class ulang {

    public static void main(final String[] args) {
        int a;
        int b;
        int sum;

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter num 1: ");
        a = in.nextLine();
        System.out.println("Enter num 2: ");
        b = in.nextLine();
        {
            sum = a + b;
        }

        for (i = 0; i < 5; i++) {
            (sum >= 10)
                System.out.println("Congratulations");
            else
                System.out.println("Sum of the number is Less than 10");
        }

    }
}

I'm weak on looping especially in Java. 我在循环方面很弱,特别是在Java中。 So I need some corrections on my coding, but I have no idea how to fix it. 所以我需要对我的编码进行一些修改,但我不知道如何解决它。

The coding should run like this: User need to insert 2 numbers and the program will calculate the sum of both number. 编码应该像这样运行:用户需要插入2个数字,程序将计算两个数字的总和。 After that, the program will determine if the total of sum is >=10 or <10. 之后,程序将确定总和是> = 10还是<10。 If the sum >=10, "Congratulations" will appear but if it is <10, then "The sum of number less than 10" will appear. 如果总和> = 10,则会出现“祝贺”,但如果<10,则会出现“小于10的数字之和”。 How to fix it? 怎么解决?

This is the immediate problem: 这是当前的问题:

(sum>=10)

I believe you meant that to be an if statement: 我相信你的意思是if声明:

if (sum>=10)

Additionally: 另外:

  • You're trying to use an in variable, but the Scanner variable is called scan 您正在尝试使用in变量,但Scanner变量称为scan
  • Scanner.nextLine() returns a String - I suspect you wanted Scanner.nextInt() Scanner.nextLine()返回一个String - 我怀疑你想要Scanner.nextInt()
  • Your for loop uses a variable that hasn't been declared. 你的for循环使用一个尚未声明的变量。 You probably meant: 你可能意味着:

     for (int i = 0; i < 5; i++) 

A few other suggestions though: 其他一些建议:

  • The sum isn't going to change between the loop iterations... why are you looping at all? 循环迭代之间的总和不会改变...为什么你要循环?
  • You've got a new block in which you're calculating the sum, but for no obvious reason. 你有一个新的块,你正在计算总和,但没有明显的原因。 Why? 为什么?
  • It's generally a good idea to declare variables at the point of initialization, eg 在初始化时声明变量通常是个好主意,例如

     Scanner scan = new Scanner(System.in); System.out.println("Enter num 1: "); int a = scan.nextInt(); System.out.println("Enter num 2: "); int b = scan.nextInt(); int sum = a + b; 
  • Given that you want to take the same basic action (writing a message to the screen) whether or not the user was successful, you might consider using the conditional operator like this: 鉴于您希望采用相同的基本操作(向屏幕写入消息),无论用户是否成功,您可以考虑使用条件运算符,如下所示:

     String message = sum >= 10 ? "Congratulations" : "Sum of the number is Less than 10"; System.out.println(message); 

    That would then allow you to refactor the loop to only evaluate the condition once: 然后,这将允许您重构循环以仅评估条件一次:

     String message = sum >= 10 ? "Congratulations" : "Sum of the number is Less than 10"; for (int i = 0; i < 5; i++) { System.out.println(message); } 

(sum>=10)

This line needs an if at the beginning, or it won't be read as a branch. 这行在开头需要if,否则不会被读作分支。

if (sum >= 10)

You also should name your main-class Ulang, because java class identifiers should start with an upper case letter, for readability. 您还应该为主类Ulang命名,因为java类标识符应以大写字母开头,以提高可读性。

The loop should look like the following: 循环应如下所示:

for (int i = 0; i < 5; i++) {

The first part defines the counter and assigns zero to it. 第一部分定义计数器并为其分配零。 The second is your condition and the last counts for you. 第二个是你的状况,最后一个是你的。

for (int i = 0; i < 5; i++) {
    if (sum >= 10)
        System.out.println("Congratulations");
    else
        System.out.println("Sum of the number is Less than 10");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM