java - 如何将循环递增 10 而不是 1?

标签 java loops

这是我现在的代码:http://ideone.com/PfXNQc

import java.util.Scanner;

public class Temperature
{

    public static double convertFtoC(double F)
    {
        return (F-32)*5/9;
    }

    public static void main(String[] args)
    {
        double F; //Stores Farenheit
        double C; //Stores Celsius

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a temperature in Fahrenheit: "); //Enter in Farenheit
        F = keyboard.nextDouble(); //Stores Farenheit

        System.out.println("The temperature in Celsius is: " + convertFtoC(F)); //Displays Celsius (call convertFtoC(F) where celcuis conversion needed)

        F = 0;

        System.out.println("Fahrenheit\t Celsius"); 
        while (F <= 100)                    
        {
            // Display the F and C in increments of 10.
            System.out.println(F + "\t\t" + convertFtoC(F));
            // Increment for the next day.
            F++;
        }       
    }
}

我希望转换循环以 10 而不是 1 的增量上升。

现在它正在输出这个:

incorrect output

但我希望它是

0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100

最佳答案

如果你这样写呢

while (F <= 90) //Last number before 100 is 90
{
        // Display the F and C in increments of 10.
        System.out.println(F + "\t\t" + convertFtoC(F));
        // Increment for the next day.
        F=F+10;
}

另外,为什么你对变量使用大写字母,按照惯例你应该以小写字母开头变量名,对象名以大写字母开头。

关于java - 如何将循环递增 10 而不是 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32835243/

相关文章:

java - 如何使用 JAXB Marshaller 编码 java.lang.Exception?

javascript - 循环数组和另一个对象中的对象

JavaScript/jQuery : Divs not changing background color on loop

c - 如何创建一个循环来连续获取浮点输入?

java - 在 Jackson/Jaxb 中打开一个元素

java - 防止对话框离开屏幕

java - 将文件大小格式化为 MB、GB 等

Java:解析 RSS 提要时出错

php - 在php中的foreach循环中获取特定记录

c++ - 对我的 do-while 循环程序感到困惑