Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 换算表_Java_Eclipse - Fatal编程技术网

Java 换算表

Java 换算表,java,eclipse,Java,Eclipse,我的任务是创建一个转换表,我不明白为什么一边的输出在另一边完成循环时重复。有人能帮忙吗?下面是我的代码: public class Conversion { public static double celsiusToFahrenheit(double celsius) { // make conversion double f = (celsius * 9 / 5) + 32; return f; } public

我的任务是创建一个转换表,我不明白为什么一边的输出在另一边完成循环时重复。有人能帮忙吗?下面是我的代码:

public class Conversion {

    public static double celsiusToFahrenheit(double celsius) {
        // make conversion
        double f = (celsius * 9 / 5) + 32;
        return f;

    }

    public static double fahrenheitToCelsius(double fahrenheit) {
        // make conversion
        double c = (fahrenheit - 32.0) * 5 / 9.0;
        return c;

    }

    public static void main(String args[]) {

        double c;
        double f;
        //Display table
        System.out.println("Celsius \t Fahrenheit \t | \tFahrenheit \t Celsius");

        //When i = 40, if i is greater than, or equal to 31, decriment until false
        for (double i = 40; i >= 31; i--) {
            c = i;

            //When j = 120, if j is greater than, or equal to 30, decriment by 10 until false
            for (double j = 120; j >= 30; j -= 10) {
                f = j;

                //Show result
                System.out.println(c + "\t\t " + (Math.round(celsiusToFahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (Math.round(fahrenheitToCelsius(f) * 100.0) / 100.0));

            }
        }
    }
}

您的输出与您描述的一样,因为每次执行用于将摄氏度转换为摄氏度的内部for循环1,以及用于将摄氏度覆盖到华氏度的外部for循环1,都会执行10次

您的输出与您描述的一样,因为每次执行用于将华氏温度转换为摄氏温度的内部for循环1,以及用于将摄氏温度覆盖到华氏温度的外部for循环1,都会执行10次

这是因为您引入了嵌套循环

for (double i = 40; i >= 31; i--) {
    c = i;

    //When j = 120, if j is greater than, or equal to 30, decriment by 10 until false
    for (double j = 120; j >= 30; j -= 10) {
        f = j;

        //Show result
        System.out.println(c + "\t\t " + (Math.round(celsiusToFahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (Math.round(fahrenheitToCelsius(f) * 100.0) / 100.0));

    }
}
想象一个双圈,就像手表的指针一样。内环比外环移动得快,就像分针比时针移动得快一样。这意味着,对于摄氏循环中的每一次迭代,我们在华氏循环中有十次迭代,就像我们在看一个表面一样,在这种情况下,我们每1小时有60分钟

作为提示,您实际上可以在循环中声明多个变量以进行迭代。您需要调整循环条件的边界,以便获得所需的结果,但这只是一个开始

//When i = 40, if i is greater than, or equal to 31, decriment until false
//When j = 120, if j is greater than, or equal to 30, decriment by 10 until false
for (double i = 40, j = 120; i >= 31 && j >= 30; i--, j -= 10) {
    c = i;
    f = j;
    //Show result
    System.out.println(c + "\t\t " + (Math.round(celsiusToFahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (Math.round(fahrenheitToCelsius(f) * 100.0) / 100.0));
}

这是因为您引入了嵌套循环

for (double i = 40; i >= 31; i--) {
    c = i;

    //When j = 120, if j is greater than, or equal to 30, decriment by 10 until false
    for (double j = 120; j >= 30; j -= 10) {
        f = j;

        //Show result
        System.out.println(c + "\t\t " + (Math.round(celsiusToFahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (Math.round(fahrenheitToCelsius(f) * 100.0) / 100.0));

    }
}
想象一个双圈,就像手表的指针一样。内环比外环移动得快,就像分针比时针移动得快一样。这意味着,对于摄氏循环中的每一次迭代,我们在华氏循环中有十次迭代,就像我们在看一个表面一样,在这种情况下,我们每1小时有60分钟

作为提示,您实际上可以在循环中声明多个变量以进行迭代。您需要调整循环条件的边界,以便获得所需的结果,但这只是一个开始

//When i = 40, if i is greater than, or equal to 31, decriment until false
//When j = 120, if j is greater than, or equal to 30, decriment by 10 until false
for (double i = 40, j = 120; i >= 31 && j >= 30; i--, j -= 10) {
    c = i;
    f = j;
    //Show result
    System.out.println(c + "\t\t " + (Math.round(celsiusToFahrenheit(c) * 10.0) / 10.0) + "\t\t |\t" + f + "\t\t" + (Math.round(fahrenheitToCelsius(f) * 100.0) / 100.0));
}

你能附上你的输出吗?这会使事情简单化。你能附上你的输出吗?这会简化事情。Makoto,非常感谢你的帮助;我将在以后创建循环时使用此建议,这是一个我没有想到的伟大技巧,非常感谢您的帮助;我将在以后创建循环时使用这个建议,这是一个我没有想到的好技巧