如何在Java中使我的代码循环回到开头?

huangapple go评论80阅读模式
英文:

How do I make my code loop back to the beginning in Java?

问题

我读过,我需要在一个“if”语句后面加上“continue”,但是每次我在我的if语句中尝试这样做,它都会显示“无法在循环外使用continue”。

英文:

I've read that I would need to put "continue" after an "if" statement, but every I tried this with my if statement and it states that "continue cannot be used outside of a loop."

答案1

得分: 2

在循环中设置它。例如:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    while (true){
        System.out.print("输入密码:密码必须至少包含八个字符,仅限字母和数字,并且至少包含两个数字。");
        String s = input.nextLine();
        if (thepassword(s)) {
            System.out.println("有效密码");
            break;
        } else {
            System.out.println("无效密码");
        }
    }
}

了解更多:Java中的Break和Continue

英文:

Set it in the loop. EX:

public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while (true){
            System.out.print("Enter a password: The password must have at least eight characters, only letters and digits, and at least two digits. ");
            String s = input.nextLine();
            if (thepassword(s)) {
                System.out.println("Valid Password");
                break;
            } else {
                System.out.println("Invalid Password");
            }
        }
    }

See more : Java Break and Continute

答案2

得分: 1

Here's the translated code:

使用一个带有标签的外部无限循环然后使用 <code>break loop_lable</code> 退出循环直到输入有效

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("输入密码:密码必须至少有八个字符,只能包含字母和数字,并且至少有两个数字。");
        loop: for (;;) {
            String s = input.nextLine();
            if (thepassword(s)) {
                System.out.println("有效密码");
                break loop;
            } else {
                System.out.println("无效密码");
                continue loop;
            }
        }
        input.close();
    }

    public static boolean thepassword(String password) {
        boolean thepassword = true;
        if (password.length() < 8) {
            thepassword = false;
        } else {
            int totaldigits = 0;
            for (int n = 0; n < password.length(); n++) {
                if (thedigit(password.charAt(n)) || theletter(password.charAt(n))) {
                    if (thedigit(password.charAt(n))) {
                        totaldigits++;
                    }
                } else {
                    thepassword = false;
                    break;
                }
            }
            if (totaldigits < 2) {
                thepassword = false;
            }
        }
        return thepassword;
    }

    public static boolean theletter(char c) {
        return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
    }

    public static boolean thedigit(char c) {
        return (c >= '0' && c <= '9');
    }
}

Please note that I've translated the code as requested, excluding the code-related portions.

英文:

Use an outer infinite loop with a lable, then break the loop using <code>break loop_lable</code>. Check until a valid input is made.

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(&quot;Enter a password: The password must have at least eight characters, only letters and digits, and at least two digits. &quot;);
loop:for(;;)
{
String s = input.nextLine();
if (thepassword(s)) 
{
System.out.println(&quot;Valid Password&quot;);
break loop;
} 
else 
{
System.out.println(&quot;Invalid Password&quot;);
continue loop;
}
}
input.close();
}
public static boolean thepassword(String password) {
boolean thepassword = true;
if (password.length() &lt; 8) {
thepassword = false;
} else { 
int totaldigits = 0;
for (int n = 0; n &lt; password.length(); n++) {
if (thedigit(password.charAt(n)) || theletter(password.charAt(n))) {
if (thedigit(password.charAt(n))) {
totaldigits++;
}
} else { 
thepassword = false;
break;
}
}
if (totaldigits &lt; 2) { 
thepassword = false;
}
}
return thepassword;
}
public static boolean theletter(char c) {
return ((c &gt;= &#39;a&#39; &amp;&amp; c &lt;= &#39;z&#39;) || (c &gt;= &#39;A&#39; &amp;&amp; c &lt;= &#39;Z&#39;));
}
public static boolean thedigit(char c) {
return (c &gt;= &#39;0&#39; &amp;&amp; c &lt;= &#39;9&#39;);
}
}

huangapple
  • 本文由 发表于 2020年7月31日 10:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63185078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定