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

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

An algebra program in java is not working correctly

问题

import java.util.*;

class F {
    public static void main(String a[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("执行代数程序:要键入的编号:");
        System.out.println(" (i) 3X+4Y+Z                         1 ");
        System.out.println(" (ii) 3A^2+4B^3+3C                   2 ");
        int ch = in.nextInt();
        switch (ch) {
            // Program 1
            case 1:
                Scanner sc = new Scanner(System.in);
                System.out.println("输入 X 的数值...");
                int X = sc.nextInt();
                System.out.println("输入 Y 的数值...");
                int Y = sc.nextInt();
                System.out.println("输入 Z 的数值...");
                int Z = sc.nextInt();

                int sum = (3 * X) + (4 * Y) + Z;

                System.out.println(" ");
                System.out.println("X 的数值为:" + X);
                System.out.println("Y 的数值为:" + Y);
                System.out.println("Z 的数值为:" + Z);
                System.out.println("3X+4Y+Z 的值为:" + sum);
                break;

            // Program 2
            case 2:
                Scanner hi = new Scanner(System.in);
                System.out.println("输入 A 的数值...");
                double A = hi.nextDouble();
                System.out.println("输入 B 的数值...");
                double B = hi.nextDouble();
                System.out.println("输入 C 的数值...");
                double C = hi.nextDouble();

                double pro = 3 * Math.pow(A, 2) + 4 * Math.pow(B, 3) + 3 * C;
                int isum = (int) pro;

                System.out.println(" ");
                System.out.println("A 的数值为:" + A);
                System.out.println("B 的数值为:" + B);
                System.out.println("C 的数值为:" + C);
                System.out.println("3A^2+4B^3+3C 的值为:" + isum);
                break;

            // Else
            default:
                System.out.println("错误的选择");
        }
    }
}
英文:

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:

/** WAP using switch case to print the following :
1. 3X+4Y+Z 
2. 3A^2+4B^3+3C*/
import java.util.*;
class F
{
public static void main(String a[])
{
Scanner in = new Scanner(System.in);
System.out.println("Algebra Program executed :                     No. to be typed: ");
System.out.println(" (i) 3X+4Y+Z                                                 1 ");
System.out.println(" (ii) 3A^2+4B^3+3C                                           2 ");
int ch = in.nextInt();
switch(ch)
{
//Program 1
case '1': Scanner sc = new Scanner(System.in);
System.out.println("Type a number for X...");
int X = sc.nextInt();
System.out.println("Type a number for Y...");
int Y = sc.nextInt();
System.out.println("Type a number for Z...");
int Z = sc.nextInt();
int sum = (3*X)+(4*Y)+Z;
System.out.println(" ");
System.out.println("Value of X is "+X);
System.out.println("Value of Y is "+Y);
System.out.println("Value of Z is "+Z);
System.out.println("Value of 3X+4Y+Z is "+sum);
break;
//Program 2
case '2': Scanner hi = new Scanner(System.in);
System.out.println("Type a number for A...");
double A = hi.nextDouble();
System.out.println("Type a number for B...");
double B = hi.nextDouble();
System.out.println("Type a number for C...");
double C = hi.nextDouble();
double pro = 3*Math.pow(A, 2) + 4*Math.pow(B, 3) + 3*C;
int isum = (int) pro;
System.out.println(" ");
System.out.println("Value of A is "+A);
System.out.println("Value of B is "+B);
System.out.println("Value of C is "+C);
System.out.println("Value of 3A^2+4B^3+3C is "+isum);
break;
//Else
default: System.out.println("Wrong Choice");
}
}
}```
</details>
# 答案1
**得分**: 4
你需要将情况更改为 `case 1` 和 `case 2`,而不是 `case &#39;1&#39;` 和 `case &#39;2&#39;`。
请注意,`&#39;1&#39;` 和 `&#39;2&#39;` 是 `char` 字面值;不是 `int` 字面值。`&#39;1&#39;` 的 ASCII 值是 `49`,`&#39;2&#39;` 的 ASCII 值是 `50`。
<details>
<summary>英文:</summary>
You need to change the case to `case 1` and `case 2` instead of  `case &#39;1&#39;` and `case &#39;2&#39;`. 
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`.
</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:

确定