Jump to content

Need Help With Program For Programming Class In School

SuperShermanTanker

So I'm learning how to program in java and I'm trying to figure out what the code part is for math also the output of the program is not coming out correctly it's outputting all the imputs instead of one when I imput a selection to select which math thing I want to do

 

here's what I have programmed so far it works but I'm stuck on making actually do what I want it to do and that is doing math

 

 

import java.util.*;
public class Converter {
    
    public static void main(String[] args) {
            
        Scanner input = new Scanner (System.in);
        System.out.println ("Please Enter a number");
        System.out.println ("1 = Addition");
        System.out.println ("2 = Subtraction");
        System.out.println ("3 = Multiplication");
        System.out.println ("4 = Division");
        System.out.println ("5 = Exit");
        int choice = input.nextInt();

        switch (choice) {
            
            case 1:
            {
                System.out.println ("Input Numbers");
            }
            case 2:
            {
                System.out.println ("Input Numbers");
            }
            case 3:
            {
                System.out.println ("Input Numbers");
            }
            case 4:
            {
                System.out.println ("Input Numbers");
            }
            case 5:
            {
                System.out.println ("DONE");
            }
            
        }
    }
}


    }
}

Link to comment
Share on other sites

Link to post
Share on other sites

40 minutes ago, SuperShermanTanker said:

 

you need to add break; under cases

 

switch()
{
	case 1:
		//code
		break;
	case 2:
		//cdpe
		break;
	default:
		//this is incase the choice is not above
		break;
}

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

import java.util.*;
public class Converter {
	public static void main(String[] args) {
		Scanner input = new Scanner (System.in);
		System.out.println ("Please Enter a number");
		System.out.println ("1 = Addition");
		System.out.println ("2 = Subtraction");
		System.out.println ("3 = Multiplication");
		System.out.println ("4 = Division");
		System.out.println ("5 = Exit");
		int choice = input.nextInt();
		
		if(choice >= 1 && choice <= 4) {
			System.out.println("Input numbers");
		} elseif(choice == 5) {
			System.out.println("DONE");
		} else {
			System.out.println("Invalid choice");
		}

		while(input1 == null) {
			input1 == input.nextDouble();
		}

		while(input2 == null) {
			input2 == input.nextDouble();
		}

		switch (choice) {
			case 1:
			{
				System.out.println (input1 + input2);
				break;
			}
			case 2:
			{
				System.out.println (input1 - input2);
				break;
			}
			case 3:
			{
				System.out.println (input1 * input2);
				break;
			}
			case 4:
			{
				System.out.println (input1 / input2);
				break;
			}
		}
	}
}

Hi, here's my try at it. I haven't coded in Java for a while, so it may not be the best and it may not work, but I did try to remove redundant code and allowed for doubles to be entered.

˙ǝɯᴉʇ ɹnoʎ ƃuᴉʇsɐʍ ǝɹɐ noʎ 'sᴉɥʇ pɐǝɹ oʇ ƃuᴉʎɹʇ ǝɹɐ noʎ ɟI

Link to comment
Share on other sites

Link to post
Share on other sites

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Choose a math function");
        System.out.println("1 = Addition");
        System.out.println("2 = Subtraction");
        System.out.println("3 = Multiplication");
        System.out.println("4 = Division");
        System.out.println("5 = Exit");
        int choice = input.nextInt();
        
        System.out.println("Input number 1");
        int num1 = input.nextInt();
        System.out.println("Input number 2");
        int num2 = input.nextInt();

        switch (choice) {

            case 1: {
                System.out.println(num1 + num2);
                break;
            }
            case 2: {
                System.out.println(num1 - num2);
                break;
            }
            case 3: {
                System.out.println(num1 * num2);
                break;
            }
            case 4: {
                System.out.println(num1 / num2);
                break;
            }
            case 5: {
                System.out.println("Goodbye");
                break;
            }

        }
    }

That kind of the functionality you're looking for?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×