是否有任何goto命令或继续命令为If语句??? JAVA

时间:2016-10-28 07:50:07

标签: java netbeans

我只是想知道if语句有没有goto或continue命令? 我正在使用继续;但它没有在if声明上工作。

                       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 ");

               }

2 个答案:

答案 0 :(得分:2)

您可以使用简单的递归方法解决它。

只需将逻辑部分包含在方法中,验证输入,如果不正确,请进行递归调用,如下例所示:

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

答案 1 :(得分: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");