Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question simple java calculator Help!

    im just learning java and im trying to program a multipurpose calculator but i keep running into brick walls. Heres the coding:

    Code:
    import java.util.Scanner;
    
    public class dinoremix2{
    
    public static void main (String[] args){
    	boolean play = false;
    		String playAgain;
    		do{
    		
    			String math;
    			System.out.println(" Hello! Just type what you want to do! add , multiply , subtract , divide , or quadratic (please dont use caps) ");
    			String math = scan.nextString;
    					
    		
    		if (math = add);{
    			do{
    				Scanner in = new Scanner(System.in);
    				Scanner scan = new Scanner(System.in);
    				System.out.println(" Please Enter Your First Number: ");
    				int num1 = dinoremix.nextInt();
    				System.out.println(" Thanks! Please Enter The Second Number: ");
    				int num2 = dinoremix.nextInt();
    				int answer = num1 + num2;
    				System.out.println(" Your answer is: " + answer );
    			
    		if (math = subtract);{
    			do{
    				Scanner in = new Scanner(System.in);
    				Scanner scan = new Scanner(System.in);
    				System.out.println(" Please Enter Your First Number: ");
    				int num1 = dinoremix.nextInt();
    				System.out.println(" Thanks! Please Enter The Second Number: ");
    				int num2 = dinoremix.nextInt();
    				int answer = num1 - num2;
    				System.out.println(" Your answer is: " + answer );
    			
    			
    		if (math = divide);{
    			do{
    				Scanner in = new Scanner(System.in);
    				Scanner scan = new Scanner(System.in);
    				System.out.println(" Please Enter Your First Number: ");
    				int num1 = dinoremix.nextInt();
    				System.out.println(" Thanks! Please Enter The Second Number: ");
    				int num2 = dinoremix.nextInt();
    				int answer = num1 / num2;
    				System.out.println(" Your answer is: " + answer );
    			
    			if (math = multiply);{
    				do{
    					Scanner in = new Scanner(System.in);
    					Scanner scan = new Scanner(System.in);
    					System.out.println(" Please Enter Your First Number: ");
    					int num1 = dinoremix.nextInt();
    					System.out.println(" Thanks! Please Enter The Second Number: ");
    					int num2 = dinoremix.nextInt();
    					int answer = num1 * num2;
    					System.out.println(" Your answer is: " * answer );
    			
    			if (math = quadratic);{
    				do{
    					Scanner in = new Scanner(System.in);
    			Scanner scan = new Scanner(System.in);
    			System.out.println(" Please Enter a: ");
    			double a = scan.nextDouble();
    			System.out.println(" Please Enter b: ");
    			double b = scan.nextDouble();
    			System.out.println(" Please Enter c: ");
    			double c = scan.nextDouble();
    			double disc = (b*b)-(4*a*c);
    			boolean isImag = false;
    			if (disc == 0){
    				double fac = (b * -1) / (2 * a);
    				System.out.println("heres the root" + fac);
    			}
    				if(disc > 0) {
    					isImag = false;
    
    			}
    				if (disc < 0){
    					isImag = true;
    						disc = disc * -1;
    			}
    							double fac1 = ((b * -1) + Math.sqrt(disc))/(2*a);
    							double fac2 = ((b * -1) - Math.sqrt(disc))/(2*a);
    				if (isImag){
    					System.out.println(fac1 + "i");
    					System.out.println(fac2 + "i");
    			}else{
    					System.out.println(fac1 + "i");
    					System.out.println(fac2 + "i"); }
    
    		System.out.println("Try again? ");
    		playAgain = in.nextLine();
    		if(playAgain.equals("yes")){
    		play = true;
    		}
    		if(playAgain.equals("no")){
    		play = false;
    		}
                    }
    }
    im missing a set of closing brackets somewhere but i cant find where

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    another brick wall: posting in the wrong branch.

    this is javascript. you're looking for java

  • Users who have thanked xelawho for this post:

    Microsuck (02-18-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,611
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    You are missing *MANY* closing braces.

    Why in the world are you using do, at all?

    You also have a fatal error in your if usage:
    Code:
        if (math = add);
    That semicolon there means the condition after the if is NOW COMPLETE.

    The code after the semicolon will thus be executed no matter if the if condition is true or false.

    KEEP IT SIMPLE!
    Code:
    		if (math = add)
    		{
    				Scanner in = new Scanner(System.in);
    				Scanner scan = new Scanner(System.in);
    				System.out.println(" Please Enter Your First Number: ");
    				int num1 = dinoremix.nextInt();
    				System.out.println(" Thanks! Please Enter The Second Number: ");
    				int num2 = dinoremix.nextInt();
    				int answer = num1 + num2;
    				System.out.println(" Your answer is: " + answer );
    		}			
    		else if (math = subtract)
    		{
    				...
    Indenting your code properly would also help you find the missing braces. I counted AT LEAST TEN missing right braces.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •