Führen Sie eine While-Schleife mit Ja = erneut starten und Nein = Programm beenden ausJava

Java-Forum
Anonymous
 Führen Sie eine While-Schleife mit Ja = erneut starten und Nein = Programm beenden aus

Post by Anonymous »

Ich muss meinem Projekt eine Do-While-Schleife hinzufügen, damit nach der Iteration des Programms Folgendes angezeigt wird: „Möchten Sie dieses Programm erneut ausführen? (Ja oder Nein)“

Wenn die Benutzereingaben „Ja“ oder „Ja“ oder „Ja“ oder „Ja“ oder „Ja“ oder „Ja“ lauten
Es wird ein Programm erneut ausgeführt.
Wenn es „Nein“ lautet, sollte das Programm automatisch beendet werden.

Code: Select all

import java.text.DecimalFormat;
import java.util.Scanner;

public class CalculatePay {

public static void main(String[] args) {
Scanner reader = new Scanner(System.in);

String Name = " ";
int hours;
double payRate;
char F;
char P;
char T;

String input = " ";
char repeat = input.charAt(0);

double grossPay;

int attempt = 0;

System.out.print("What is your name? ");
Name = reader.nextLine();
System.out.print("How many hours did you work? ");
hours = reader.nextInt();
while (hours < 0 || hours > 280)
{
System.out.println("That's not possible, try again!");
hours = reader.nextInt();
attempt++;
if(attempt >= 2) {
System.out.println("You are Fired!");
return ;
}
}
System.out.print("What is your pay rate? ");
payRate = reader.nextDouble();
System.out.print("What type of employee are you? ");
F = reader.next().charAt(0);
grossPay = hours * payRate;
DecimalFormat decFor = new DecimalFormat("0.00");

switch (F)
{
case 'F' :
case 'f' :
System.out.println("Hi " + Name + ", you made $" + decFor.format(grossPay) + " this week"+" as a full-time employee");
if (hours < 0)
{
System.out.println(" That's not possible ");
}
else if (hours > 280)
{
System.out.print(" That's not possible ");
}
else if (grossPay >= 100)
{
System.out.print(" You must be a Java programmer!");
}
else
{
System.out.print(" this week");
}

break;
case 'P' :
case'p' :
System.out.println("Hi " + Name + ", you made $" + decFor.format(grossPay) + " this week"+" as a part- time employee");
if (hours < 0)
{
System.out.println(" That's not possible ");
}
else if (hours > 280)
{
System.out.print(" That's not possible ");
}
else if (grossPay >= 100)
{
System.out.print(" You must be a Java programmer!");
}
else
{
System.out.print(" this week");
}

break;
case 'T' :
case 't' :
System.out.println("Hi " + Name + ", you made $" + decFor.format(grossPay) + " this week"+" as a temporary employee");
if (hours < 0)
{
System.out.println(" That's not possible ");
}
else if (hours > 280)
{
System.out.print(" That's not possible ");
}
else if (grossPay >= 100)
{
System.out.print(" You must be a Java programmer!");
}
else
{
System.out.print(" this week");
}

break;
default:
System.out.println(" unknown employee type");

if (hours < 0)
{
System.out.println(" That's not possible ");
}
else if (hours > 280)
{
System.out.print(" That's not possible ");
}
else if (grossPay >= 100)
{
System.out.print(" You must be a Java programmer!");
}
else
{
System.out.print(" this week");
}
}
System.out.println("Do you want to run this program again? (Yes or No)");
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post