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

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

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

/**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");
        }
    }
}```
brvekthn

brvekthn1#

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

相关问题