很抱歉,如果问过这个问题(可能很多次),但我找不到答案。我正在学习Java,我已经编写了一个简单的代码。它要求2个数字,比较它并使用If来给出一些结果。它运行良好,但我尝试循环它。当我将第一个数字值设为10时,循环是循环的。这不会像这样工作,因为“首先不能解析为变量”因为它在循环内部。好的我明白了,但是有什么办法可以让我在内部循环中使用变量吗?
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");
}
}
答案 0 :(得分:2)
但是有什么办法可以让我在内部循环中使用变量吗?
没有。你必须在循环之外声明它。
您也可以声明一个布尔变量,并使用它{}为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");
}
但是正如你所看到的,你需要在循环之外声明它。
答案 1 :(得分:2)
正如其他人所说,你必须在循环范围之外声明条件变量。您还可以对您的计划应用其他一些改进:
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 :(得分:1)
首先,请使用lowerCamelCase命名变量。第一个字母总是小写。
不,你不能在循环中声明一个变量并将其用作条件。使用变量时,需要了解定义它的范围。如果在循环内定义它,它将仅在循环内可用。因此,你唯一能做的就是在循环外定义变量,然后在循环中使用它。
int first = 0;
do {
...
first = Integer.parseInt(FirstNum); // changing String to int
...
} while (first == 10);
答案 3 :(得分:0)
不,你必须在循环之外声明变量
你可以这样做
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");
}
}
答案 4 :(得分:0)
您应首先声明您的扫描程序在循环之外。
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
期间循环,则表示如果"首先"您的程序将退出不同于10。
答案 5 :(得分:0)
注意:在ANY循环内声明一个变量(for do,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");
答案 6 :(得分:0)
解决这个问题,不推荐。正如你发布的那样 但是有什么方法可以让我在内部循环中使用变量吗?
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");
}
}