一个Java的代数程序无法正常运行。

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

An algebra program in java is not working correctly

问题

  1. import java.util.*;
  2. class F {
  3. public static void main(String a[]) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.println("执行代数程序:要键入的编号:");
  6. System.out.println(" (i) 3X+4Y+Z 1 ");
  7. System.out.println(" (ii) 3A^2+4B^3+3C 2 ");
  8. int ch = in.nextInt();
  9. switch (ch) {
  10. // Program 1
  11. case 1:
  12. Scanner sc = new Scanner(System.in);
  13. System.out.println("输入 X 的数值...");
  14. int X = sc.nextInt();
  15. System.out.println("输入 Y 的数值...");
  16. int Y = sc.nextInt();
  17. System.out.println("输入 Z 的数值...");
  18. int Z = sc.nextInt();
  19. int sum = (3 * X) + (4 * Y) + Z;
  20. System.out.println(" ");
  21. System.out.println("X 的数值为:" + X);
  22. System.out.println("Y 的数值为:" + Y);
  23. System.out.println("Z 的数值为:" + Z);
  24. System.out.println("3X+4Y+Z 的值为:" + sum);
  25. break;
  26. // Program 2
  27. case 2:
  28. Scanner hi = new Scanner(System.in);
  29. System.out.println("输入 A 的数值...");
  30. double A = hi.nextDouble();
  31. System.out.println("输入 B 的数值...");
  32. double B = hi.nextDouble();
  33. System.out.println("输入 C 的数值...");
  34. double C = hi.nextDouble();
  35. double pro = 3 * Math.pow(A, 2) + 4 * Math.pow(B, 3) + 3 * C;
  36. int isum = (int) pro;
  37. System.out.println(" ");
  38. System.out.println("A 的数值为:" + A);
  39. System.out.println("B 的数值为:" + B);
  40. System.out.println("C 的数值为:" + C);
  41. System.out.println("3A^2+4B^3+3C 的值为:" + isum);
  42. break;
  43. // Else
  44. default:
  45. System.out.println("错误的选择");
  46. }
  47. }
  48. }
英文:

I made a program which should display an equation after choosing a type of algebra equation which doesn't works for some reason.
I have tried changing the scanners name but it just displays Wrong choice when an expression is chosen.
I would really appreciate it if you can help me thank you.
This is the code:

  1. /** WAP using switch case to print the following :
  2. 1. 3X+4Y+Z
  3. 2. 3A^2+4B^3+3C*/
  4. import java.util.*;
  5. class F
  6. {
  7. public static void main(String a[])
  8. {
  9. Scanner in = new Scanner(System.in);
  10. System.out.println("Algebra Program executed : No. to be typed: ");
  11. System.out.println(" (i) 3X+4Y+Z 1 ");
  12. System.out.println(" (ii) 3A^2+4B^3+3C 2 ");
  13. int ch = in.nextInt();
  14. switch(ch)
  15. {
  16. //Program 1
  17. case '1': Scanner sc = new Scanner(System.in);
  18. System.out.println("Type a number for X...");
  19. int X = sc.nextInt();
  20. System.out.println("Type a number for Y...");
  21. int Y = sc.nextInt();
  22. System.out.println("Type a number for Z...");
  23. int Z = sc.nextInt();
  24. int sum = (3*X)+(4*Y)+Z;
  25. System.out.println(" ");
  26. System.out.println("Value of X is "+X);
  27. System.out.println("Value of Y is "+Y);
  28. System.out.println("Value of Z is "+Z);
  29. System.out.println("Value of 3X+4Y+Z is "+sum);
  30. break;
  31. //Program 2
  32. case '2': Scanner hi = new Scanner(System.in);
  33. System.out.println("Type a number for A...");
  34. double A = hi.nextDouble();
  35. System.out.println("Type a number for B...");
  36. double B = hi.nextDouble();
  37. System.out.println("Type a number for C...");
  38. double C = hi.nextDouble();
  39. double pro = 3*Math.pow(A, 2) + 4*Math.pow(B, 3) + 3*C;
  40. int isum = (int) pro;
  41. System.out.println(" ");
  42. System.out.println("Value of A is "+A);
  43. System.out.println("Value of B is "+B);
  44. System.out.println("Value of C is "+C);
  45. System.out.println("Value of 3A^2+4B^3+3C is "+isum);
  46. break;
  47. //Else
  48. default: System.out.println("Wrong Choice");
  49. }
  50. }
  51. }```
  52. </details>
  53. # 答案1
  54. **得分**: 4
  55. 你需要将情况更改为 `case 1` 和 `case 2`,而不是 `case &#39;1&#39;` 和 `case &#39;2&#39;`。
  56. 请注意,`&#39;1&#39;` 和 `&#39;2&#39;` 是 `char` 字面值;不是 `int` 字面值。`&#39;1&#39;` 的 ASCII 值是 `49`,`&#39;2&#39;` 的 ASCII 值是 `50`。
  57. <details>
  58. <summary>英文:</summary>
  59. You need to change the case to `case 1` and `case 2` instead of `case &#39;1&#39;` and `case &#39;2&#39;`.
  60. Note that `&#39;1&#39;` and `&#39;2&#39;` are `char` literals; not the `int` literals. the ASCII value of `&#39;1&#39;` is `49` and that of `&#39;2&#39;` is `50`.
  61. </details>

huangapple
  • 本文由 发表于 2020年9月6日 19:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63763569.html
匿名

发表评论

匿名网友

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

确定