首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java-温度转换循环问题

Java-温度转换循环问题
EN

Stack Overflow用户
提问于 2018-10-21 08:14:02
回答 1查看 702关注 0票数 0

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

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

下面是我的完整代码:

代码语言:javascript
复制
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’部分有问题。

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-21 11:29:20

您已经将所有内容都包含在if( choice !=3)中,所以即使您输入choice作为3,也不会运行else included terminated。所以只要重新组织一下括号,一切都会好起来的。

代码语言:javascript
复制
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

  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52911098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档