Java woes

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

Post Reply
Nicholai
Posts: 509
Joined: Sun Nov 13, 2005 4:28 pm

Java woes

Post by Nicholai »

Code: Select all

import java.util.Scanner;
public class calculator
{
	public static void main(String[] args)
	{

		Scanner choice = new Scanner(System.in);
		System.out.println("1.) Addition");
		System.out.println("2.) Subtraction");
		System.out.println("3.) Multiplication");
		System.out.println("4.) Division");
		System.out.println();
		System.out.print("#: ");
		int select = choice.nextInt();
		if (select == 1);	
		{
			System.out.println();
			System.out.println("Enter your first number: X + Y");
			System.out.print("X = ");
			double num1 = choice.nextDouble();
			System.out.println("Enter your second number: " + num1 + " + Y");
			System.out.print("Y = ");
			double num2 = choice.nextDouble();
			double ans = num1 + num2;
			System.out.println("Answer: " + num1 + " + " + num2 + " = " + ans);	
		}
		if (select == 2);
		{
			System.out.println();
			System.out.println("Enter your first number: X - Y");
			System.out.print("X = ");
			double num1 = choice.nextDouble();
			System.out.println("Enter your second number: " + num1 + " - Y" );
			System.out.print("Y = ");
			double num2 = choice.nextDouble();
			double ans = num1 - num2;
			System.out.println("Answer: " + num1 + " - " + num2 + " = " + ans);
		}
		if (select == 3);
		{
			System.out.println();
			System.out.println("Enter your first number: X * Y");
			System.out.print("X = ");
			double num1 = choice.nextDouble();
			System.out.println("Enter your second number: " + num1 + " * Y" );
			System.out.print("Y = ");
			double num2 = choice.nextDouble();
			double ans = num1 * num2;
			System.out.println("Answer: " + num1 + " * " + num2 + " = " + ans);	
		}
		if (select == 4);
		{
			System.out.println();
			System.out.println("Enter your first number: X / Y");
			System.out.print("X = ");
			double num1 = choice.nextDouble();
			System.out.println("Enter your second number: " + num1 + " / Y" );
			System.out.print("Y = ");
			double num2 = choice.nextDouble();
			double ans = num1 / num2;
			System.out.println("Answer: " + num1 + " / " + num2 + " = " + ans);
		}
	}
}
	
Why won't this work? If this belongs in Tech Q&A, move it accordingly.
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

We have a programming section now. :P

Why do your conditionals have semi-colons after them?
Nicholai
Posts: 509
Joined: Sun Nov 13, 2005 4:28 pm

Post by Nicholai »

What a silly mistake. :D

We haven't learned conditionals, and this is for 10 points extra credit, if we were able to figure them out.

Code: Select all

import java.util.Scanner;
public class addition
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		double num1;
		double num2;
		double sum;
		
		System.out.println("Enter your first number: X + Y");
		num1 = input.nextDouble();
		System.out.println("Enter your second number: " + num1 + " + Y");
		num2 = input.nextDouble();
		sum = num1 + num2;
		System.out.println("Answer: " + num1 + " + " + num2 + " = " + sum);
		
	}
}
That would be the 100 point version.
marshallh
Moderator
Posts: 2987
Joined: Sat Sep 10, 2005 2:17 pm
360 GamerTag: marshallh
Location: here and there
Contact:

Post by marshallh »

Looks fine, my first guess is that your variables have duplicate names. You also have a lot of fluff code. You can give shot at fixing both:

Code: Select all

import java.util.Scanner;
public class calculator
{
   public static void main(String[] args)
   {
      char op = ' ';
      double ans = 0;
      double num1 = 0;
      double num2 = 0;
      int select = 0;


      Scanner choice = new Scanner(System.in);
      System.out.println("1.) Addition");
      System.out.println("2.) Subtraction");
      System.out.println("3.) Multiplication");
      System.out.println("4.) Division");
      System.out.println();
      System.out.print("#: ");
      select = choice.nextInt();

      switch(select)
      {
         case 1:
            op = '+';
            break;
         case 2:
            op = '-';
            break;
         case 3:
            op = '*';
            break;
         case 4:
            op = '//';     
      }
      
      System.out.println();
      System.out.println("Enter your first number: X " + op + " Y");
      System.out.print("X = ");
      num1 = choice.nextDouble();

      System.out.println("Enter your second number: " + num1 + " " + op + " Y");
      System.out.print("Y = ");
      num2 = choice.nextDouble();

      switch(select)
      {
         case 1:
            ans = num1 + num2;
            break;
         case 2:
            ans = num1 - num2;
            break;
         case 3:
            ans = num1 * num2;
            break;
         case 4:
            ans = num1 / num2;    
      }


      System.out.println("Answer: " + num1 + " " + op + " " + num2 + " = " + ans);   

   }
} 
You might have to escape the some of the chars. I don't have a compiler set up to test it right now, but see how it works.
Image
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Switches are great to compact things, but it's always a good idea to use a default: case just in case (lol pun) your end-user's input is not any of your cases;

Code: Select all

      switch(select)
      {
         case 1:
            ans = num1 + num2;
            break;
         case 2:
            ans = num1 - num2;
            break;
         case 3:
            ans = num1 * num2;
            break;
         case 4:
            ans = num1 / num2; 
            break;
         default:
            System.out.println("Unknown operation!");
            exit;
      } 
Just change Marshall's switch to mine.

More on switch cases: http://java.sun.com/docs/books/tutorial ... witch.html
More on if/else if/else: http://java.sun.com/docs/books/tutorial ... ts/if.html
Nicholai
Posts: 509
Joined: Sun Nov 13, 2005 4:28 pm

Post by Nicholai »

Yeah, no idea how to do that. :P
Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Final code:

Code: Select all

import java.util.Scanner;
public class calculator
{
   public static void main(String[] args)
   {
      char op = ' ';
      double ans = 0;
      double num1 = 0;
      double num2 = 0;
      int select = 0;


      Scanner choice = new Scanner(System.in);
      System.out.println("1.) Addition");
      System.out.println("2.) Subtraction");
      System.out.println("3.) Multiplication");
      System.out.println("4.) Division");
      System.out.println();
      System.out.print("#: ");
      select = choice.nextInt();

      switch(select)
      {
         case 1:
            op = '+';
            break;
         case 2:
            op = '-';
            break;
         case 3:
            op = '*';
            break;
         case 4:
            op = '//';     
      }
     
      System.out.println();
      System.out.println("Enter your first number: X " + op + " Y");
      System.out.print("X = ");
      num1 = choice.nextDouble();

      System.out.println("Enter your second number: " + num1 + " " + op + " Y");
      System.out.print("Y = ");
      num2 = choice.nextDouble();

      switch(select)
      {
         case 1:
            ans = num1 + num2;
            break;
         case 2:
            ans = num1 - num2;
            break;
         case 3:
            ans = num1 * num2;
            break;
         case 4:
            ans = num1 / num2;   
            break;
         default:
            System.out.println("Unknown operation!");
            return;
      }


      System.out.println("Answer: " + num1 + " " + op + " " + num2 + " = " + ans);   

   }
}
Post Reply