我正在做一些关于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
}
好东西是,代码,包括方法是working.The用户可以输入选择1或2,然后输入温度数字,并以摄氏度或华氏度显示它。但如果用户输入选项3,程序将显示'Program terminated‘。对我来说,当我进入选项3时,程序停止工作(没有显示'Program terminated')。我认为'loop‘部分或者'IF’部分有问题。
有人能帮我吗?
发布于 2018-10-21 11:29:20
您已经将所有内容都包含在if( choice !=3)中,所以即使您输入choice作为3,也不会运行else included terminated。所以只要重新组织一下括号,一切都会好起来的。
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
}
https://stackoverflow.com/questions/52911098
复制相似问题