java中的代数程序工作不正常

tyu7yeag  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(351)

我做了一个程序,它应该显示一个方程后,选择了一种代数方程,它不工作的一些原因。我尝试过更改扫描仪名称,但它只是显示错误的选择时,一个表达式被选中。如果你能帮我我会非常感激的谢谢。代码如下:

  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. }```
brvekthn

brvekthn1#

你得把案子改成 case 1 以及 case 2 而不是 case '1' 以及 case '2' .
请注意 '1' 以及 '2'char 文字;不是那个 int 文字。的ascii值 '1'49 那就是 '2'50 .

相关问题