简体   繁体   English

Java 中的代数程序无法正常工作

[英]An algebra program in java is not working correctly

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

You need to change the case to case 1 and case 2 instead of case '1' and case '2' .您需要将 case 更改为case 1case 2而不是case '1'case '2'

Note that '1' and '2' are char literals;请注意, '1''2'char文字; not the int literals.不是int文字。 the ASCII value of '1' is 49 and that of '2' is 50 . '1'的 ASCII 值是49'2'的 ASCII 值是50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM