Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im writing a Java Program and dont know how to make it so that if the user enters an invalid number of HoursWorked 3 times

Im writing a Java Program and dont know how to make it so that if the user enters an invalid number of HoursWorked 3 times the program terminates. I know that i have to use a while loop but don't know how to initialize and increment it so that it only terminates if the user has to be asked 3 times.

import java.util.Scanner;

public class CalculatePay {

public static void main(String[] args) { //Declare the variable data types double HoursWorked, PayRate, TotalPay; //Declare an int for the while loop //int Retries = 1; //Declare the sting Variables String EmployeeName, EmployeeInput, RepeatInput; //Declare the char variable char EmployeeType, Repeat; //Create a scanner object to read inputs Scanner keyboard = new Scanner(System.in); //Start the do-while loop do { //Get the users name System.out.println("Hello, what is your name? "); EmployeeName = keyboard.nextLine();

//Get the amount of hours the employee worked System.out.println("How many hours did you work this week? "); HoursWorked = keyboard.nextDouble(); while(HoursWorked < 0 || HoursWorked > 280) { System.out.println(" The amount of hours worked is not possible! try again/ "); HoursWorked = keyboard.nextDouble(); } //Get the employees hourly pay rate System.out.println("What is your hourly pay rate? "); PayRate = keyboard.nextDouble(); //Calculate the employees total pay TotalPay = PayRate * HoursWorked; //Consume the remaining new line keyboard.nextLine(); //Get the type of employee the user is System.out.println("Are you Part time, Full Time, or Temporary? "); System.out.println("Enter P for part time, F for full time and T for temporary "); EmployeeInput = keyboard.nextLine(); //Can't read a string into a char so only getting the first letter EmployeeType = EmployeeInput.charAt(0); // Determine the employee type switch(EmployeeType) { case 'p': case 'P': System.out.println("You are a Part-time employee. "); break; case 'f': case 'F': System.out.println("You are a Full-time employee. "); break; case 't': case 'T': System.out.println("You are a Temporary employee. "); break; default: System.out.println("Unknown Employee Type. "); } //Print message on screen giving the employees pay for the week //with a precision of 2 decimal places only if the statement is true if(HoursWorked > 0 && HoursWorked < 280) { System.out.println("Hello " + EmployeeName + ", you made " ); System.out.printf("$%.2f this week! ", TotalPay); } //This messages only displayes if everything is valid if(PayRate >= 100 && HoursWorked > 0 && HoursWorked < 280) System.out.println(" You must be a Java Programmer! "); //Aske the user if they want to run the program again System.out.println("Would you like to run the program again? "); System.out.println("Enter either Yes or No. "); RepeatInput = keyboard.nextLine(); Repeat = RepeatInput.charAt(0); }while(Repeat == 'Y' || Repeat == 'y'); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

Evaluate the integrals in Problems 3348. Le dx en

Answered: 1 week ago

Question

74 Motivation concepts and applications.

Answered: 1 week ago