即使while循环的条件为false,我的while循环仍会输出数据。
package temperature;
import java.util.Scanner;
public class Temperature {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double tempInput = 0, tempF = 0, counter = 0;
while (tempInput > -100.-0) {
System.out.println("Please enter in temperature in Centigrade");
tempInput = scan.nextDouble();
tempF = (9.0/5.0)*tempInput + 32.0;
System.out.print("(F" + tempF + ")");
System.out.print(" " + "C (" + tempInput + ")");
System.out.println("");
}
}
}
示例输出:
Please enter in temperature in Centigrade
-100
(F-148.0) C (-100.0)
更多:
Please enter in temperature in Centigrade
23
(F73.4) C (23.0)
Please enter in temperature in Centigrade
32
(F89.6) C (32.0)
Please enter in temperature in Centigrade
100
(F212.0) C (100.0)
Please enter in temperature in Centigrade
答案 0 :(得分:1)
目前你的while循环条件是:
while (tempInput > -100.-0) { }
-100.-0
的评估结果为-100。循环在tempInput < -100
时停止,我运行程序并输入-101并且循环终止。
答案 1 :(得分:1)
WHILE LOOP
当特定条件为真时,while语句会不断执行一个语句块。其语法可表示为:
while (expression) {
statement(s)
}
你的循环将一直有效,直到给定的条件为真:
while (tempInput > -100.-0)
直到你提供tempInput的值&gt; -100.0,如果为tempInput提供23的正值,它将检查条件为(23> -100.0),这是真的。因此循环将继续执行,直到tempInput的时间大于-100。
答案 2 :(得分:1)
你可以写:
import java.util.Scanner;
public class Temperature {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double tempInput = 0, tempF = 0, counter = 0;
while (true) {
System.out.println("Please enter in temperature in Centigrade");
tempInput = scan.nextDouble();
if (tempInput <= -100.0) {
break;
}
tempF = (9.0/5.0)*tempInput + 32.0;
System.out.print("(F" + tempF + ")");
System.out.print(" " + "C (" + tempInput + ")");
System.out.println("");
}
}
}
答案 3 :(得分:0)
当您输入数据时,您将立即将其打印出来而不检查条件。如果在打印之前再做另一个。
package temperature;
import java.util.Scanner;
public class Temperature {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double tempInput = 0, tempF = 0, counter = 0;
while (tempInput > -100.-0) {
System.out.println("Please enter in temperature in Centigrade");
tempInput = scan.nextDouble();
tempF = (9.0/5.0)*tempInput + 32.0;
if (tempInput > -100.-0) {
System.out.print("(F" + tempF + ")");
System.out.print(" " + "C (" + tempInput + ")");
System.out.println("");
}
}
}
}
这里的关键概念是while条件将在循环的每次迭代开始时进行评估,而不是像一些初学者所想的那样观察变量的变化。
答案 4 :(得分:-1)
我认为您已将扫描仪输入写入错误的位置。
package temperature;
import java.util.Scanner;
public class Temperature {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double tempInput = 0, tempF = 0, counter = 0;
System.out.println("Please enter in temperature in Centigrade");
tempInput = scan.nextDouble();
while (tempInput > -100. - 0) {
tempF = (9.0 / 5.0) * tempInput + 32.0;
System.out.print("(F" + tempF + ")");
System.out.print(" " + "C (" + tempInput + ")");
System.out.println("");
break;
}
}
}
答案 5 :(得分:-2)
正如我所说,你必须在while循环之外定义Scanner和double变量,以确保每次迭代的值不能为每个双变量ex..tempInput = 0,tempF = 0设置为零,counter = 0;
这将有效。
答案 6 :(得分:-3)
添加一个用于停止循环的其他内容。
package temperature;
import java.util.Scanner;
public class Temperature {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double tempInput = 0, tempF = 0, counter = 0;
while (tempInput > -100.-0) {
System.out.println("Please enter in temperature in Centigrade");
tempInput = scan.nextDouble();
tempF = (9.0/5.0)*tempInput + 32.0;
System.out.print("(F" + tempF + ")");
System.out.print(" " + "C (" + tempInput + ")");
System.out.println("");
} else {
//Stopping the loop
}
}
}