Remaster_Expert
Remaster_Expert

Reputation: 53

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

Upvotes: 0

Views: 65

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79075

You need to change the case to case 1 and case 2 instead of case '1' and case '2'.

Note that '1' and '2' are char literals; not the int literals. the ASCII value of '1' is 49 and that of '2' is 50.

Upvotes: 4

Related Questions