所以我有一個關於停止循環的大問題。主要問題是我必須在用戶輸入無效3次後停止while循環。但是,我不知道該怎麼做。如何在第三次無效嘗試後停止While循環?
如何在第三次無效嘗試後停止while循環?
我應該使用什麼樣的代碼?
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;
char repeat;
String input = " ";
double grossPay;
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 == 3)
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){
// irrelevant for the question
}
}
}
JavaScript不是JAVA。 – PHPglue