在Java中执行while循环更改变量
我一直在尝试做一个选择你的冒险类型的程序,但我遇到了一个问题。我在do-while循环中运行整个过程,每个选项都是数组中的一个元素。我在do中也有if语句,它可以根据用户已经完成的操作更改某些元素。下面是我的代码的示例:在Java中执行while循环更改变量,java,if-statement,do-while,Java,If Statement,Do While,我一直在尝试做一个选择你的冒险类型的程序,但我遇到了一个问题。我在do-while循环中运行整个过程,每个选项都是数组中的一个元素。我在do中也有if语句,它可以根据用户已经完成的操作更改某些元素。下面是我的代码的示例: import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner input; input=new S
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner input;
input=new Scanner(System.in);
boolean run;
boolean theBoo = false;
run = true;
int choice;
choice = 0;
do {
String[] theArray;
theArray = new String[2];
theArray[0] = "Hello";
if(theBoo){
theArray[1] = "Goodbye";
}
else{
theArray[1] = "Hi";
theBoo = true;
}
System.out.println(theArray[choice]);
choice = input.nextInt();
} while(run);
}
}
但出于某种原因,如果您输入1,它会打印“再见”,即使它应该打印“Hi”,因为boo为false。我的问题是:为什么do while循环会更改变量的值,以及如何防止它这样做?谢谢
编辑:顺便说一句,我是新来的,所以如果我做错了什么,我道歉
编辑2:首先,谢谢大家的快速回答。我做了你推荐的更改,但它仍然在做同样的事情。我通过更改将代码更新为原来的状态。使用
=
而不是=
来测试是否相等。事实上,对于布尔人来说,你不需要任何明确的等号。你只需要:
if(theBoo)
//something
else
//something else
while(run)
boolean theBoo = false;
请注意,如果没有两个if
s,您就不会有两个if。不仅没有必要,而且测试(theBoo==true)
然后如果(theBoo==false)
(其中,您可以完全忽略==true
或==false
部分)在逻辑上也没有意义。如果第一个条件为真,则第二个条件不能(因为它与第一个条件相反),因此您确实需要一个else
或else If
与底部while条件的==
相同。你只需要:
if(theBoo)
//something
else
//something else
while(run)
boolean theBoo = false;
另一个注释-您通常不需要在声明的另一行定义变量。你只需要:
if(theBoo)
//something
else
//something else
while(run)
boolean theBoo = false;
而不是:
boolean theBoo;
theBoo = false;
第一条路在同一条线上更短更干净 使用=
而不是=
来测试是否相等。事实上,对于布尔人来说,你不需要任何明确的等号。你只需要:
if(theBoo)
//something
else
//something else
while(run)
boolean theBoo = false;
请注意,如果没有两个if
s,您就不会有两个if。不仅没有必要,而且测试(theBoo==true)
然后如果(theBoo==false)
(其中,您可以完全忽略==true
或==false
部分)在逻辑上也没有意义。如果第一个条件为真,则第二个条件不能(因为它与第一个条件相反),因此您确实需要一个else
或else If
与底部while条件的==
相同。你只需要:
if(theBoo)
//something
else
//something else
while(run)
boolean theBoo = false;
另一个注释-您通常不需要在声明的另一行定义变量。你只需要:
if(theBoo)
//something
else
//something else
while(run)
boolean theBoo = false;
而不是:
boolean theBoo;
theBoo = false;
第一条路在同一条线上更短更干净 使用one=sign,您将boo的值设置为true,而不是测试其值
用于测试
固定代码
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner input;
input=new Scanner(System.in);
boolean run;
boolean theBoo;
theBoo = false;
run = true;
int choice;
choice = 0;
do{
String[] theArray;
theArray = new String[2];
theArray[0] = "Hello";
if(theBoo == true){
theArray[1] = "Goodbye";
}
if(theBoo == false){
theArray[1] = "Hi";
theBoo = true;
}
System.out.println(theArray[choice]);
choice = input.nextInt();
}while(run == true);
}
}
使用one=sign,将theBoo的值设置为true,而不是测试其值
用于测试
固定代码
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner input;
input=new Scanner(System.in);
boolean run;
boolean theBoo;
theBoo = false;
run = true;
int choice;
choice = 0;
do{
String[] theArray;
theArray = new String[2];
theArray[0] = "Hello";
if(theBoo == true){
theArray[1] = "Goodbye";
}
if(theBoo == false){
theArray[1] = "Hi";
theBoo = true;
}
System.out.println(theArray[choice]);
choice = input.nextInt();
}while(run == true);
}
}
将代码更新为
if(theBoo){
theArray[1] = "Goodbye";
}
else{
theArray[1] = "Hi";
theBoo = true;
}
将代码更新为
if(theBoo){
theArray[1] = "Goodbye";
}
else{
theArray[1] = "Hi";
theBoo = true;
}
这是Java中静态类型无法防止愚蠢错误的情况之一
表达式x=v
始终是赋值,结果的类型与v
表达式的类型相同。不幸的是,在这种情况下,这意味着boolean
类型的结果在与条件一起使用时不会引发类型错误
boolean a = true;
// a -> true
boolean b = a = false; // boolean b = (a = false);
// a -> false
// b -> false
这是Java中静态类型无法防止愚蠢错误的情况之一
表达式x=v
始终是赋值,结果的类型与v
表达式的类型相同。不幸的是,在这种情况下,这意味着boolean
类型的结果在与条件一起使用时不会引发类型错误
boolean a = true;
// a -> true
boolean b = a = false; // boolean b = (a = false);
// a -> false
// b -> false
使用=
进行比较。@ZouZou所说的不是暗示,而是问题的根本原因!您可以使用IDE进行代码格式化,阅读原始文章非常困难。许多错误源于低代码可读性。为什么人们坚持使用if(boolean==true)
。很明显,if
语句对boolean
进行求值,因此很明显,if(boolean)
是正确的构造。这也是防止if(boolean=true)
的一个很好的特性,这就是我们这里的功能。这里的人们甚至不可能浪费10秒钟的时间去理解乱七八糟的卷发背带。请善待他人,并始终提供干净、格式良好的代码。使用==
进行比较。@ZouZou所说的不是暗示,而是问题的根本原因!您可以使用IDE进行代码格式化,阅读原始文章非常困难。许多错误源于低代码可读性。为什么人们坚持使用if(boolean==true)
。很明显,if
语句对boolean
进行求值,因此很明显,if(boolean)
是正确的构造。这也是防止if(boolean=true)
的一个很好的特性,这就是我们这里的功能。这里的人们甚至不可能浪费10秒钟的时间去理解乱七八糟的卷发背带。请善待他人,并始终提供干净、格式良好的代码。