2016-10-28 60 views
-3

即時通訊只是想知道是否有任何轉到或繼續命令爲if語句? 即時使用繼續;但它不適用於if語句。是否有任何goto命令或繼續命令爲If語句??? JAVA

     if (typess == 1){ 
       ***OUTER:*** 

        System.out.println("Type: Current Minimum of 1000.00 "); 
        System.out.print("Amount: "); 
        currenttype = Float.parseFloat(in.readLine()); 
        if (currenttype >= 1000){ 
        balance += currenttype; 
        System.out.print("Successful " + currenttype + " Added to your Account"); 

        } 

        else 
        { 
         **continue OUTER;** 
         System.out.print("MINIMUM IS 1000.00 "); 

       } 
+0

什麼?你能否讓代碼更清楚(格式化並明確問題的出處) – ItamarG3

+3

你想要發生什麼?即使條件爲假,你是否還想執行if子句?這沒有什麼意義。看起來你可能需要一個循環。 – Eran

+1

你所描述的'去'可以/必須用方法來解決。每當輸入無效時,您都會遞歸地調用它。 – SomeJavaGuy

回答

1

有這樣的可能性,但您應該使用do-while循環,例如

boolean printMsg = false; 
do { 
    if (printMsg) { 
     System.out.print("MINIMUM IS 1000.00 "); 
    } 
    printMsg = true; 
    System.out.println("Type: Current Minimum of 1000.00 "); 
    System.out.print("Amount: "); 
    currenttype = Float.parseFloat(in.readLine()); 
} while (currenttype < 1000); 

balance += currenttype; 
System.out.print("Successful " + currenttype + " Added to your Account"); 
2

你可以用簡單的遞歸方法解決它。

只需包括邏輯部分成方法,驗證輸入並且如果不正確it's,撥打呼叫recursiv,像在本例中:

public class Project { 

    static Scanner scanner = new Scanner(System.in); 

    public static void main(String[] args) { 
     float float_val = getInput(); 
     System.out.println("You did input: " + float_val); 
    } 

    public static float getInput() { 
     System.out.println("Please input variable"); 
     float input = scanner.nextFloat(); 
     if(input < 0) { 
      System.out.println("Invalid input!"); 
      return getInput(); 
     } 
     return input; 
    } 
} 

樣本輸入:

-5 
-4 
5 

示例輸出:

Please input variable 
-5 
Invalid input! 
Please input variable 
-4 
Invalid input! 
Please input variable 
5 
You did input: 5.0