第一次使用温度转换器的方法

时间:2016-03-08 04:54:43

标签: java

我第一次使用方法,我的程序主要工作,除了它不会循环转换语句与它对应的问题...用户输入对应转换语句,所以3次会提示

转换#1

...

转换#2

...

转换#3

...

farenheight到摄氏温度之间的转换工作但不是从摄氏温度到farenheight之间的转换,下面的任何见解都会对我的代码有帮助,

import java.util.Scanner;
import java.text.DecimalFormat;

    public class TempConverter {

        public static void main(String[] args){


            DecimalFormat df = new DecimalFormat("#,###.0");
            System.out.println("Temperature Converter");
            System.out.println("---------------------");
            System.out.println();
             Scanner input = new Scanner(System.in);

              System.out.print("How many conversions would you like to make: ");
              int conversions=input.nextInt();

              for(int i = 1; i < conversions; i++){
              System.out.println("Conversion # " + i++);
              System.out.println();
              System.out.println ("To convert from celsius to fahrenheit type 1 ");
              System.out.print ("To convert from fahrenheit to celsius type 2: ");
              int choice=input.nextInt();

                switch(choice){

                case 1:
                     System.out.println();
                     System.out.print ("Enter a celsius temperature: ");
                     double cels=input.nextDouble();
                     double result=celsiusToFahrenheit(choice,cels);
                     System.out.println();
                     System.out.println ("The conversion of "+cels+" from celcius to fahrenheit is "+df.format(result) );
                     break;
                case 2:
                     System.out.println();
                     System.out.print("Enter a farenheight temperature: ");
                     double fahr=input.nextDouble();
                     double result1=fahrenheitToCelsius(choice,fahr);
                     System.out.println ("The conversion of "+fahr+" from fahrenheit to celcius is "+df.format(result1) );

                }
              }
           }



            public static double celsiusToFahrenheit(int choice, double cels)
            {

             double converted=0.0;
              if (choice == 1)
              converted=9.0/5.0*cels+32;

              return converted;
              }

            public static double fahrenheitToCelsius(int choice, double fahr)
            {
                double converted2=0.0;
                if (choice ==2)
                   converted2=5.0/9.0*(fahr-32);

                   return converted2;
            }

            }

2 个答案:

答案 0 :(得分:1)

你有两个错误:

(1)这一行错了:

for(int i = 1; i < conversions; i++)

这意味着只要i < conversions循环。如果conversions为3,则表示它将与i==1i==2一起循环,但不会为i==3循环,因为3<3false }。使用<=

(2)上述i++语句中的for每次循环时都会递增i。这就是你想要的。但是,你通过在代码中添加另一个i++来打败它,这将使它增加一段额外的时间。

答案 1 :(得分:0)

循环代码错误,并且switch case2没有break。 在这里找到更正的代码:

import java.text.DecimalFormat;
import java.util.Scanner;

public class TempConverter {

    public static void main(String[] args) {

        DecimalFormat df = new DecimalFormat("#,###.0");
        System.out.println("Temperature Converter");
        System.out.println("---------------------");
        System.out.println();
        Scanner input = new Scanner(System.in);

        System.out.print("How many conversions would you like to make: ");
        int conversions = input.nextInt();

        for (int i = 1; i <= conversions; i++) {
            System.out.println("Conversion # " + i);
            System.out.println();
            System.out.println("To convert from celsius to fahrenheit type 1 ");
            System.out.print("To convert from fahrenheit to celsius type 2: ");
            int choice = input.nextInt();
            switch (choice) {
            case 1:
                System.out.println();
                System.out.print("Enter a celsius temperature: ");
                double cels = input.nextDouble();
                double result = celsiusToFahrenheit(choice, cels);
                System.out.println();
                System.out.println("The conversion of " + cels + " from celcius to fahrenheit is " + df.format(result));
                break;
            case 2:
                System.out.println();
                System.out.print("Enter a farenheight temperature: ");
                double fahr = input.nextDouble();
                double result1 = fahrenheitToCelsius(choice, fahr);
                System.out
                        .println("The conversion of " + fahr + " from fahrenheit to celcius is " + df.format(result1));
                break;
            }
        }
    }

    public static double celsiusToFahrenheit(int choice, double cels) {

        double converted = 0.0;
        if (choice == 1)
            converted = 9.0 / 5.0 * cels + 32;

        return converted;
    }

    public static double fahrenheitToCelsius(int choice, double fahr) {
        double converted2 = 0.0;
        if (choice == 2)
            converted2 = 5.0 / 9.0 * (fahr - 32);

        return converted2;
    }

}