3 Replies - 152 Views - Last Post: 29 January 2018 - 04:50 AM Rate Topic: -----

#1 aemaeth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 10-February 17

Just trying to make a infinite loop and when user types '5', i

Posted 28 January 2018 - 09:22 PM

Below is my class code for Java. I'm basically trying to make a little calculator that should keep asking the user what mathematical operation they want to perform UNTIL they hit '5', which breaks the loop. Can anyone tell me why it isn't breaking the loop when user enters '5'? I have it set to change the Boolean value of the while loop to false, so I don't get why it's not working.



import java.util.Scanner;

public class commandLineCalculator {
	int op1;
	int op2;
	
	public commandLineCalculator() {
	op1 = 0;
	op2 = 0;
	}

	public void calculate() {
		
		int answer = 0;
		boolean a = true;
		
		Scanner k = new Scanner(System.in);
		
		System.out.println("=======================================================\r\n" + 
				"Welcome to my calculator!");
		
		System.out.println("1 - Addition\r\n" + 
				"2 - Subtraction\r\n" + 
				"3 - Multiplication\r\n" + 
				"4 - Division\r\n" + 
				"5 - Exit");
		
		while(a != false) {
		System.out.println("Which operation do you want to perform: ");
		char operation = k.next().charAt(0);
		
		System.out.println("What is your first operand:");
		int op1 = k.nextInt();
		System.out.println("What is your second operand:"); 
		int op2 = k.nextInt();
		
		if(operation == '1')
		{
			answer = op1 + op2;
		}
		else if(operation == '2')
		{
			answer = op1 - op2;
		}
		else if(operation == '3')
		{
			answer = op1 * op2;
		}
		else if(operation == '4')
		{
			answer = op1/op2;
			if(op2==0)
				System.out.println("Division by zero not allowed!");
		}
		else if(operation == '5')
		{
			a = false;
		}
		else
		{
			System.out.println("Thank you for using our calculator!");
		}
		
		System.out.println("The result is: " + answer);
		}
		
		System.out.println("Thanks for using my calculator!\r\n" + 
				"=======================================================");
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Just trying to make a infinite loop and when user types '5', i

#2 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 700
  • View blog
  • Posts: 2,863
  • Joined: 13-June 14

Re: Just trying to make a infinite loop and when user types '5', i

Posted 28 January 2018 - 10:31 PM

When I ran your program (after adding a main method), I got the following output:

=======================================================
Welcome to my calculator!
1 - Addition
2 - Subtraction
3 - Multiplication
4 - Division
5 - Exit
Which operation do you want to perform:
5
What is your first operand:
1
What is your second operand:
2
The result is: 0
Thanks for using my calculator!
=======================================================

What do you expect to be different? It's breaking out of the loop when you tell it to.

A few things:

1. Please use meaningful names for your variables, methods and class names. Names like a and k don't tell anyone (you included!) what those things are for. Good names help readability.

2. Your code is poorly indented. Lines between curly braces should be indented within them (like all the lines between the opening and ending braces of calculate), because that helps readability. All the lines within your while loop should be indented and so should the lines in your constructor.

3. The convention for class names in Java is that they begin with uppercase letters.
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 700
  • View blog
  • Posts: 2,863
  • Joined: 13-June 14

Re: Just trying to make a infinite loop and when user types '5', i

Posted 28 January 2018 - 11:37 PM

Remember that computers are dumb and do exactly what you tell them. If you give your instructions in the wrong order, they'll be executed in the wrong order...
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3503
  • View blog
  • Posts: 15,835
  • Joined: 20-September 08

Re: Just trying to make a infinite loop and when user types '5', i

Posted 29 January 2018 - 04:50 AM

Quote

System.out.println("1 - Addition\r\n" + 
				"2 - Subtraction\r\n" + 
				"3 - Multiplication\r\n" + 
				"4 - Division\r\n" + 
				"5 - Exit");


You don't want those platform-specific line feeds. Issue System.out.println for each command
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1