Question

In: Computer Science

in java Write a while loop to ask the user to type number of hours(double) they...

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.

Solutions

Expert Solution

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:


Related Solutions

Write a while loop to ask the user to type number of hours(double) they work per...
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 0 and less than 5, then:  salary = numberofhours * 12, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 12, then: salary = numberofours * 14, loop continues, the user can type another number If...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to see if the value is a number between 1 and 10 #if number is too high or too low, tell user, if they guessed it break out of loop Display "Welcome to my Guess the number program!" random mynumber count=1 while True try Display "Guess a number between 1 and 10"   Get guess while guess<1 or guess>10 Display "Guess a number between 1 and...
7. Ask the user to type in a number and compare this number with 8. The...
7. Ask the user to type in a number and compare this number with 8. The user could try twice. If the first user input was 8 then print out “Correct!” and terminate the program, otherwise ask user to type in a number for the second time. If the second input was 8 then print out “Correct”, otherwise print “Game over!” Finish the assembly code segment for the above requirement. .data message: .asciiz "Please enter a random number:" right_str: .asciiz...
Ask the user how many days that they collected gems. A loop you write should loop...
Ask the user how many days that they collected gems. A loop you write should loop how many days the user enters. If the user enters 5 days, then the loop should loop 5 times to collect the data from each day. So, each loop iteration represents a day. In each loop iteration, ask the user how many gems they collected that day. After the loop finishes gathering the data for each day, calculate the total and average gems collected....
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT