Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

condition to leave all your loops

 
Aretha Clark
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that conditions can be written to check multiple things. For example...

while(condition1 && condition2 && condition3...)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic