我必须在我的项目中添加一个do-while循环,所以在程序迭代后它会说:“你想再次运行这个程序吗?(是或否)”
如果用户输入为是或YE或YES或yE或yES或yeS 它再次运行程序。 如果不是,程序应该自动退出。
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)");
}
}
答案 0 :(得分:-1)
我建议您将逻辑包装在另一个方法中,以便稍后调用它:
import java.text.DecimalFormat;
import java.util.Scanner;
public class CalculatePay {
private static void doStuff() {
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");
}
}
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
do {
//Call your bussines logic.
doStuff();
System.out.println("Do you want to run this program again? (Yes or No)");
}
while(reader.nextLine().toLowerCase().equals("yes"));
} }
请注意,该功能为static
。这是因为您无法从static
上下文调用非静态方法(main是静态的)。