Java 如何循环一个if-else
我在循环我的if-else语句时遇到了麻烦,你能帮我一下吗。我想做的是,当数字被更改为“热”和“冷”时,我想让它回到if-else语句的开头,以便它能够再次更改数字Java 如何循环一个if-else,java,loops,if-statement,repeat,Java,Loops,If Statement,Repeat,我在循环我的if-else语句时遇到了麻烦,你能帮我一下吗。我想做的是,当数字被更改为“热”和“冷”时,我想让它回到if-else语句的开头,以便它能够再次更改数字 import java.util.Scanner; public class test { public static void main(String args[]) { Scanner gus = new Scanner (System.in); double incrd,
import java.util.Scanner;
public class test {
public static void main(String args[])
{
Scanner gus = new Scanner (System.in);
double incrd, hot, cold;
hot = 10;
cold = 9;
System.out.println("press 1 for hotter");
System.out.println("press 2 for colder");
System.out.println("press 3 for more preasure");
System.out.println("press 4 for less pressure");
incrd = gus.nextDouble();
if (incrd == 1) {
hot += 2.5;
cold = cold - 2.5;
} else if (incrd == 2) {
hot = hot - 2.5;
cold += 2.5;
} else if (incrd == 3){
hot += 2.5;
cold += 2.5;
} else if (incrd == 4){
hot = hot - 2.5;
cold = cold - 2.5;
} else {
System.out.println ("invalid Number please enter either 1 or 2");
}
System.out.println(hot);
System.out.println(cold);
}
}
这将通过一个无限循环来完成。无限循环如下所示:
while (true) {
// do stuff
}
把你想要循环的代码一遍又一遍地放在循环中。你完了
好的,差不多完成了。这种代码永远不会停止。您可以添加另一个菜单选项来停止程序,如果用户按下该数字,您将运行以下代码行:
break;
这就停止了循环和程序。打印出热的和冷的。你需要一个while循环,比如:
import java.util.Scanner;
public class test {
public static void main(String args[])
{
Scanner gus = new Scanner (System.in);
double hot, cold;
int incrd;
hot = 10;
cold = 9;
System.out.println("press 1 for hotter");
System.out.println("press 2 for colder");
System.out.println("press 3 for more preasure");
System.out.println("press 4 for less pressure");
System.out.println("press 5 to finish.");
while (true) {
incrd = gus.nextInt();
if (incrd == 1) {
hot += 2.5;
cold -= 2.5;
} else if (incrd == 2) {
hot -= 2.5;
cold += 2.5;
} else if (incrd == 3){
hot += 2.5;
cold += 2.5;
} else if (incrd == 4){
hot -= 2.5;
cold -= 2.5;
} else if (incrd == 5){
break;
} else {
System.out.println ("invalid Number please enter either 1 or 2");
}
}
System.out.println(hot);
System.out.println(cold);
}
}
对于无限循环,您也可以这样做:
while (incrd.hasNextInt()) {
incrd = gus.nextInt();
....
}
此外,您将incrd定义为双倍数字。因此,最好不要使用“=”将其与整数进行比较 一个问题是,你描述它的方式不是很清楚,但如果我理解正确,你只需要把它放在一个while(true)循环中。我不喜欢为别人做家庭作业,但这是你问题的答案
import java.util.Scanner;
public class test {
public static void main(String args[])
{
Scanner gus = new Scanner (System.in);
double incrd, hot, cold;
hot = 10;
cold = 9;
while(true){
System.out.println("press 1 for hotter");
System.out.println("press 2 for colder");
System.out.println("press 3 for more preasure");
System.out.println("press 4 for less pressure");
incrd = gus.nextDouble();
if (incrd == 1) {
hot += 2.5;
cold = cold - 2.5;
} else if (incrd == 2) {
hot = hot - 2.5;
cold += 2.5;
} else if (incrd == 3){
hot += 2.5;
cold += 2.5;
} else if (incrd == 4){
hot = hot - 2.5;
cold = cold - 2.5;
} else {
System.out.println ("invalid Number please enter either 1 or 2");
}
System.out.println(hot);
System.out.println(cold);
}
}
}
我将使用while循环返回到您的顶部if/else块:
while (1) {
incrd = gus.nextDouble();
if (incrd == 1) {
hot += 2.5;
cold = cold - 2.5;
} else if (incrd == 2) {
hot = hot - 2.5;
cold += 2.5;
} else if (incrd == 3){
hot += 2.5;
cold += 2.5;
} else if (incrd == 4){
hot = hot - 2.5;
cold = cold - 2.5;
} else {
// 3 & 4 are valid options also !
System.out.println ("invalid Number please enter either 1 or 2");
}
System.out.println(hot);
System.out.println(cold);
} // end of INFINITE while loop
试试这个!!如果格斯。返回-1我们停止代码
import java.util.Scanner;
public class test {
public static void main(String args[])
{
Scanner gus = new Scanner (System.in);
double incrd, hot, cold;
hot = 10;
cold = 9;
int StopValue;
//If the value is -1 we stop the loop
StopValue=-1;
System.out.println("press 1 for hotter");
System.out.println("press 2 for colder");
System.out.println("press 3 for more preasure");
System.out.println("press 4 for less pressure");
while (true) {
incrd = gus.nextDouble();
if (incrd == StopValue)
{
break;
}
else
{
if (incrd == 1) {
hot += 2.5;
cold = cold - 2.5;
} else if (incrd == 2) {
hot = hot - 2.5;
cold += 2.5;
} else if (incrd == 3){
hot += 2.5;
cold += 2.5;
} else if (incrd == 4){
hot = hot - 2.5;
cold = cold - 2.5;
} else if (incrd == 5){
break;
} else {
System.out.println ("invalid Number please enter either 1 or 2");
}
}
System.out.println(hot);
System.out.println(cold);
}//else
}//while
}
我想这样的东西很适合你的需要:
(实际上,当您输入任何非整数值时,它将退出程序.)
把它放在一个循环中。这个代码没有循环。@nhgrif,我想他说的是循环;他正试图使它成为一个基于菜单的程序。如果选择无效;回去重新开始。不确定。为什么你用
+=
来增加数字,而不用-=
来减少数字?@Barmar可能是因为很多初学者不知道有一个世界在+=
之外。类似于许多高级用户甚至不考虑%=
。试试我给你的方法,如果你在StopValue变量中点击声明的值,循环结束,你就退出了。。。如果你不做类似于我建议你做的事情,那么真的不会结束,我想,而不是说While(1)
<代码>while(true)是更好的可读性原因。
public class test {
public static void main(String args[]) {
Scanner gus = new Scanner(System.in);
int incrd;
double hot, cold;
hot = 10;
cold = 9;
System.out.println("press 1 for hotter");
System.out.println("press 2 for colder");
System.out.println("press 3 for more preasure");
System.out.println("press 4 for less pressure");
System.out.println("press 'x' to Exit the program");
while (gus.hasNextInt()) {
incrd = gus.nextInt();
if (incrd == 1) {
hot += 2.5;
cold = cold - 2.5;
} else if (incrd == 2) {
hot = hot - 2.5;
cold += 2.5;
} else if (incrd == 3) {
hot += 2.5;
cold += 2.5;
} else if (incrd == 4) {
hot -= 2.5;
cold -= 2.5;
} else {
System.out.println("invalid Number please enter either 1 or 2");
}
System.out.println(hot);
System.out.println(cold);
}
}
}