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

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

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

问题

  1. import java.util.Scanner;
  2. public class FHeightToCelsius {
  3. public static void main(String[] args)
  4. //计算输入的华氏温度值对应的摄氏温度
  5. //使用公式 C=(F-32)*5/9
  6. {
  7. final int BASE = -32;
  8. final double CONVERSION_FACTOR = 5.0/9.0; // 请注意:这里需要使用浮点数除法
  9. Double message;
  10. Scanner scan = new Scanner(System.in);
  11. System.out.println("输入华氏温度:"); // 修改为中文提示
  12. message = scan.nextDouble();
  13. double CelsiusTemp;
  14. CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;
  15. System.out.println("转换后的摄氏温度是:" + CelsiusTemp); // 修改为中文提示
  16. }
  17. }

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

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

英文:
  1. import java.util.Scanner;
  2. public class FHeightToCelsius {
  3. public static void main(String[] args)
  4. //Computes the Celsius equivalent of the inputted Fahrenheit value
  5. //using the formula C=(F-32)*5/9
  6. {
  7. final int BASE = -32;
  8. final double CONVERSION_FACTOR = 5/9;
  9. Double message;
  10. Scanner scan = new Scanner(System.in);
  11. System.out.println("Enter A Fahrenheit:");
  12. message = scan.nextDouble();
  13. double CelsiusTemp;
  14. CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;
  15. System.out.println("Your conversion is:" + CelsiusTemp);
  16. }
  17. }

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类型的变量。
我建议进行以下更改:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args)
  4. //使用公式C=(F-32)*5/9计算输入的华氏温度值的摄氏温度等价值
  5. {
  6. final int BASE = -32;
  7. //如果你使用变量double,应该除以5.0 / 9
  8. final double CONVERSION_FACTOR = 5.0 / 9;
  9. Scanner scan = new Scanner(System.in);
  10. System.out.println("输入华氏温度:");
  11. //为了避免我上面描述的问题,请使用nextInt()方法而不是nextLine()
  12. int message = scan.nextInt();
  13. double CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;
  14. //我用转换后的值替换了"message"
  15. System.out.println("你的转换结果是:" + CelsiusTemp);
  16. }
  17. }

我希望我有所帮助。

英文:

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:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args)
  4. //Computes the Celsius equivalent of the inputted Fahrenheit value
  5. //using the formula C=(F-32)*5/9
  6. {
  7. final int BASE = -32;
  8. // If you use the variable double you should divide 5.0 / 9
  9. final double CONVERSION_FACTOR = 5.0 /9;
  10. Scanner scan = new Scanner(System.in);
  11. System.out.println("Enter A Fahrenheit:");
  12. // to avoid the problem I described above use the nextInt() method instead of nextLine()
  13. int message = scan.nextInt();
  14. double CelsiusTemp = (message + BASE) * CONVERSION_FACTOR;
  15. // I replaced "message" with the converted value
  16. System.out.println("Your conversion is:" + CelsiusTemp);
  17. }
  18. }

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:

确定