我是新來的Java。我希望代碼在用戶輸入錯誤的地方重複,而不是從頭開始。在「輸入b:」或「輸入c:」時,它回到開始「輸入a:」。我希望它只能重複使用,用戶輸入是a,b,c。提前致謝。while true try catch嵌套
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
boolean itsANumber = true;
while (itsANumber)
{
System.out.print("Enter a: ");
try
{
a = Double.parseDouble(sc.nextLine());
System.out.print("Enter b: ");
try
{
b = Double.parseDouble(sc.nextLine());
System.out.print("Enter c: ");
try
{
c = Double.parseDouble(sc.nextLine());
if (a == 0)
{
aZero();
} else
{
aNotZero();
}
} catch (NumberFormatException nfe)
{
System.out
.println("That's not a number, please try again!");
}
} catch (NumberFormatException nfe)
{
System.out
.println("That's not a number, please try again!");
}
} catch (NumberFormatException nfe)
{
System.out.println("That's not a number, please try again!");
}
}
}
異常處理不應該被用於控制邏輯流程。 – Renjith