首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java "while“-循环中的变量

Java "while“-循环中的变量
EN

Stack Overflow用户
提问于 2014-07-30 08:25:02
回答 7查看 2.3K关注 0票数 0

如果这个问题被问了(可能很多次),我很抱歉,但我找不到答案。我正在学习Java,我做了一个简单的代码。它要求两个数字,比较和使用如果给出一些结果。很好,但我试着把它循环起来。gram在循环中,而我给出的第一个数字值为10。这不会像这样工作,因为"First不能被解析为变量“,因为它在循环中。好的,我明白了,但是有什么方法可以用循环中的变量来工作吗?

代码语言:javascript
复制
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");
    }
}
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-07-30 08:41:05

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

代码语言:javascript
复制
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.");
}
票数 2
EN

Stack Overflow用户

发布于 2014-07-30 08:27:27

但是,有什么方法可以让这个工作与变量在循环中吗?

不是的。你必须在循环之外声明它。

您还可以声明一个布尔变量,并使用它的First整数。类似于:

代码语言:javascript
复制
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");
}

但是正如您所看到的,您需要在循环之外声明它。

票数 2
EN

Stack Overflow用户

发布于 2014-07-30 08:29:25

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

不,不能在循环中声明变量并使用它作为条件。当您使用变量时,您需要知道定义它的作用域。如果在循环中定义了它,那么它只能在循环中使用。因此,您唯一能做的就是在循环之外定义变量,然后在循环中使用它。

代码语言:javascript
复制
int first = 0;

do {
    ...
    first = Integer.parseInt(FirstNum); // changing String to int
    ...
} while (first == 10);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25032000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档