提示用户他们希望进行多少次转化

时间:2016-03-07 21:25:33

标签: java

编写包含以下两种方法的程序:

public static double celsiusToFahrenheit(double cels)             
public static double fahrenheitToCelsius(double fahr)

这两种方法都会返回转换后的温度。主要方法将提示用户他们想要进行的转换次数。使用该数字进行for循环的计数器控制。确保数字输出有一个小数位。

我有一半的转换程序,但我不知道如何询问用户他们想要多少次转换以及如何进行转换。

import java.util.Scanner;

public class TempConverter{
    public static void main(String[] args)
    {

       double fahrenheit;
       double celsius;

       Scanner input = new Scanner(System.in);

       System.out.println("Enter the Degrees in Fahrenheit");
       fahrenheit = input.nextDouble();
       celsius = (5.0/9.0)*(fahrenheit - 32);
       System.out.println("The number of degrees of Fahrenheit: " + fahrenheit);
       System.out.println("Converted to Celsius is: " + celsius);//end convert to celsius

       System.out.println("Enter the Degrees in Celsius");
       celsius = input.nextDouble();
       fahrenheit = (9.0/5.0)* celsius + 32;
       System.out.println("The number of degrees of Celsius: " + celsius);
       System.out.println("Converted to Fahrenheit is: " + fahrenheit);
    }

}

1 个答案:

答案 0 :(得分:0)

使用扫描仪输入次数。运行该数字的循环,并使用扫描仪为华氏温度和摄氏温度询问温度。

或者运行无限循环并要求使用扫描仪输入Fahrenhei和Celsius。如果用户输入END / QUIT,则结束循环并退出。

相关问题