java – if … elseif一次又一次输入值时出错
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – if … elseif一次又一次输入值时出错,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2251字,纯文字阅读大概需要4分钟。
内容图文
![java – if … elseif一次又一次输入值时出错](/upload/InfoBanner/zyjiaocheng/757/31de10d59f94430ebd5ade775aaa1e5c.jpg)
在这个程序中,我必须一次又一次地在循环中输入温度.当我输入温度一旦它显示正确的显示,但当我再次输入温度时,程序自行终止.
代码是:
import java.util.Scanner;
public class CheckTemperature
{
public static void main(String [] args)
{
final double TEMPERATURE = 102.5;
double thermostat;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the substance's temperature: ");
thermostat = keyboard.nextDouble();
if(thermostat > TEMPERATURE)
{
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat < TEMPERATURE)
{
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else if(thermostat == TEMPERATURE)
{
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
else
{
System.out.println("Enter the correct temperature value ");
System.out.println("Enter the thermostat here: ");
thermostat = keyboard.nextDouble();
}
}
}
解决方法:
这是因为你不在循环中.你问温度一次在顶部.
然后在if中,然后结束.
public static void main(String [] args)
{
final double TEMPERATURE = 102.5;
double thermostat;
Scanner keyboard = new Scanner(System.in);
while (true) {
System.out.println("Enter the substance's temperature: ");
thermostat = keyboard.nextDouble();
if(thermostat > TEMPERATURE)
{
System.out.print("The temperature is too high. ");
System.out.print("Turn down the thermostat. ");
System.out.println("Wait for 5 minutes and check the thermostat again. ");
}
else if(thermostat < TEMPERATURE)
{
System.out.println("The temperature is low.");
System.out.println("Turn up the thermostat.");
System.out.println("Check for 5 minutes and check again.");
}
else if(thermostat == TEMPERATURE) {
System.out.println("The temperature is acceptable. Check again in 15 minutes.");
}
else{
System.out.println("Enter the correct temperature value ");
}
}
}
小心,暂时不会停止,但你可以根据自己的意愿改变条件.
内容总结
以上是互联网集市为您收集整理的java – if … elseif一次又一次输入值时出错全部内容,希望文章能够帮你解决java – if … elseif一次又一次输入值时出错所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】