[英]Java- Temperature conversion loop issue
我在做Java编程小练习。
基本上,该应用程序允许用户输入华氏温度并显示摄氏温度或输入摄氏温度并显示华氏温度。 该应用程序包括一个摄氏温度方法,该方法返回等于华氏温度的摄氏温度。 该应用程序还包括一个华氏温度方法,该方法返回等于摄氏温度的华氏温度。
这是我的完整代码:
import java.util.Scanner;
public class Temperature{
public static void main(String[] args) { // Main Method//
Scanner input = new Scanner(System.in);
//Declare variables
int choice; // the user's choice in the menu //
int temp;
do {
// print the menu
System.out.println("1.fahrenheit to Celsius");
System.out.println("2.Celsius to Fahrenheit");
System.out.println("3.Exit");
System.out.println("");
System.out.println("Choice:");
choice = input.nextInt();
if ( choice != 3 ) {
System.out.print( "Enter temperature: " ); // Display Enter Temperature
temp = input.nextInt(); // Read Temperature
if (choice == 1) {
System.out.println( temp + " Fahrenheit is " + toCelsius( temp ) + " Celsius ");
System.out.println("");
}
else if (choice == 2 ) {
System.out.println(temp + " Celsius is " + toFahrenheit( temp ) + " Fahrenheit ");
System.out.println("");
}
else {
System.out.println("Program terminated "); // Display "Program terminated" if user entered 3
}
} //end if loop
} // end do loop
while (choice !=3);
} // end main method
// return Celsius equivalent of Fahrenheit temperature
public static int toCelsius(int fahrenheit) {
int celsius;
celsius = (int) (5.0/9.0 * (fahrenheit - 32));
return celsius;
} // end method celsius
// return Fahrenheit equivalent of Celsius temperature
public static int toFahrenheit(int celsius) {
int fahrenheit;
fahrenheit = (int) (9.0/5.0 * celsius + 32);
return fahrenheit;
} // end method fahrenheit
}
很好的是,包括方法在内的代码都可以正常工作。用户可以输入选项1或2,然后输入温度数字并以摄氏度或华氏度显示。 但是,如果用户输入选项3,程序将显示“程序已终止”。 对我来说,当我输入选项3时,程序停止工作(未显示“程序已终止”)。 我认为“循环”部分或“ IF”部分存在问题。
谁能帮我这个?
你的逻辑是错误的。 您有一个外部if
语句,仅当用户未输入三个时才输入。 因此,如果执行此操作,则内部if
的else
将不会执行,因此您的消息将永远不会打印:
//If you do enter three, it will skip over all of this:
if ( choice != 3 ) {
System.out.print( "Enter temperature: " ); // Display Enter Temperature
temp = input.nextInt(); // Read Temperature
if (choice == 1) {
System.out.println( temp + " Fahrenheit is " + toCelsius( temp ) + " Celsius ");
System.out.println("");
}
else if (choice == 2 ) {
System.out.println(temp + " Celsius is " + toFahrenheit( temp ) + " Fahrenheit ");
System.out.println("");
}
else {
System.out.println("Program terminated "); // Display "Program terminated" if user entered 3
}
}
您需要删除外部if
,或者将else
块移动到外部if
。
您已将所有内容都包含在if(choice!= 3)中,因此即使您将choice输入为3,包含程序终止的else也将无法运行。 因此,只要重新组织括号,一切都会正常进行。
import java.util.Scanner;
public class Temperature{
public static void main(String[] args) { // Main Method//
Scanner input = new Scanner(System.in);
//Declare variables
int choice; // the user's choice in the menu //
int temp;
do {
// print the menu
System.out.println("1.fahrenheit to Celsius");
System.out.println("2.Celsius to Fahrenheit");
System.out.println("3.Exit");
System.out.println("");
System.out.println("Choice:");
choice = input.nextInt();
if ( choice != 3 ) {
System.out.print( "Enter temperature: " ); // Display Enter Temperature
temp = input.nextInt(); // Read Temperature
if (choice == 1) {
System.out.println( temp + " Fahrenheit is " + toCelsius( temp ) +" Celsius ");
System.out.println("");
}
else if (choice == 2 ) {
System.out.println(temp + " Celsius is " + toFahrenheit( temp ) + " Fahrenheit ");
System.out.println("");
}
}
else {
System.out.println("Program terminated "); // Display "Program terminated" if user entered 3
}//end else loop
} // end do loop
while (choice !=3);
} // end main method
// return Celsius equivalent of Fahrenheit temperature
public static int toCelsius(int fahrenheit) {
int celsius;
celsius = (int) (5.0/9.0 * (fahrenheit - 32));
return celsius;
} // end method celsius
// return Fahrenheit equivalent of Celsius temperature
public static int toFahrenheit(int celsius) {
int fahrenheit;
fahrenheit = (int) (9.0/5.0 * celsius + 32);
return fahrenheit;
} // end method fahrenheit
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.