开发者

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. So I need some corrections on my coding, but I have no idea how to fix it.

开发者_StackOverflow社区

The coding should run like this: User need to insert 2 numbers and the program will calculate the sum of both number. After that, the program will determine if the total of sum is >=10 or <10. If the sum >=10, "Congratulations" will appear but if it is <10, then "The sum of number less than 10" will appear. How to fix it?


This is the immediate problem:

(sum>=10)

I believe you meant that to be an if statement:

if (sum>=10)

Additionally:

  • You're trying to use an in variable, but the Scanner variable is called scan
  • Scanner.nextLine() returns a String - I suspect you wanted Scanner.nextInt()
  • Your for loop uses a variable that hasn't been declared. 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, e.g.

    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 (sum >= 10)

You also should name your main-class Ulang, because java class identifiers should start with an upper case letter, for readability.

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");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜