Refresh the value of a variable?

When using the while loop in the prog., it does not ask user to make a

Page 1 of 1

5 Replies - 4124 Views - Last Post: 06 December 2008 - 04:46 PM Rate Topic: -----

#1 DeWire  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-December 08

Refresh the value of a variable?

Post icon  Posted 06 December 2008 - 02:32 PM

Hi,

I would like the user to be asked if they want to do multiplication, addition or subtraction again after they chose 'Y' at the end question, but it saves what option was initially chosen. Any suggestions ?

Thanks for all help,
Dave

class ass2{
public static void main (String[]args){

char menu;
	char again;// char value declared

System.out.println("Welcome to Dave's Calculator");
System.out.println("Please chose from one of the following options");
			System.out.println("[A] Addition");
			System.out.println("[S] Subtraction");
			System.out.println("[M] Multiplication");


			menu = Keyboard.readChar();//allows menu value to be taken in from the users keyboard
			menu = Character.toUpperCase(menu);





do{

while((menu != 'A')&&(menu != 'S')&&(menu != 'M')) {
System.out.println("Please chose options A , S or M");//error message if the user enters a value other than A, S or M
menu = Keyboard.readChar();//allows menu value to be taken in from users keyboard using keyboard.class
}

double number1, number2, result;

		switch (menu){//switch for the menu to operate

		case 'A' : System.out.println("You chose A for addition.");
				 System.out.println("Please enter number 1");
				 number1 = Keyboard.readDouble();
				 System.out.println("Please enter number 2");
				 number2 = Keyboard.readDouble();
				 result = number1 + number2;
				 System.out.println("The answer is:   " + result);

break;

		case 'S' : System.out.println("You chose S for subtraction.");
				 System.out.println("Please enter number 1");
				 number1 = Keyboard.readDouble();
				 System.out.println("Please enter number 2");
				 number2 = Keyboard.readDouble();
				 result = number1 - number2;
				 System.out.println("The answer is:   " + result);  break;

		case 'M' : System.out.println("You chose M for multiplication.");
				 System.out.println("Please enter number 1");
				 number1 = Keyboard.readDouble();
				 System.out.println("Please enter number 2");
				 number2 = Keyboard.readDouble();
				 result = number1 * number2;
				 System.out.println("The answer is:   " + result);  break;

	}

do{

			System.out.println("would you like to calculate another sum?");
			again=Keyboard.readChar();
			again = Character.toUpperCase(again);//allows acceptance of capital y and n

		}while(!(again =='N' || again =='Y'));
		} while (again == 'Y');{
		System.out.println("Welcome to Dave's Calculator");
		System.out.println("Please chose from one of the following options");
					System.out.println("[A] Addition");
					System.out.println("[S] Subtraction");
					System.out.println("[M]. Multiplication");



					menu = Keyboard.readChar();//allows menu value to be taken in from the users keyboard

}



}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Refresh the value of a variable?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2728
  • View blog
  • Posts: 11,470
  • Joined: 20-September 08

Re: Refresh the value of a variable?

Posted 06 December 2008 - 02:42 PM

The way to 'refresh' it is to re-read it, so put it inside your do loop
Was This Post Helpful? 0
  • +
  • -

#3 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Refresh the value of a variable?

Posted 06 December 2008 - 02:43 PM

Reset the value of menu variable when you ask for yes or now, because in the second or more time through the loop the menu variable is one of the choices so:
while((menu != 'A')&&(menu != 'S')&&(menu != 'M')) {


will never execute again.

Also the input is invalid as the class KeyBoard does not exist
Was This Post Helpful? 0
  • +
  • -

#4 DeWire  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-December 08

Re: Refresh the value of a variable?

Posted 06 December 2008 - 03:25 PM

thank you for your responses but could you please explain what you both mean ?
Was This Post Helpful? 0
  • +
  • -

#5 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Refresh the value of a variable?

Posted 06 December 2008 - 04:06 PM

The first do loop executes once, and then again wile 'Y' is selected, immediately after the loop starts a while loop is executed with the menu variable not being equal to the choices as the condition, after the loop through if 'Y' is selected and the menu variable is not reset the same choice will be in there and the while loop will never be executed. The same would apply for the again variable in the inner do loop, create an infinite loop, except that is a do while loop so it is executed once always. So another choice besides resetting the value is making the while loop a do while loop.
do{


		while((menu != 'A')&&(menu != 'S')&&(menu != 'M')) {
		System.out.println("Please chose options A , S or M");//error message if the user enters a value other than A, S or M
		menu = Keyboard.readChar();//allows menu value to be taken in from users keyboard using keyboard.class
		}

		double number1, number2, result;

				switch (menu){//switch for the menu to operate
				case 'A' : System.out.println("You chose A for addition.");
						 System.out.println("Please enter number 1");
						 number1 = Keyboard.readDouble();
						 System.out.println("Please enter number 2");
						 number2 = Keyboard.readDouble();
						 result = number1 + number2;
						 System.out.println("The answer is:   " + result);
						 break;
				case 'S' : System.out.println("You chose S for subtraction.");
						 System.out.println("Please enter number 1");
						 number1 = Keyboard.readDouble();
						 System.out.println("Please enter number 2");
						 number2 = Keyboard.readDouble();
						 result = number1 - number2;
						 System.out.println("The answer is:   " + result);  
						 break;
				case 'M' : System.out.println("You chose M for multiplication.");
						 System.out.println("Please enter number 1");
						 number1 = Keyboard.readDouble();
						 System.out.println("Please enter number 2");
						 number2 = Keyboard.readDouble();
						 result = number1 * number2;
						 System.out.println("The answer is:   " + result);  
						 break;

			}

				do{

					System.out.println("would you like to calculate another sum?");
					again=Keyboard.readChar();
					again = Character.toUpperCase(again);//allows acceptance of capital y and n
					menu = ' ';   //Reset menu to any char except 'M', 'S', or 'A'
				}while(!(again =='N' || again =='Y'));
		 } while (again == 'Y'); 

Was This Post Helpful? 1
  • +
  • -

#6 DeWire  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-December 08

Re: Refresh the value of a variable?

Posted 06 December 2008 - 04:46 PM

View PostBigAnt, on 6 Dec, 2008 - 03:06 PM, said:

The first do loop executes once, and then again wile 'Y' is selected, immediately after the loop starts a while loop is executed with the menu variable not being equal to the choices as the condition, after the loop through if 'Y' is selected and the menu variable is not reset the same choice will be in there and the while loop will never be executed. The same would apply for the again variable in the inner do loop, create an infinite loop, except that is a do while loop so it is executed once always. So another choice besides resetting the value is making the while loop a do while loop.


Your a gent. thanks mate.

Dave
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1