In: Computer Science
in java
Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input.
If number of hours is greater than or equal to 0 and less than 5, then: salary = numberofhours * 5, loop continues, the user can type another number
If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user can type another number
If number of hours is greater or equal to 10, and less than 24, then salary = numberofours * 10, loop continues, the user can type another number
If number of hours is negative or (greater or equal to)24, then the loop stops, message "Quit with invalid input" will be printed.
We assume that the user will type numbers only.
Java code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args){
Scanner input=new
Scanner(System.in);
//initializing number of
hours
double hours;
//asking for number of hours
System.out.print("Enter number of
hours: ");
//accepting number of hours
hours=input.nextInt();
//looping till user enter invalid
value
while(true){
//checking
if the number of hours is greater than or equal to 0 and less than
5
if(hours>=0 && hours<5){
//printing
Salary
System.out.println("Salary="+(hours*5));
//asking for
number of hours
System.out.print("Enter number of hours: ");
//accepting number
of hours
hours=input.nextInt();
}
//checking
if the number of hours is greater than or equal to 5 and less than
10
else
if(hours>=5 && hours<10){
//printing
Salary
System.out.println("Salary="+(hours*8));
//asking for
number of hours
System.out.print("Enter number of hours: ");
//accepting number
of hours
hours=input.nextInt();
}
//checking
if the number of hours is greater than or equal to 10 and less than
24
else
if(hours>=10 && hours<24){
//printing
Salary
System.out.println("Salary="+(hours*10));
//asking for
number of hours
System.out.print("Enter number of hours: ");
//accepting number
of hours
hours=input.nextInt();
}
//checking
for invalid value
else
if(hours<0 || hours>=24){
//printing invalid
input
System.out.print("Quit with invalid input");
//exiting
loop
break;
}
}
}
}
Screenshot:
Input and Output: