有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

循环中的Java“while”变量

抱歉,如果有人问这个问题(可能很多次),但我找不到答案。我正在学习Java,并编写了一个简单的代码。它需要两个数字,比较并使用If给出一些结果。它运行得很好,但我试着循环它。gram在循环中,而我给出的第一个数值是10。这不会像这样工作,因为“First无法解析为变量”,因为它位于循环中。好吧,我明白了,但有没有办法让这个循环中的变量起作用呢

import java.util.Scanner; 

public class Loop {

    public static void main(String[] args) {
        do {    
            System.out.println("give me 2 numbers: ");
            String FirstNum, SecNum;
            Scanner FirstRead = new Scanner(System.in);
            Scanner SecRead = new Scanner(System.in);
            FirstNum = FirstRead.nextLine();
            SecNum = SecRead.nextLine();
            int First = Integer.parseInt(FirstNum); // changing String to int
            int Second = Integer.parseInt(SecNum); // changing String to int

            // Playing with loop

            if (First == Second) {
                System.out.println("First " + First + " is same as "+ Second);
            }
            else {
                System.out.println("Number " + First + " is different then " + Second);
            }   
        }
        while(First == 10); 
        System.out.print("We're done here, because first number is 10");
    }
}

共 (3) 个答案

  1. # 1 楼答案

    首先,请使用lowerCamelCase来命名变量。第一个字母总是小写的

    不,不能在循环中声明变量并将其用作条件。使用变量时,需要知道定义变量的范围。如果在循环内定义它,它将仅在循环内可用。因此,你唯一能做的就是在循环外定义变量,然后在循环内使用它

    int first = 0;
    
    do {
        ...
        first = Integer.parseInt(FirstNum); // changing String to int
        ...
    } while (first == 10);
    
  2. # 2 楼答案

    你应该首先声明你的扫描仪在循环之外

    import java.util.Scanner; 
    
    public class Loop {
        public static void main(String[] args) {
            int first = 0;
            final Scanner scanner = new Scanner(System.in);
            do {    
                System.out.println("Give me 2 numbers: ");
                final String firstNum = scanner.nextLine();
                final String secNum = scanner.nextLine();
                first = Integer.parseInt(firstNum); // changing String to int
                final int second = Integer.parseInt(secNum); // changing String to int
    
                if (first == second) {
                    System.out.println("First " + first + " is same as "+ second);
                } else {
                    System.out.println("Number " + first + " is different then " + second);
                }   
            // Playing with loop
            } while(first != 10); 
    
            System.out.print("We're done here, because first number is 10");
    
            // Free resources (should be done in a try/finally clause)
            scanner.close();
        }
    }
    

    你的while条款错了。如果在first == 10时循环,这意味着如果“first”不同于10,程序将退出

  3. # 3 楼答案

    正如其他人所说,您必须在循环范围之外声明条件变量。您还可以对您的程序应用其他一些改进:

    public static void main(final String[] args) {
        // You need only one scanner for both numbers. I t can be reused, so
        // declare it outside the loop.
        final Scanner scanner = new Scanner(System.in);
        // Also the number variables can be reused.
        int first, second;
    
        do {
            System.out.println("Give me two numbers:");
    
            // Scan the integers directly without parsing the lines manually.
            first = scanner.nextInt();
            second = scanner.nextInt();
    
            if (first == second) {
                System.out.println("The first number (" + first + ") is the same as the second (" + second + ").");
            } else {
                System.out.println("The first number (" + first + ") is different from the second (" + second + ").");
            }
        } while (10 != first);
    
        scanner.close();
    
        System.out.println("We are done here, because the first number is 10.");
    }