Results 1 to 6 of 6
Thread: Do loop help
- 02-28-2013, 02:49 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
Do loop help
I'm in need of a little help... I am trying to use a do loop and I'm stuck on how to make the second inner loop to when it prints "Calculate another interest?" if they type yes to loop up and ask the loan amt, interest, and term again? Any help would be great thanks!
Java Code:public static void main(String[] args ) { Scanner input = new Scanner(System.in); do{ //Prompt user to enter loan amount System.out.print("Enter Loan Amount: "); int principle = input.nextInt(); //Prompt user to enter interest rate System.out.print("Enter Yearly Interest Rate (1 to 100 percent): "); double interest = input.nextDouble(); //Prompt user to enter term System.out.print("Enter the Term (in months): "); int term = input.nextInt(); if (principle > 0) { if (interest >= 0 && interest <= 100) { if(term > 0) { double simple= principle*(interest/100.0)*(term/12.0); double repaidSimple= simple+principle; double a = 1.0+((interest/100.0)/365.0); double b = (365.0*(term/12.0)); double daily = Math.pow(a,b); double dailyCompound = (principle*daily)-principle; double repaidDaily = dailyCompound + principle; double c = 1.0+((interest/100)/12.0); double d = (12.0*(term/12.0)); double monthly = Math.pow(c, d); double monthlyCompound = (principle*monthly)-principle; double repaidMonthly = monthlyCompound + principle; int type = 0; //Prompt user to enter calculation type do{System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):"); type = input.nextInt(); switch(type) { case 1:System.out.println("Total calculated interest: " +simple); System.out.println("Total amount to be repaid: " +repaidSimple); break; case 2:System.out.println("Total calculated interest: " +monthlyCompound); System.out.println("Total amount to be repaid: " +repaidMonthly); break; case 3:System.out.println("Total calculated interest: " +dailyCompound); System.out.println("Total amount to be repaid: " +repaidDaily); break; default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type); break; } }while(type<1||type>3); } else { System.out.println("Data Error: Term must be greater than zero. You entered " +term); } } else { System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest); } } else { System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle); } do{ System.out.println("Calculate another loan interest (Yes/No)?"); }while(input.next().equalsIgnoreCase("yes")&& input.next().equalsIgnoreCase("no")); }while (input.next().equalsIgnoreCase("yes")); System.out.println("Thank you for using Interest Calculator!"); } }
- 02-28-2013, 03:00 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 895
- Rep Power
- 1
Re: Do loop help
First, your loops won't ever work properly as written:
Two problems here:Java Code:do{ System.out.println("Calculate another loan interest (Yes/No)?"); }while(input.next().equalsIgnoreCase("yes")&& input.next().equalsIgnoreCase("no"));
Your first input.next() reads an answer so the next input.next() would either block or read something else that is not an answer.
So you should normally do this:
There is still a huge problem. Answer can't be both yes and no at the same time so the loop will alwaysJava Code:String answer = input.next(); do{ System.out.println("Calculate another loan interest (Yes/No)?"); }while(answer.equalsIgnoreCase("yes")&& answer.equalsIgnoreCase("no"));
fail.
Try rewriting the code and putting in print statements as debugging aids.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-28-2013, 04:20 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
Re: Do loop help
I see that this will cause a problem, I'm just having trouble where I should place theJava Code:do{ System.out.println("Calculate another loan interest (Yes/No)?"); }while(input.next().equalsIgnoreCase("yes"));and then where I should place theJava Code:do{to make the program ask for the principle, interest, and term again..Java Code:}while(input.next().equalsIgnoreCase("yes"));
Thanks for the quick response, I'm still new to using java so bear with me!
- 02-28-2013, 04:22 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 895
- Rep Power
- 1
Re: Do loop help
Your big loop which does all the processing should be the inner loop. The outer loop is the one which asks if the user wants to repeat the process. So in essence, the outer loop and question/answer determines if the inner loop will run.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-28-2013, 04:47 AM #5
Member
- Join Date
- Feb 2013
- Posts
- 7
- Rep Power
- 0
Re: Do loop help
I think I have it, well it seems to be working properly for me... now when I need is when it prompts "Calculate another loan interest (Yes/No)?" if you type in Okay, maybe, basically anything but yes, no, y,n, etc it returns the error "Please enter 'Yes' or 'No'" Am I just setting it up wrong or how should I key that in? use an if statement?
Java Code:public static void main(String[] args ) { Scanner input = new Scanner(System.in); do{ do{ //Prompt user to enter loan amount System.out.print("Enter Loan Amount: "); int principle = input.nextInt(); //Prompt user to enter interest rate System.out.print("Enter Yearly Interest Rate (1 to 100 percent): "); double interest = input.nextDouble(); //Prompt user to enter term System.out.print("Enter the Term (in months): "); int term = input.nextInt(); if (principle > 0) { if (interest >= 0 && interest <= 100) { if(term > 0) { double simple= principle*(interest/100.0)*(term/12.0); double repaidSimple= simple+principle; double a = 1.0+((interest/100.0)/365.0); double b = (365.0*(term/12.0)); double daily = Math.pow(a,b); double dailyCompound = (principle*daily)-principle; double repaidDaily = dailyCompound + principle; double c = 1.0+((interest/100)/12.0); double d = (12.0*(term/12.0)); double monthly = Math.pow(c, d); double monthlyCompound = (principle*monthly)-principle; double repaidMonthly = monthlyCompound + principle; int type = 0; //Prompt user to enter calculation type do{System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):"); type = input.nextInt(); switch(type) { case 1:System.out.println("Total calculated interest: " +simple); System.out.println("Total amount to be repaid: " +repaidSimple); break; case 2:System.out.println("Total calculated interest: " +monthlyCompound); System.out.println("Total amount to be repaid: " +repaidMonthly); break; case 3:System.out.println("Total calculated interest: " +dailyCompound); System.out.println("Total amount to be repaid: " +repaidDaily); break; default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type); break; } }while(type<1||type>3); } else { System.out.println("Data Error: Term must be greater than zero. You entered " +term); } } else { System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest); } } else { System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle); } System.out.println("Calculate another loan interest (Yes/No)?"); }while(input.next().equalsIgnoreCase("yes")); System.out.println("Thank you for using Interest Calculator!"); }while (input.next().equalsIgnoreCase("yes")); } }
- 02-28-2013, 05:09 AM #6
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 895
- Rep Power
- 1
Re: Do loop help
Here is skeleton of what I think you want to do.
Jim
Java Code:boolean val = true; while (val) { System.out.print("Would you like to play a game?"); String ans = input.next(); if (ans.equal("yes")) { do { // Your processing code goes here. } while ( /* Some inner test here */ ); } else { val = false; //causes outer loop to fall thru (you could also use a break here) } } System.out.println("Thank you for playing");The Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
Converting a for loop to a do-while loop
By awesom in forum New To JavaReplies: 1Last Post: 11-23-2011, 03:02 PM -
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks