用Java输入一个Celsius到Fahrenheit的转换表

时间:2017-10-16 22:37:41

标签: java loops

不可否认,再次成为Java的新手。在线上课,要求从Celsius到Fahrenheit的转换表,用户输入询问当前的摄氏温度。该类要求我们使用一个循环(没有指定哪个),经过一些试验和错误以及谷歌搜索后我感觉我已经接近让这个工作,但我觉得x++参数是拧紧转换输出。它不是随意吐出,而是连续添加1。

System.out.println("Enter a City : ");
   Scanner input = new Scanner(System.in);
   String city = input.nextLine();

    //Initiates a string that contains the celsius degree Unicode So i can call it easily.
    final String DEGREE  = "\u00b0";
    double celsius, fahrenheit;

    System.out.println("Enter your temperature in Celsius format for: " + city + " " );
    celsius = input.nextInt();
    fahrenheit = 32 + (celsius * 9 / 5);

    System.out.println("Celsius\tFahrenheit");
    System.out.println("====================");

    for (int x = 0; x <= 40; x++) {

    System.out.print(x + celsius);
    System.out.print("\t\t");
    System.out.print(x + fahrenheit);
    System.out.println();

1 个答案:

答案 0 :(得分:0)

这样的事情会起作用:

System.out.println("Enter a City : ");
Scanner input = new Scanner(System.in);
String city = input.nextLine();

//Initiates a string that contains the celsius degree Unicode So i can call it easily.
final String DEGREE  = "\u00b0";
double celsius, fahrenheit;

System.out.println("Enter your temperature in Celsius format for: " + city + " " );
celsius = input.nextInt();

System.out.println("Celsius\tFahrenheit");
System.out.println("====================");

for (int x = 0; x <= 40; x++) {
    System.out.print(celcius + x);
    System.out.print("\t\t");
    System.out.print(32 + ((celsius + x) * 9 / 5));
    System.out.println();
}