Java华氏到摄氏循环(带方法)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20022018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:24:02  来源:igfitidea点击:

Java Fahrenheit to Celsius loop (with methods)

javafor-loopconvertertemperature

提问by novicecodewriter

I need to convert Fahrenheit to Celsius in a Java program with methods, a forloop statement, and no user input. It is supposed to display Fahrenheit temperatures 0 through 20 and it's Celsius conversions. Any solutions?

我需要在带有方法、for循环语句且没有用户输入的 Java 程序中将华氏温度转换为摄氏度。它应该显示 0 到 20 的华氏温度和摄氏转换。任何解决方案?

import java.util.Scanner;

public class celsiusTempTable
{
public static void main(String[] args)
{
      System.out.println("Fahrenheit to Celsius Conversion Table");
    double tempC = celsiusConversion(tempC);
      int tempF = fahrenheit(tempF);
    displayData(tempF, tempC);
}
    public static int fahrenheit(int F)
    {
       for(F = 0; F <= 20; F++)
       {
          return F;
       }
    }
    public static double celsiusConversion(double C)
{
        Scanner input = new Scanner(System.in);
        for(int F = 0; F <= 20; F++)
    {
    C = (5.0/9.0) * (F - 32);
    return C;
    }
}
public static void displayData(int F, double C)
{
        for(F = 0; F <= 20; F++)
    {
      System.out.println("\nThe temperature in Fahrenheit is: " + F);
      System.out.println("The temperature in Celsius is: " + C);
        }
}

}

回答by JB Nizet

I'll only give hints.

我只会给出提示。

  1. Without user input. So forget about using a Scanner and System.in.
  2. You need to understand what method arguments and method return values are. Arguments are usually the inputs of a method. And the return value is the output of the method. You're being asked to translate a temperature in fahrenheit degrees to a temperature in celsius degrees. This is a perfect situation where a method is useful. The input of the translation method is thus a unique integer value (the temperature in fahrenheit degrees), and the output is another single integer value (the temperature in celsius degrees).
  3. You must do that 21 times. Once with 0 as input, once with 1 as input, etc. until 20. This means you need a loop, and that at each iteration, you will translate the current temperature (0, 1, 2, etc.) into celsius degrees by calling the translation method, and print the result. Read your text book about for loops. This part should be in the main method.
  1. 无需用户输入。所以忘记使用扫描仪和System.in.
  2. 您需要了解什么是方法参数和方法返回值。参数通常是方法的输入。返回值是方法的输出。您被要求将华氏度的温度转换为摄氏度的温度。这是一种方法有用的完美情况。因此,转换方法的输入是一个唯一的整数值(以华氏度为单位的温度),而输出是另一个单一的整数值(以摄氏度为单位的温度)。
  3. 你必须这样做21次。一次以 0 作为输入,一次以 1 作为输入,依此类推,直到 20。这意味着您需要一个循环,并且在每次迭代时,您会将当前温度(0、1、2 等)转换为摄氏度调用翻译方法,并打印结果。阅读有关 for 循环的教科书。这部分应该在main方法中。