簡體   English   中英

Java“ while”-循環內的變量

[英]Java “while” - variable inside the loop

抱歉,這個問題是否被問了(可能多次),但我找不到答案。 我正在學習Java,並編寫了一個簡單的代碼。 它要求提供2個數字,將其進行比較,然后使用If給出一些結果。 它工作正常,但我嘗試循環播放。 gram在循環中,而我給出的第一個數字值為10。這不會像這樣工作,因為“ First無法解析為變量”,因為它在循環內。 好的,我明白了,但是有什么辦法可以使變量在loop里面工作呢?

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

但是有什么辦法可以使變量在循環內工作呢?

不。您必須在循環外聲明它。

您還可以聲明一個布爾變量,並將其與First 就像是:

public static void main(String[] args) {
    boolean check = false;
    do {    
        [...]
        check = First == 10;
    }

    while(check); 

    System.out.print("We're done here, because first number is 10");
}

但是正如您所看到的,您需要在循環外聲明它。

正如其他人所說,您必須在循環作用域之外聲明condirional變量。 您還可以對程序進行其他一些改進:

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

首先,請使用LowerCamelCase命名變量。 第一個字母總是小寫。

不,您不能在循環內聲明變量並將其用作條件。 使用變量時,需要知道定義變量的范圍。 如果在循環內定義它,則僅在循環內可用。 因此,您唯一可以做的就是在循環外定義變量,然后在循環內使用它。

int first = 0;

do {
    ...
    first = Integer.parseInt(FirstNum); // changing String to int
    ...
} while (first == 10);

不,您不必在循環外聲明變量

你可以這樣做

public class Loop {
public static void main(String[] args) {
    int first = 0; //declare first outside of the loop
    do {    
        //...
        first = Integer.parseInt(FirstNum); // parse int to string
        //....
    }
    while(first == 10); 

    System.out.print("We're done here, because first number is 10");
}
}

您應該先聲明,然后在循環之外聲明掃描儀。

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”不同於first == 10 ,則程序將退出。

注意:在ANY循環內聲明變量(for while while do-while)是非常糟糕的編程習慣,應避免使用。

但是對於您的問題: but is there any way I can make this work with variable inside loop?

如果適合您,可以使用以下break語句。

public static void main(String[] args) {
 do {    
      int First = Integer.parseInt(FirstNum); // changing String to int

      if (First == 10)
      {  break; }

   } while (true)
   System.out.print("We're done here, because first number is 10");

最好使用以下while循環:

   while (true)
   {    
      int First = Integer.parseInt(FirstNum); // changing String to int

      if (First == 10)
      {  break; }

   } 
   System.out.print("We're done here, because first number is 10");

參考: 中斷DO While Java嗎?

解決方法可以做到,不建議這樣做。 正如您所發布的, 但是有什么辦法可以使變量在loop內部工作呢?

import java.util.Scanner;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("Enter first no. ");
            int first = sc.nextInt();
            System.out.println("Enter second no. ");
            int second = sc.nextInt();
            if(first == second) {
                System.out.printf("First and second both are equal i.e: %d\n", first);
            } else {
                System.out.printf("First: %d and second: %d both are different\n", first, second);
            }
            if(10 == first) {
                break;
            }
        } while(true);
        System.out.println("Done, since value of first is 10");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM