Java 我的 do/while 循环不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18893023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
My do/while loop is not working
提问by user2764700
So I have a do-while loop that's supposed to read user input. If the user types J then it should ask the user to type in 2 numbers to calculate them but if the user types N then it should stop the loop. Any idea why it isn't working?
所以我有一个 do-while 循环,它应该读取用户输入。如果用户输入 J 那么它应该要求用户输入 2 个数字来计算它们,但是如果用户输入 N 那么它应该停止循环。知道为什么它不起作用吗?
if (input.equalsIgnoreCase("J")) {
do
{
status=true;
}
while (status);{
if (input.equalsIgnoreCase("N"))
status = false;
}
}
Full code:
完整代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean status=true;
while (status){
Scanner minScanner1 = new Scanner(System.in); // mina scanners som tar in userinput
Scanner scanner2 = new Scanner(System.in);
System.out.println("Skriv in ditt nummer en och en :\n"); //obivous
double nr1 = minScanner1.nextDouble(); // int nr 1 lagrar det userinput skriver in p? scanner 1
double nr2 = minScanner1.nextDouble();
System.out.println("v?lj vad du vill g?ra: ");
double svar =0;
String anv?ndarInl?gg = scanner2.nextLine(); // sj?lva minir?knaren
if(anv?ndarInl?gg.equalsIgnoreCase("+")) {
svar = nr1 + nr2;
}
else if(anv?ndarInl?gg.equalsIgnoreCase("-")) {
svar = nr1 - nr2;
}
else if(anv?ndarInl?gg.equalsIgnoreCase("*")) {
svar = nr1 * nr2;
}
else if(anv?ndarInl?gg.equalsIgnoreCase("/")) {
svar = nr1 / nr2;
}
System.out.printf("= %.2f\n ", svar);
System.out.println("Skriv in J f?r att forts?tta N f?r att sluta: \n");
Scanner scanner3 = new Scanner(System.in);
String input=scanner3.nextLine();
do
{
if (input.equalsIgnoreCase("N")) status = false;
else if (input.equalsIgnoreCase("J")) status =true;
} while(true);
}
}
}
回答by Math
The do-while
structure is this part only:
该do-while
结构仅仅是这一部分:
do
{
status=true;
}
while (status);
All the rest don't belong to the do-while
structure.
其余的都不属于该do-while
结构。
BTW, this will be an infinite loop, since the while condition will always be true.
顺便说一句,这将是一个无限循环,因为 while 条件将始终为真。
EDIT
编辑
This might be what you really want:
这可能是你真正想要的:
do
{
//TODO here: update input variable
if (input.equalsIgnoreCase("N")) status = false;
} while(status);
EDIT2
编辑2
String input;
do
{
scanner3 = new Scanner(System.in);
input=scanner3.nextLine();
if (input.equalsIgnoreCase("N")) status = false;
} while(status);
回答by JohnnyAW
try this code:
试试这个代码:
package com.example.main;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
public static void main(String[] args) {
boolean status = true;
Scanner sc = new Scanner(System.in);
while (status){
System.out.println("Skriv in ditt nummer en och en :\n");
double nr1 = Double.parseDouble(sc.nextLine());
double nr2 = Double.parseDouble(sc.nextLine());
System.out.println("v?lj vad du vill g?ra: ");
double svr = 0;
String anv?ndarInl?gg = sc.nextLine();
if (anv?ndarInl?gg.equalsIgnoreCase("+")) {
svr = nr1 + nr2;
} else if (anv?ndarInl?gg.equalsIgnoreCase("-")) {
svr = nr1 - nr2;
} else if (anv?ndarInl?gg.equalsIgnoreCase("*")) {
svr = nr1 * nr2;
} else if (anv?ndarInl?gg.equalsIgnoreCase("/")) {
svr = nr1 / nr2;
}
System.out.printf("= %.2f\n ", svr);
System.out
.println("Skriv in J f?r att forts?tta N f?r att sluta: \n");
String input = sc.nextLine();
if (input.equalsIgnoreCase("N"))
status = false;
}
}
}
you set status to false, if input is "N"
如果输入为“N”,则将状态设置为 false
EDIT: put scanner-init out of while
编辑:把扫描仪初始化放在外面
old answer:
旧答案:
its infinite loop, you set status to true but, you iterate as long as status IS true
它的无限循环,您将状态设置为真,但是,只要状态为真,您就进行迭代
do
{
status=true;
}
while (status);
actually i posted you a workaround for that problem today on your other question...
实际上,我今天在您的另一个问题上为您发布了解决该问题的方法...
回答by LuigiEdlCarno
Your loop is:
你的循环是:
do{
status=true;
} while (status);
The if
clause is never reached. You need to put that if
clause inside the loop.
该if
条款从未达成。您需要将该if
子句放入循环中。
回答by Prabhaker A
try this
尝试这个
do
{
if (input.equalsIgnoreCase("J")) {
status=true;
}
else if (input.equalsIgnoreCase("N"))
status = false;
}
}while (status);
回答by A4L
This is not the correct syntax fo do/while loop
这不是 do/while 循环的正确语法
it has to be
它一定要是
do {
// body
} while(condition);
The while loop looks like that
while 循环看起来像这样
while(condition) {
// body
}
You have to change your code to something like that:
您必须将代码更改为以下内容:
String input;
boolean status;
do {
input = // read input
status = input.equalsIgnoreCase("J");
} while(status);