我的Java方法运行不正确并导致我的程序编译错误

时间:2016-10-17 19:07:27

标签: java

我正在编写一个程序,它会提示用户输入temp作为整数,然后提示用户选择temp所在的比例,华氏度,摄氏度或开尔文。然后它会提示用户从3个刻度选项中选择要转换为的温度,然后计算转换并显示最终温度。我的程序目前运行但运行不正确,我已经盯着这几天了,我不知道它为什么要编译它的方式。我仍然是一个相当新的编码,这是一个我一直在寻找一些建议的课程作业。

封装tempconverter; import java.util。*;

公共类TempConverter {

public static void main(String[] args) 
{
    double temp1;
    double convertTo;
    double finalTemp;
    double currScale;
    char cont='E';
    char process='P';
    int scale1=0;
    int scale2=0;

    do
    {
        //input
        getTemp();
        scaleOne(scale1);
        toScale(scale2);

        //process


        System.out.print("Your final temp is: " +convert()+toScale(scale2));

    }while(cont=='N');
}   
    private static double getTemp()
    {
         double temp;
         double currentTemp;
         Scanner userIn= new Scanner(System.in);
         System.out.print("Enter a temperature to convert ");
         while(!userIn.hasNextDouble())
         {
          System.out.println("That is not a valid temperature!");
          System.out.print("Re-enter a temperature: ");
          userIn.next();
         }
         temp=userIn.nextDouble();



         return temp;
    }

    private static String scaleOne(int userChoice)
    {

        Scanner userIn= new Scanner(System.in);



        System.out.print("What scale is your temp currently in?"+
                          "1: Fahrenheit" + 
                          "2: Celsius" + 
                          "3: Kelvin" + 
                          "Please enter 1, 2, or 3: ");


        String scale="";

        switch(userChoice)
        {
            case 1:
            {scale= "Fahrenheit";
            break;}

            case 2:
            {scale="Celsius";
            break;}

            case 3:
            {scale="Kelvin";
            break;}

            default:
            {scale= "There was an error in your choice!";}
        }

        return scale;

    }



    private static String toScale(int userChoice)
    {

        Scanner userIn= new Scanner(System.in);



        System.out.print("What scale would you like to convert to?"+
                          "1: Fahrenheit" + 
                          "2: Celsius" + 
                          "3: Kelvin" + 
                          "Please enter 1, 2, or 3: ");


        String scale2="";

        switch(userChoice)
        {
            case 1:
            {scale2= "Fahrenheit";
            break;}

            case 2:
            {scale2="Celsius";
            break;}

            case 3:
            {scale2="Kelvin";
            break;}

            default:
            {scale2= "There was an error in your choice!";}
        }

        return scale2;

    }



    public static double convert()
    {
        double farToKel;
        double farToCel;
        double celToKel;
        double celToFar;
        double kelToFar;
        double kelToCel;
        int scale1=0;
        double converts;

        if(scaleOne(scale1)=="Fahrenheit" && toScale(scale1)=="Kelvin")        
        {
         converts=farToKel=(getTemp()+459.67)* 5/9;
        }

        else if(scaleOne(scale1)=="Fahrenheit" && toScale(scale1)=="Celsius")
        {
         converts=farToCel=(getTemp()-32)*5/9;
        }

        else if(scaleOne(scale1)=="Celsius" && toScale(scale1)=="Kelvin")
        {
         converts=celToKel=getTemp()+273.15;
        }

        else if(scaleOne(scale1)=="Celsius" && toScale(scale1)=="Fahrenheit")
        {
         converts=celToFar=getTemp()*9/5+32;
        }

        else if(scaleOne(scale1)=="Kelvin" && toScale(scale1)=="Fahrenheit")
        {
         converts=kelToFar=getTemp()*9/5-459.67;
        }

        else
        { 
         converts=kelToCel=getTemp()-276.15;
        }

        return converts;
    }








    }

这就是它编译的内容

运行: 输入要转换100的温度 你的气温目前是多少?1:华氏2:摄氏3:开尔文请输入1,2或3:你要转换成什么尺度?1:华氏2:摄氏3:开尔文请输入1,2或3:什么尺度是你的临时状态是什么?1:华氏温度2:摄氏度3:开尔文请输入1,2或3:你的温度目前是多少?1:华氏温度2:摄氏温度3:开尔文请输入1,2或3:你的温度目前是多少?1:华氏温度2:摄氏度3:开尔文请输入1,2或3:你当前的温度是多少?1:华氏温度2:摄氏温度3:开尔文请输入1,2或3:你的温度目前是多少?1:华氏温度2 :Celsius3:Kelvin请输入1,2或3:输入要转换的温度2 你希望转换成什么尺度?1:华氏2:摄氏3:开尔文请输入1,2或3:你的最终温度是:-274.15你选择的错误!建立成功(总时间:33秒)

它似乎没有按照正确的顺序进行编译,或者以正确的顺序获取正确的信息并一遍又一遍地获取一些信息。输入数字温度后,它会提示您输入刻度,但不会只是一次打印出来,然后您输入第一个刻度的选择,它就会一直完成而无需您输入选择toScale。

4 个答案:

答案 0 :(得分:3)

请参阅下面的代码中插入的注释和更正。

public class TempConverter {

    // variables that are needed in more than one method must be declared outside the methods
    // (static since the methods are static; not recommended, but let it be for now)
    static double temp1;
    static String scale1;
    static String scale2;

    public static void main(String[] args) {
        // get rid of unused variables
        char cont = 'E';

        do {
            // input
            // save all inputs to variables
            temp1 = getTemp();
            // Java parameters are pass by value, so to get a value out of the method, use the returned value
            // then you don’t need the parameter
            scale1 = scaleOne();
            scale2 = toScale();

            // process
            // don’t call toScale() again, or the user will be prompted again,
            // instead, use the value already stored in scale2
            System.out.print("Your final temp is: " + convert() + ' ' + scale2);

            // you should set cont to something if you want the loop to repeat

        } while (cont == 'N');
    }

    private static double getTemp() {
        double temp;
        Scanner userIn = new Scanner(System.in);
        System.out.print("Enter a temperature to convert ");
        while (!userIn.hasNextDouble()) {
            System.out.println("That is not a valid temperature!");
            System.out.print("Re-enter a temperature: ");
            userIn.next();
        }
        temp = userIn.nextDouble();

        return temp;
    }

    private static String scaleOne() {

        Scanner userIn = new Scanner(System.in);

        // \n is a line break for readability
        System.out.print("What scale is your temp currently in?" 
                + "\n1: Fahrenheit" 
                + "\n2: Celsius" 
                + "\n3: Kelvin"
                + "\nPlease enter 1, 2, or 3: ");

        // read a value from the user
        int userChoice = userIn.nextInt();

        String scale = "";

        switch (userChoice) {
        case 1: {
            scale = "Fahrenheit";
            break;
        }

        case 2: {
            scale = "Celsius";
            break;
        }

        case 3: {
            scale = "Kelvin";
            break;
        }

        default: {
            scale = "There was an error in your choice!";
        }
        }

        return scale;

    }

    private static String toScale() {

        Scanner userIn = new Scanner(System.in);

        System.out.print("What scale would you like to convert to?" + "1: Fahrenheit" + "2: Celsius" + "3: Kelvin"
                + "Please enter 1, 2, or 3: ");

        String scale2 = "";

        int userChoice = userIn.nextInt();

        // ideally should not accept the same scale as scale1 (from scale)
        switch (userChoice) {
        case 1: {
            scale2 = "Fahrenheit";
            break;
        }

        case 2: {
            scale2 = "Celsius";
            break;
        }

        case 3: {
            scale2 = "Kelvin";
            break;
        }

        default: {
            scale2 = "There was an error in your choice!";
        }
        }

        return scale2;

    }

    public static double convert() {
        // need only one variable for result
        double converts;

        // don’t call methods again, use variables
        // don’t use == for comparing strings, use .equals
        // typo: the second scale corrected to scale2
        if (scale1.equals("Fahrenheit") && scale2.equals("Kelvin")) {
            // don’t call getTemp() again
            converts = (temp1 + 459.67) * 5 / 9;
        }

        else if (scale1.equals("Fahrenheit") && scale2.equals("Celsius")) {
            converts = (temp1 - 32) * 5 / 9;
        }

        else if (scale1.equals("Celsius") && scale2.equals("Kelvin")) {
            converts = temp1 + 273.15;
        }

        else if (scale1.equals("Celsius") && scale2.equals("Fahrenheit")) {
            converts = temp1 * 9 / 5 + 32;
        }

        else if (scale1.equals("Kelvin") && scale2.equals("Fahrenheit")) {
            converts = temp1 * 9 / 5 - 459.67;
        }

        else { // Kelvin to Celsius
            converts = temp1 - 276.15;
        }

        return converts;
    }

}

我相信它现在基本上有用了。

答案 1 :(得分:1)

public static void main(String[] args)
{


    String scaleOne;
    String scaleTwo;

    System.out.println("What scale is your temp currently in?\n"+
            "1: Fahrenheit\n" +
            "2: Celsius\n" +
            "3: Kelvin\n" +
            "Please enter 1, 2, or 3: \n");

    scaleOne = getScale();

    System.out.println("What scale would you like to convert to?\n"+
            "1: Fahrenheit\n" +
            "2: Celsius\n" +
            "3: Kelvin\n" +
            "Please enter 1, 2, or 3:\n ");

    scaleTwo = getScale();
    System.out.print("Your final temp is: " +convert(scaleOne,scaleTwo));


}
private static double getTemp()
{
    double temp;
    Scanner userIn= new Scanner(System.in);
    System.out.print("Enter a temperature to convert ");
    while(!userIn.hasNextDouble())
    {
        System.out.println("That is not a valid temperature!");
        System.out.println("Re-enter a temperature: ");
        userIn.next();
    }
    temp=userIn.nextDouble();

    return temp;
}

private static String getScale()
{

    Scanner userIn= new Scanner(System.in);

    String scale="";

    switch(userIn.nextInt())
    {
        case 1:
            scale= "Fahrenheit";
            break;

        case 2:
            scale="Celsius";
            break;

        case 3:
            scale="Kelvin";
            break;

        default:
            scale= "There was an error in your choice!";
    }

    return scale;

}

相应地更改主函数以及scaleone()和toScale()函数。您的代码存在许多无法单独解决的问题。请随时询问有关答案的任何说明

答案 2 :(得分:1)

你必须相信编译器正确地完成它的工作。错误在于你的逻辑。我假设您正在尝试创建一个程序,将温度从3种格式转换为其他格式。

  1. 在调用scaleOne(scale1);方法时,您提供了一个硬编码值0作为参数,并且从未在方法内部进行更改。同时你试图从用户那里获得输入。要获取用户输入,您需要在方法正文中添加int userChoice = userIn.nextInt();。 这将返回一个sting值,您需要将其存储在main方法的sacle1变量中。

  2. 类似于toScale()方法,您需要使其非参数化并从用户内部方法中获取输入。 这将再次返回一个sting值,您需要将其存储在main方法的sacle2变量中。

  3. 现在,在调用转换方法时,您需要传递scale1scale2作为参数。我不明白在每个if条件下再次调用scaleOne()toScale()方法的逻辑是什么。这是您的代码一次又一次地要求选择温度类型的原因。我在解决方案中使用scale1scale2变量删除了它们。 永远记住,永远不要使用==运算符进行String比较。始终使用字符串类的equals()equalsEgnoreCase()方法。

  4. 我已对您的代码进行了更改,并尝试根据需要获得更具体的解决方案。希望这会有所帮助。不要忘了投票。

        public class TempConverter {
        public static void main(String[] args) 
    {
        String scale1;
        String scale2;   
            //input
           scale1 =scaleOne();
           scale2 = toScale();
        //process
        System.out.print("Your final temp is: " +convert(scale1, scale2));
    }   
        private static double getTemp()
        {
             double temp;
             Scanner userIn= new Scanner(System.in);
             System.out.print("Enter a temperature to convert ");
             while(!userIn.hasNextDouble())
             {
              System.out.println("That is not a valid temperature!");
              System.out.print("Re-enter a temperature: ");
              userIn.next();
             }
             temp=userIn.nextDouble();
             return temp;
        }
    
        private static String scaleOne()        {
    
            Scanner userIn= new Scanner(System.in);
            System.out.print("What scale is your temp currently in?"+
                              "1: Fahrenheit" + 
                              "2: Celsius" + 
                              "3: Kelvin" + 
                              "Please enter 1, 2, or 3: ");
           int userChoice = userIn.nextInt();
            String scale="";
            switch(userChoice)
            {
                case 1:
                {scale= "Fahrenheit";
                break;}
                case 2:
                {scale="Celsius";
                break;}
                case 3:
                {scale="Kelvin";
                break;}
                default:
                {scale= "There was an error in your choice!";}
            }
            return scale;
        }
    
    
    
        private static String toScale(){
            Scanner userIn= new Scanner(System.in);
            System.out.print("What scale would you like to convert to?"+
                              "1: Fahrenheit" + 
                              "2: Celsius" + 
                              "3: Kelvin" + 
                              "Please enter 1, 2, or 3: ");
            String scale2="";
            int userChoice =  userIn.nextInt();
            switch(userChoice)
            {
                case 1:
                {scale2= "Fahrenheit";
                break;}
                case 2:
                {scale2="Celsius";
                break;}
                case 3:
                {scale2="Kelvin";
                break;}
                default:
                {scale2= "There was an error in your choice!";}
            }
            return scale2;
        }
    
        public static double convert(String scale1, String scale2)
        {       
            double converts;
    
            if(scale1.equalsIgnoreCase("Fahrenheit") && scale2.equalsIgnoreCase("Kelvin"))        
            {
             converts=(getTemp()+459.67)* 5/9;
            }
    
            else if(scale1.equalsIgnoreCase("Fahrenheit") && scale2.equalsIgnoreCase("Celsius"))
            {
             converts=(getTemp()-32)*5/9;
            }
    
            else if(scale1.equalsIgnoreCase("Celsius") && scale2.equalsIgnoreCase("Kelvin"))
            {
             converts=getTemp()+273.15;
            }
    
            else if(scale1.equalsIgnoreCase("Celsius") && scale2.equalsIgnoreCase("Fahrenheit"))
            {
             converts=getTemp()*9/5+32;
            }
    
            else if(scale1.equalsIgnoreCase("Kelvin") && scale1.equalsIgnoreCase("Fahrenheit"))
            {
             converts=getTemp()*9/5-459.67;
            }
    
            else
            { 
             converts=getTemp()-276.15;
            }
    
            return converts;
        }} 
    

答案 3 :(得分:1)

毕竟说完了,我完成了它,并且按照课堂要求正确运行。感谢所有的反馈!

    package tempconverter;
import java.util.Scanner;

public class TempConverter 
{
        static double temp1;
        static String scale1;
        static String scale2;
        static double conv;


    public static void main(String[] args) 
    {
        char cont='N';

        do
        {   menueOne();



            menueCatch();

            cont=contOption();




        }while(cont=='Y');
    }   
        private static void menueOne()
        {
            System.out.print("Please make a selection."
                               +"\n1: Begin"
                               +"\n2: Exit"
                               +"\n");
        }

        private static void menueCatch()
        {   
            Scanner userIn= new Scanner(System.in);
            int userChoice=userIn.nextInt();

            String input="";
            switch(userChoice)
            {

                case 1:
                {
                 temp1=getTemp();
                 scale1=scaleOne();
                 scale2=toScale();
                 conv=convert();
                 displayConversion();
                 break;
                }

                case 2:
                {

                 break;
                }
            }
        }

        private static double getTemp()
        {
             double temp;
             double currentTemp;
             Scanner userIn= new Scanner(System.in);
             System.out.print("Enter a temperature to convert as a digit(ex: 100, 32, 10, 0):  ");
             while(!userIn.hasNextDouble())
             {
              System.out.println("That is not a valid temperature!");
              System.out.print("Re-enter a temperature: ");
              userIn.next();
             }
             temp=userIn.nextDouble();



             return temp;
        }

        private static String scaleOne()
        {

            Scanner userIn= new Scanner(System.in);



            System.out.print("What scale is your temp currently in?"+
                              "\n1: Fahrenheit" + 
                              "\n2: Celsius" + 
                              "\n3: Kelvin" + 
                              "\nPlease enter 1, 2, or 3: ");

            int userChoice = userIn.nextInt();

            String scale="";

            switch(userChoice)
            {
                case 1:
                {scale= "Fahrenheit";
                break;}

                case 2:
                {scale="Celsius";
                break;}

                case 3:
                {scale="Kelvin";
                break;}

                default:
                {scale= "There was an error in your choice!";}
            }

            return scale;

        }



        private static String toScale()
        {

            Scanner userIn= new Scanner(System.in);



            System.out.print("What scale would you like to convert to?"+
                              "\n1: Fahrenheit" + 
                              "\n2: Celsius" + 
                              "\n3: Kelvin" + 
                              "\nPlease enter 1, 2, or 3: ");

            int userChoice = userIn.nextInt();

            String scale2="";

            switch(userChoice)
            {
                case 1:
                {scale2= "Fahrenheit";
                break;}

                case 2:
                {scale2="Celsius";
                break;}

                case 3:
                {scale2="Kelvin";
                break;}

                default:
                {scale2= "There was an error in your choice!";}
            }

            return scale2;

        }



        public static double convert()
        {

            double converts;

            if (scale1.equals("Fahrenheit") && scale2.equals("Kelvin")) {

            converts = (temp1 + 459.67) * (5.0 / 9.0);
        }

        else if (scale1.equals("Fahrenheit") && scale2.equals("Celsius")) {
            converts = (temp1 - 32) * (5.0 / 9.0);
        }

        else if (scale1.equals("Celsius") && scale2.equals("Kelvin")) {
            converts = temp1 + 273.15;
        }

        else if (scale1.equals("Celsius") && scale2.equals("Fahrenheit")) {
            converts = temp1 * (9.0 / 5.0) + 32;
        }

        else if (scale1.equals("Kelvin") && scale2.equals("Fahrenheit")) {
            converts = temp1 * (9.0 / 5.0) - 459.67;
        }

        else { // Kelvin to Celsius
            converts = temp1 - 276.15;
        }

        return converts;
        }

        private static void displayConversion()
        {
            System.out.print("Your final temp is: " +conv+' '+scale2+' ');
        }

         private static char contOption()
        {
            char answer;
        Scanner userIn=new Scanner(System.in);
        System.out.print("\nDo you wish to convert another temp?(Y/N): ");
        answer=userIn.next().toUpperCase().charAt(0);

        return answer;
        }    






        }