Results 1 to 3 of 3
- 05-05-2011, 04:08 AM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
Loop returns at second input rather than first.
Could someone provide some input on why my loop is returning back to the second portion of the program? I would actually prefer that the first loop keep looping until "stop" is issued but at this point I will take any advice to have it start at step 1.
import java.util.Scanner;
public class Payrollb
{
public static void main( String[] args )
{
Scanner input = new Scanner( System.in );
int dept1;
double salary1;
double sum;
boolean contRun = true;
while (contRun)
{
System.out.println( "Enter Name of The Department:" );
String deptName = input.nextLine();
if(deptName.equalsIgnoreCase("stop")){
}
do
{
System.out.println("Enter The Number of Employees In The Department:" );
dept1 = input.nextInt();
if(dept1 < 0)
System.out.println("Number Must Be A Positive");
}
while(dept1 < 0);
do
{
System.out.println("Enter The Average Salary Of Employees:" );
salary1 = input.nextDouble();
if(salary1 < 0)
System.out.println("Number Must Be A Positive");
}
while(salary1 < 0);
sum = dept1 * salary1;
System.out.println( "For Department: " + deptName);
System.out.println( "Average Salary Per Employee Is: $" + sum );
}
}
}
- 05-05-2011, 04:19 AM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
You're missing a } in your first loop, causing the other loops to be part of that loop.
Also, if you didn't already know, "break;' terminates the loop so you can use that when the user enters stop.
- 05-05-2011, 06:03 AM #3
OP,
Use Code Tags while posting your code. And read about the branching statements in Java : Branching Statements
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
Every Input File I Use Returns "Unsorted"
By Cod in forum New To JavaReplies: 36Last Post: 02-27-2011, 06:43 AM -
while loop bypasses scanner input on 2nd pass
By xf021209 in forum New To JavaReplies: 2Last Post: 02-28-2010, 08:10 AM -
[SOLVED] User Input - loop
By new person in forum New To JavaReplies: 4Last Post: 02-22-2009, 10:02 PM -
loop when there is no user-input
By becky in forum New To JavaReplies: 12Last Post: 02-02-2009, 10:02 PM -
Perfect Square Array Input Using For Loop
By dalangley in forum New To JavaReplies: 9Last Post: 01-27-2009, 01:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks