我被这个计算器程序困住了,我得不到正确的答案。

huangapple 未分类评论53阅读模式
标题翻译

I'm stuck with this calculator program, I'm not getting the right answer

问题

这是你要的翻译内容:

  1. 我的编程课程要求我实现这样的结果:
  2. - 输入第一个数字!
  3. - *9*
  4. - 输入第二个数字!
  5. - *5*
  6. - 9 + 5 = 14
  7. - 9 - 5 = 4
  8. - 9 * 5 = 45
  9. - 9 / 5 = **1.8**,这是个问题,我编写的程序只给我1.0作为答案。我如何让这个数变成1.8而不是1.0
  10. import java.util.Scanner;
  11. public class Nelilaskin {
  12. public static void main(String[] args) {
  13. Scanner reader = new Scanner(System.in);
  14. System.out.println("输入第一个数字!");
  15. int first = Integer.valueOf(reader.nextLine());
  16. System.out.println("输入第二个数字!");
  17. int second = Integer.valueOf(reader.nextLine());
  18. int plus = (first + second);
  19. int minus = (first - second);
  20. int multi = (first * second);
  21. double division = (first / (double) second);
  22. System.out.println(first + " + " + second + " = " + plus);
  23. System.out.println(first + " - " + second + " = " + minus);
  24. System.out.println(first + " * " + second + " = " + multi);
  25. System.out.println(first + " / " + second + " = " + division);
  26. }
  27. }
英文翻译

My programming course wants me to have this kind of endcome:

  • Enter the first number!
  • 9
  • Enter the Second number!
  • 5
  • 9 + 5 = 14
  • 9 - 5 = 4
  • 9 * 5 = 45
  • 9 / 5 = 1.8 and this is the problem, the program I've written only gives me 1.0 as an answer. How can I get this number to be 1.8 not 1.0?
  1. public class Nelilaskin {
  2. public static void main(String[] args) {
  3. Scanner reader = new Scanner(System.in);
  4. System.out.println("Enter the first number!");
  5. int first = Integer.valueOf(reader.nextLine());
  6. System.out.println("Enter the second number!");
  7. int second = Integer.valueOf(reader.nextLine());
  8. int plus = (first + second);
  9. int minus = (first - second);
  10. int multi = (first * second);
  11. double division = (first / second * 1.0);
  12. System.out.println(first + " + " + second + " = " + plus);
  13. System.out.println(first + " - " + second + " = " + minus);
  14. System.out.println(first + " * " + second + " = " + multi);
  15. System.out.println(first + " / " + second + " = " + division);
  16. }
  17. }

答案1

得分: 2

请考虑将first和second的数据类型更改为Float
然后也将结果存储在一个float变量中,那么输出将会符合要求。

  1. float plus = (first + second);
  2. float minus = (first - second);
  3. float multi = (first * second);
  4. float division = first / second;
英文翻译

Consider replacing the data type for first and second as Float.
And store the resultant in a float variable as well, then the output would be as required.

  1. float plus = (first + second);
  2. float minus = (first - second);
  3. float multi = (first * second);
  4. float division = first / second;

答案2

得分: 1

这是因为你正在对两个整数值进行除法运算,尝试将它们中至少一个转换为双精度类型(double)。

  1. double division = (double)first / (double)second ;
英文翻译

this is because you are dividing two int values, try to cast at least one of them to double..

  1. double division = (double)first / (double)second ;

huangapple
  • 本文由 发表于 2020年3月16日 19:40:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/60705315.html
匿名

发表评论

匿名网友

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

确定