GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Oct-2007, 22:27
fishguts fishguts is offline
New Member
 
Join Date: Sep 2007
Posts: 3
fishguts is on a distinguished road

Question on how to apply do while loop


Hello I have the following code below. I need the different menu choices in main to keep repeating until the user decides to enter 5 which then terminates the program. I was told to use a do while statement. I have tried it and It did not work right. It would stop after only 2 tries through the code. I also tried enclosing the whole chunk of code in a do while loop but that did not work either. Can anyone help me?

JAVA Code:
//The imports below are for I/O 
import java.util.*;
import java.io.*; 

// Declare a public class
public class Homework4 { 
	
		static int doIntMath(char op,int numa, int numb){
		int return_val0;
		return_val0 = 0;
		switch (op)	
		
		//These switch statements determines what input the user enters and computes it.
		{
		case '+': 
		return_val0 = numa + numb;
		break;
		
		case '-': 
		return_val0 = numa - numb;
		break;
		
		case '*': 
		return_val0 = numa * numb;
		break;
		
		case '/': 
		return_val0 = numa / numb;
		break;
		
		case '%': 
		return_val0 = numa + numb;
		break;
		
		//if the user enters an exponent expression the exponent method will handle the input.
		case '^':
		return_val0 = exponent(numa, numb);
		break;
		
		//Declare a default statement if the user enters a invalid expression and return number to 0
		default:
		return_val0 = 0;
		System.out.println("You have entered an Invalid Operation");
		}
		
		return return_val0;
		
}

	//Make method to do double expressions
	static double doDoubleMath(char op,double numa, double numb){
		double return_val;
		return_val = 0;
		switch (op)	
		
		//These switch statements determines what input the user enters and computes it.
		{
		case '+': 
		return_val = numa + numb;
		break;
		
		case '-': 
		return_val = numa - numb;
		break;
		
		case '*': 
		return_val = numa * numb;
		break;
		
		case '/': 
		return_val = numa / numb;
		break;
		
		//Declare a default statement if the user enters a invalid expression and return number to 0
		default:
		return_val = 0;
		System.out.println("You have entered an Invalid Operation");
		}
		
		return return_val;
		
}
//Make the exponent method
	static int exponent(int a, int b){
		int return_val = 1;	
		
		//This method will handle an exponent expression
		if (b < 0) 
		{
		System.out.println("Error: Exponent Must Be >=0");
		return_val = 1;
		}
		
		else if (b == 0) 
		{
		return_val = 1;
		}
		
		else if (b == 1) 
		{
		return_val = a;
		}
		
		else 
		{
		return_val = a * exponent (a, b-1);
		}
		return (return_val);
		}	

	//Declare main
	public static void main (String[] args){
	
	//Declare variables
	int inta, intb;
	char option0, charop;
	double intc, intd;
	String userinput0, op0, op1;
	
	
	//Create scanner
	Scanner sc = new Scanner(System.in);
	
	System.out.println("1) Do Integer Expression");
	System.out.println("2) Do Floating Point Expression");
	System.out.println("3) Show integer Answers");
	System.out.println("4) Show Floating Point Answers");
	System.out.println("5) End Program");
	System.out.println("Selection:");
	
	userinput0 = sc.next();
	option0 = userinput0.charAt(0);
	
	if (option0 == '1')
	{
	System.out.println("Enter An Integer Expression");
	
	inta = sc.nextInt();
	op0 = sc.next();
	charop = op0.charAt(0);
	intb = sc.nextInt();
	
	System.out.println(inta + op0 + intb + "=" +doIntMath(charop, inta, intb));
	}
	
	else if (option0 == '2')
	{
	System.out.println("Enter a floating point expression");
	
	intc = sc.nextDouble();
	op1 = sc.next();
	charop = op1.charAt(0);
	intd = sc.nextDouble();
	
	System.out.println(intc + op1 + intd + "="+doDoubleMath(charop, intc, intd));
	}	
	

	
}
}
  #2  
Old 04-Oct-2007, 12:21
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Question on how to apply do while loop


Seems that the 'do' could be placed just before the options display, and then place the 'while' portion after the last option check, which, in the above code, would be after the last option check of '2'.

So that the code within the loop would be similar to this:
JAVA Code:
do
{
   // display the choices
   
   // get the input

   // option 1 check

   // option 2 check

   // more option checks as needed

}while ( option0 != '5' );
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20
Simple question about while loop Lej C++ Forum 6 07-Aug-2005 14:57
Array 1 dimensional help please asap lion123 C Programming Language 10 18-Feb-2005 22:53
Nested for loop with function Tori C++ Forum 11 08-Nov-2004 14:02
C switch / loop issue spudtheimpaler C Programming Language 4 20-Feb-2004 21:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:01.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.