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...
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.
​​​​​​​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...
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...
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
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
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
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...
Write a do-while loop IN JAVA to build a Pokémon game Prompt the user for two...
Write a do-while loop IN JAVA to build a Pokémon game Prompt the user for two names one for their Pokémon, and one for the computers Pokémon Make two integer variables to hold the HP of each Pokémon, HP should be 20 for both Pokémon’s. Use Math.random() to determine the amount of damage caused, and which move the computer will select. The range of damage that can be done is up to you. Output to the user an attack menu...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT