【问题标题】:Java- Temperature conversion loop issueJava-温度转换循环问题
【发布时间】:2018-10-21 00:14:01
【问题描述】:

我正在做关于 java 编程的小练习。

基本上,该应用程序允许用户输入华氏温度并显示等效摄氏温度,或者输入摄氏温度并显示等效华氏温度。 该应用程序包括一个摄氏方法,该方法返回与华氏温度等效的摄氏温度。该应用程序还包含一个 Fahrenheit 方法,该方法返回相当于摄氏温度的华氏温度。

这是我的完整代码:

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”部分存在问题。

谁能帮我解决这个问题?

【问题讨论】:

    标签: java eclipse loops if-statement methods


    【解决方案1】:

    你的逻辑是错误的。您有一个外部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 之后。

    【讨论】:

    • 我将“else”块移到了外部 if 之后。现在它显示消息。感谢您的帮助。
    【解决方案2】:

    您已将所有内容都包含在 if(choice!=3) 中,因此即使您将选项输入为 3,那么包括 Program terminate 在内的 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
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多