Conversion of Fahrenheit to Celsius and taking input from the user to modify

huangapple go评论67阅读模式
英文:

Conversion of Fahrenheit to Celsius and taking input from the user to modify

问题

import java.util.Scanner;

public class FHeightToCelsius {

    public static void main(String[] args)
    //计算输入的华氏温度值对应的摄氏温度
    //使用公式 C=(F-32)*5/9
    {
        final int BASE = -32;
        final double CONVERSION_FACTOR = 5.0/9.0; // 请注意:这里需要使用浮点数除法

        Double message;
        Scanner scan = new Scanner(System.in);
        System.out.println("输入华氏温度:"); // 修改为中文提示
        message = scan.nextDouble();

        double CelsiusTemp;

        CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;

        System.out.println("转换后的摄氏温度是:" + CelsiusTemp); // 修改为中文提示
    }
}

数学不是我的强项,问题解决也不是(但我会努力的!)我还需要帮助理解和完善这个公式是否正确,以及我们如何在代码中应用它。我在网上查找了这个公式,但不确定我是否正确地在代码中转换了它。

我无法让数学公式正常工作。

英文:
import java.util.Scanner;

public class FHeightToCelsius {

    public static void main(String[] args)
    //Computes the Celsius equivalent of the inputted Fahrenheit value
    //using the formula C=(F-32)*5/9
    {
        final int BASE = -32;
        final double CONVERSION_FACTOR = 5/9;

        Double message;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter A Fahrenheit:");
        message = scan.nextDouble();

        double CelsiusTemp;

        CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;

        System.out.println("Your conversion is:" + CelsiusTemp);

    }
}

Math isn't my strong suit, nor is problem solving (YET!!)
What I also need help with is fleshing out and understanding if this formula is right or correct and how we come about that. I looked up the formula online, but, I'm not sure if I converted it right in code.

I cannot get the math formula to work.

答案1

得分: 1

错误消息已经说得很清楚了。您正试图对字符串执行数学操作,这是不允许的。因此,请将您的字符串转换为数字。

英文:

The error message says it all. You're trying to perform mathematical operations on strings, that's not allowed. So turn your strings into numbers.

答案2

得分: 1

I'm new to Java, but I probably know the solution to your problem.
问题是你试图将int添加到String类型的变量。
我建议进行以下更改:

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    //使用公式C=(F-32)*5/9计算输入的华氏温度值的摄氏温度等价值
    {
        final int BASE = -32;
        //如果你使用变量double,应该除以5.0 / 9
        final double CONVERSION_FACTOR = 5.0 / 9;

        Scanner scan = new Scanner(System.in);
        System.out.println("输入华氏温度:");
        //为了避免我上面描述的问题,请使用nextInt()方法而不是nextLine()
        int message = scan.nextInt();

        double CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;
        
        //我用转换后的值替换了"message"
        System.out.println("你的转换结果是:" + CelsiusTemp);
    }
}

我希望我有所帮助。

英文:

I'm new to Java, but I probably know the solution to your problem.

The problem is that you are trying to add an int to a String type variable.

I suggest the following changes:

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    //Computes the Celsius equivalent of the inputted Fahrenheit value
    //using the formula C=(F-32)*5/9
    {
        final int BASE = -32;
        // If you use the variable double you should divide 5.0 / 9
        final double CONVERSION_FACTOR = 5.0 /9;

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter A Fahrenheit:");
        // to avoid the problem I described above use the nextInt() method instead of nextLine()
        int message = scan.nextInt();

        double CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;
        
        // I replaced "message" with the converted value
        System.out.println("Your conversion is:" + CelsiusTemp);
    }
}

I hope I helped.

huangapple
  • 本文由 发表于 2023年6月30日 02:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定