954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Do While Sentinel Looping

Hi there. This is actually one of my assignment and i am having problem with the do while sentinel looping. The result i get is totally inverse from the one i expected. The system is supposingly ended when user enter 0, but it ended when user enter any value but the looping continue when user enter 0.

These are the code. Thank you.Sorry i am still very new in programming. =(

import java.util.Scanner;
public class SoftwareWorkshop {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Registrant, totalRegistrant;
double Charges, totalCharges, averageCharges, totalAll;
totalRegistrant = 0;
totalAll = 0;
Registrant =0;
do {
System.out.println("Enter amount of registrant in the company.Enter 0 to end the system.");
Registrant = input.nextInt();
if (Registrant >=11) {
Charges = 250;
totalCharges = (Registrant * Charges);
totalRegistrant += Registrant;
totalAll += totalCharges;
System.out.println("The total is RM" + totalCharges);
}
else if (Registrant >= 6) {
Charges = 400;
totalCharges = (Registrant * Charges);
totalRegistrant += Registrant;
totalAll += totalCharges;
System.out.println("The total is RM" + totalCharges);
}
else {
Charges = 450;
totalCharges = (Registrant * Charges);
totalRegistrant += Registrant;
totalAll += totalCharges;
System.out.println("The total is RM" + totalCharges);
}
}

while(Registrant == 0); {
System.out.println("End");
}
}

}

limpek1991
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

You have

do {
   ...
while(Registrant == 0);

ie keep doing the loop as long as Registrant is 0
Can you see why that's wrong?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

One thing that you will learn about computers is that they will always do exactly what you tell them to do. ...even when that's not what you want.

So you've asked the computer to continue doing the loop while the Registrant (number) is equal to zero.

Is that what you really want? :-/

JeffGrigg
Posting Whiz in Training
218 posts since Aug 2011
Reputation Points: 192
Solved Threads: 28
 

oh ok bro! thank you i get it now! sorry for the newbie question. thanks for the answer! love ya guys.

limpek1991
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You