Results 1 to 16 of 16
Thread: What kind of loop?
- 01-27-2012, 09:37 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
What kind of loop?
If I want to ask a user for certain options and then add those options, how do I do that? I did an if..else statment but is not working too good. Here is the code:
Java Code:if(skillLevel == 2 || skillLevel == 3) System.out.println("Choose your insurance options:"); option = keyboard.nextInt(); if(option == 1) insurance = 32.50; else if(option == 2) insurance = 20.00; else if(option == 3) insurance = 10.00; else if(option == 1 && option == 2) insurance = 52.50; else if(option == 1 && option == 3) insurance = 42.50; else if(option == 2 && option == 3) insurance = 30.00; else insurance = 0;Last edited by gabrielpr12; 01-27-2012 at 09:47 PM. Reason: indenting
- 01-27-2012, 09:40 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Re: What kind of loop?
Your indentation makes my eyes bleed; please change that, Java is not Pascal.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-27-2012, 10:20 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: What kind of loop?
any input?
- 01-27-2012, 10:35 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: What kind of loop?
Do you want the entire piece of code to execute ONLY when skillLevel is equal to either 2 or 3? If so, it's wrong; the way you currently have it, only the System.out.println will run only in that condition; the rest will always run.
If you want an if statement to apply to multiple lines of code and not just one, you must put {curly braces} around the code that you want the if statement to apply to.
-
Re: What kind of loop?
Will this ever be true?
Java Code:if(option == 1 && option == 3)
This equivalent to saying :
Java Code:if (sexOfPerson == Sex.MALE && sexOfPerson == Sex.FEMALE) { // hire them out as a circus freak }
- 01-27-2012, 11:45 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
- 01-27-2012, 11:53 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
- 01-28-2012, 04:51 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: What kind of loop?
Should I implement a do..while loop while the user wants to add insurance?
-
Re: What kind of loop?
I've no idea as you're not really told us what the problem is. You may wish to spend a few minutes and type out a paragraph or two describing your problem in greater detail. Please have a look at my link below on how to ask smart questions that will give you more helpful suggestions on how to make your questions easier to answer.
- 01-28-2012, 05:02 PM #10
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: What kind of loop?
I am trying to create a program that asks the user for their skill level, which will determine the pay rate. Now, skill level 1 does not have option to choose insurance, but skill level 2 and 3 do. Skill Level 2 and 3 can choose up to three insurance options. I need to create a loop that keeps asking for adding insurance. Then skill level 3 can opt to choose retirement plan at 3% of their gross pay. At the end I need to display the hours worked, pay rate, regular pay, overtime pay, total pay, deductions and net pay. The above code was just the if..else statement dealing with the insurance options. Here is the complete code:
Java Code:import java.util.*; import javax.swing.*; public class Pay3 { public static void main(String[] args) { int addInsurance = 2; final double MEDICAL = 32.50; final double DENTAL = 20.00; final double DISABILITY = 10.00; int choice; choice = 0; double retirement; double insurance; double totalPay; insurance = 0; double rate; rate = 0; int option; double hoursWorked; double regularPay; double overtimePay; double netPay; int skillLevel; final int FULL_WEEK = 40; final double OT_RATE = 1.5; Scanner keyboard = new Scanner(System.in); System.out.print("What is your skill level?"); skillLevel = keyboard.nextInt(); if(skillLevel == 1) rate = 17.00; else if(skillLevel == 2) rate = 20.00; else if(skillLevel == 3) rate = 22.00; else System.out.println("Invalid skill level."); System.out.print("How many hours did you work this week?"); hoursWorked = keyboard.nextDouble(); if(hoursWorked > FULL_WEEK) { regularPay = FULL_WEEK * rate; overtimePay = (hoursWorked - FULL_WEEK) * OT_RATE * rate; } else { regularPay = hoursWorked * rate; overtimePay = 0.0; } if(skillLevel != 1) {System.out.println("Choose your insurance options:"); option = keyboard.nextInt(); if(option == 1) insurance = MEDICAL; else if(option == 2) insurance += insurance + DENTAL; else if(option == 3) insurance += insurance + DISABILITY; else if(option == 1 && option == 2) insurance = 52.50; else if(option == 1 && option == 3) insurance = 42.50; else if(option == 2 && option == 3) insurance = 30.00; else insurance = 0;} if(skillLevel == 3) {System.out.println("Would you like to participate in thge retirement plan?"); choice = keyboard.nextInt(); if(choice == 1) retirement = 0.03; else retirement = 0;} totalPay = regularPay + overtimePay; netPay = totalPay - insurance; System.out.println("Regular pay is " + regularPay + "\nOvertime pay is " + overtimePay + "\nHours worked: " + hoursWorked + "\nYour hourly rate is: " + rate + "\nTotal gross pay is: " + (regularPay + overtimePay) + "\nYour total deductions are: " + insurance + "\nYour net pay is: " + netPay); } }
-
Re: What kind of loop?
Suggestions:
- Your if/else blocks (and other portions of your code) are indented in a very misleading way suggesting that you have nested if statements when in fact you don't. You should learn to indent correctly as this will make your code much easier for you and others to read and debug.
For example compare your code:
with correct indentation:Java Code:if(option == 1) insurance = MEDICAL; else if(option == 2) insurance += insurance + DENTAL; else if(option == 3) insurance += insurance + DISABILITY; else if(option == 1 && option == 2) insurance = 52.50; else if(option == 1 && option == 3) insurance = 42.50; else if(option == 2 && option == 3) insurance = 30.00; else insurance = 0;}
Java Code:if (option == 1) insurance = MEDICAL; else if (option == 2) insurance += insurance + DENTAL; else if (option == 3) insurance += insurance + DISABILITY; else if (option == 1 && option == 2) insurance = 52.50; else if (option == 1 && option == 3) insurance = 42.50; else if (option == 2 && option == 3) insurance = 30.00; else insurance = 0; - Next, nest all code blocks in curly braces. This will make the blocks more clear and will reduce careless errors. So I'd change the above to:
Java Code:if (option == 1) { insurance = MEDICAL; } else if (option == 2) { insurance += insurance + DENTAL; } else if (option == 3) { insurance += insurance + DISABILITY; } else if (option == 1 && option == 2) { insurance = 52.50; } else if (option == 1 && option == 3) { insurance = 42.50; } else if (option == 2 && option == 3) { insurance = 30.00; } else { insurance = 0; } - Next display better prompts. For instance, you are requesting insurance "options" but you're not showing a list of what options are avaiable, so this prompt means nothing to the user. Also it means nothing to me or your instructor since we have no way of interpretting what options mean what.
Also, again your code is in error and will need to be re-written since option can never equal 2 and 3 at the same time.Last edited by Fubarable; 01-28-2012 at 05:29 PM.
- Your if/else blocks (and other portions of your code) are indented in a very misleading way suggesting that you have nested if statements when in fact you don't. You should learn to indent correctly as this will make your code much easier for you and others to read and debug.
-
Re: What kind of loop?
But to answer your direct question, yes a do-while loop could work well for getting correct input from the user of your program.
- 01-28-2012, 05:57 PM #13
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: What kind of loop?
Ok, so I got most of it fixed. Now my only problem is the do..while loop is not working correctly, it is not looping. This is the updated code:
Java Code:import java.util.*; import javax.swing.*; public class Pay3 { public static void main(String[] args) { int addInsurance = 2; final double MEDICAL = 32.50; final double DENTAL = 20.00; final double DISABILITY = 10.00; int choice; choice = 0; double retirement; double insurance; double totalPay; double deductions; insurance = 0; double rate; rate = 0; int option; double hoursWorked; double regularPay; double overtimePay; double netPay; int skillLevel; final int FULL_WEEK = 40; final double OT_RATE = 1.5; Scanner keyboard = new Scanner(System.in); System.out.print("What is your skill level?"); skillLevel = keyboard.nextInt(); if(skillLevel == 1) rate = 17.00; else if(skillLevel == 2) rate = 20.00; else if(skillLevel == 3) rate = 22.00; else System.out.println("Invalid skill level."); System.out.print("How many hours did you work this week?"); hoursWorked = keyboard.nextDouble(); if(hoursWorked > FULL_WEEK) { regularPay = FULL_WEEK * rate; overtimePay = (hoursWorked - FULL_WEEK) * OT_RATE * rate; } else { regularPay = hoursWorked * rate; overtimePay = 0.0; } totalPay = regularPay + overtimePay; if(skillLevel != 1) { do { JOptionPane.showInputDialog("Choose your insurance options: \n1) Medical Insurance" + "\n2) Dental Insurance \n3) Long-term Disability Insurance \n0) No Insurance"); option = keyboard.nextInt(); if(option == 1) insurance = MEDICAL; else if(option == 2) insurance += insurance + DENTAL; else if(option == 3) insurance += insurance + DISABILITY; }while(option != 0); } else insurance = 0; if(skillLevel == 3) System.out.println("Would you like to participate in the retirement plan?"); choice = keyboard.nextInt(); if(choice == 1) retirement = 0.03 * totalPay; else retirement = 0; deductions = insurance + retirement; netPay = totalPay - deductions; System.out.println("Regular pay is " + regularPay + "\nOvertime pay is " + overtimePay + "\nHours worked: " + hoursWorked + "\nYour hourly rate is: " + rate + "\nTotal gross pay is: " + (regularPay + overtimePay) + "\nYour total deductions are: " + deductions + "\nYour net pay is: " + netPay); } }Last edited by gabrielpr12; 01-28-2012 at 06:23 PM.
-
Re: What kind of loop?
OK, let's look at the do-while loop a little more closely:
This loops until choice is non-0. Where do you change choice inside of the loop? Should you perhaps be using another variable, not choice, as the control variable, the one that is checked in the while boolean condition?Java Code:do { JOptionPane.showInputDialog("Choose your insurance options: \n1) Medical Insurance" + "\n2) Dental Insurance \n3) Long-term Disability Insurance \n0) No Insurance"); option = keyboard.nextInt(); if (option == 1) insurance = MEDICAL; else if (option == 2) insurance += insurance + DENTAL; else if (option == 3) insurance += insurance + DISABILITY; } while (choice != 0);
- 01-28-2012, 06:23 PM #15
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
-
Re: What kind of loop?
When this happens put in debug statements to test the code. For instance:
Then once you see this result, you should see how you can fix it.Java Code:do { JOptionPane.showInputDialog("Choose your insurance options: \n1) Medical Insurance" + "\n2) Dental Insurance \n3) Long-term Disability Insurance \n0) No Insurance"); option = keyboard.nextInt(); if (option == 1) insurance = MEDICAL; else if (option == 2) insurance += insurance + DENTAL; else if (option == 3) insurance += insurance + DISABILITY; // add the following System.out.println("Debug, option is: " + option); } while (option != 0);
Similar Threads
-
what kind of application is this?
By mymark in forum SWT / JFaceReplies: 0Last Post: 08-03-2011, 12:11 AM -
Kind of stuck
By Nicky Swans in forum New To JavaReplies: 8Last Post: 10-22-2010, 02:46 PM -
';' expected, not of the common kind..
By Addez in forum New To JavaReplies: 6Last Post: 09-02-2009, 04:37 AM -
how client know what kind of server
By lemur in forum NetworkingReplies: 3Last Post: 05-31-2008, 07:11 AM -
Total Newbie, Be Kind :)
By dazza-s in forum New To JavaReplies: 11Last Post: 04-26-2008, 10:54 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks