加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何将循环增加10而不是1?

发布时间:2020-12-15 04:30:06 所属栏目:Java 来源:网络整理
导读:这是我现在的代码: 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
这是我现在的代码: 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("Fahrenheitt Celsius"); 
        while (F <= 100)                    
        {
            // Display the F and C in increments of 10.
            System.out.println(F + "tt" + 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 + "tt" + convertFtoC(F));
        // Increment for the next day.
        F=F+10;
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读