wood burning stoves*
The moose likes Beginning Java and the fly likes condition to leave all your loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "condition to leave all your loops " Watch "condition to leave all your loops " New topic
Author

condition to leave all your loops

Aretha Clark
Greenhorn

Joined: Mar 07, 2008
Posts: 21
Looking for some advise as to correct these 2 problems i had with my past homework.


1.Include a condition to leave all your loops instead of using a break to leave. No infinite loops are needed.

2.You need to use proper indentation and there is no indentation here.





import java.util.Scanner; // This program uses class Scanner

public class Payroll {

// main method begins execution of Java Application
public static void main(String args[]) {

//create a scanner object to obtain input from command window
Scanner input = new Scanner(System.in);

String employeeName = ""; //input employee name
double hourlyWage;
double hoursWorked;
double weeklyPay;

// loop until the user enters 'stop' as the name of the employee.
do {

// Get Employee Name
System.out.print("Enter Name of Employee:");
employeeName = input.next();
// if the employee name is 'stop', then exit the loop.
if (employeeName.equals("stop")) {
break;
}

// ask for hourly wage
System.out.print("Enter hourly Wage:");
hourlyWage = input.nextDouble();
// require that the hourly wage is a non-negative
while (hourlyWage < 0.0) {
System.out.print("Hourly wage must be a positive number. Please enter a positive hourly wage:");//prompt
hourlyWage = input.nextDouble();
input.nextLine();
}

// ask the hours worked
System.out.print("Enter hours worked:");
hoursWorked = input.nextDouble();
// require that hours worked be a non-negative number
while (hoursWorked < 0.0) {
System.out.print("Hours worked must be a positive number. Please enter a positive number of hours:");//prompt
hoursWorked=input.nextDouble();
input.nextLine();


}

weeklyPay = hourlyWage * hoursWorked;
System.out.printf("The employee, %s, was paid $ %.2f this week.\n\n", employeeName, weeklyPay);

} while (true);

} //end method main

} //end class Payroll
[ March 14, 2008: Message edited by: Jim Yingst ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note that conditions can be written to check multiple things. For example...

while(condition1 && condition2 && condition3...)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Aretha Clark:
... You need to use proper indentation and there is no indentation here...

What "proper" means is a matter of debate. Have you been provided certain standards to work with? If not, you might start with Sun's Java Conventions.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: condition to leave all your loops
 
Similar Threads
Nested while loop inside do...while loop?
illegal start of expression
Code problems
Code Help
NoClassDefFoundError