Java-温度转换循环问题

标签 java eclipse loops if-statement methods

我正在做 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 block 移至外部 if 后面。

关于Java-温度转换循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52911098/

相关文章:

bash - 当我使用命令 "at"执行脚本时,出现无限循环

python - 多线程 python 应用程序在运行其线程时挂起

java - 找不到 ID 为 'gwt' 的插件

java - 词法分析器操作中不允许使用属性引用

java - 创建名称为 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration' 的 bean 时出错

jquery - 循环 jQuery 函数来检查 div 位置

java - 使用 Java 连接到 Azure 中的 MongoDB

java - 静态非静态引用混淆

eclipse - 如何从 eclipse 的 tomcat 插件更改 tomcat 的端口

java - 将操作添加到表单页面中的工具栏