Java 循环回到代码的开头

Java 循环回到代码的开头,java,loops,Java,Loops,我见过一些这样的线程,但我不完全理解如何在代码中实现它 import java.util.Scanner; public class Test { public static void main(String[] args)throws java.io.IOException { Scanner reader = new Scanner(System.in); System.out.println("Enter a number betwe

我见过一些这样的线程,但我不完全理解如何在代码中实现它

import java.util.Scanner;

public class Test {



    public static void main(String[] args)throws java.io.IOException {



        Scanner reader = new Scanner(System.in);

        System.out.println("Enter a number between 1-1000: ");

        int n = reader.nextInt();

        if(90<=n && n<=110) System.out.println(n + " is close to 100");
        if(190<=n && n<=210) System.out.println(n + " is close to 200");
        if(290<=n && n<=310) System.out.println(n + " is close to 300");
        if(390<=n && n<=410) System.out.println(n + " is close to 400");
        if(490<=n && n<=510) System.out.println(n + " is close to 500");
        if(590<=n && n<=610) System.out.println(n + " is close to 600");
        if(690<=n && n<=710) System.out.println(n + " is close to 700");
        if(790<=n && n<=810) System.out.println(n + " is close to 800");
        if(890<=n && n<=910) System.out.println(n + " is close to 900");
        if(n>1000)System.out.println("Your number is too high");
        else System.out.println("Your number is not close to any 100s");


    }

}       
import java.util.Scanner;
公开课考试{
公共静态void main(字符串[]args)引发java.io.IOException{
扫描仪阅读器=新扫描仪(System.in);
System.out.println(“输入一个介于1-1000之间的数字:”);
int n=reader.nextInt();
如果(90试试这个

布尔标志;
做{
flag=false;
System.out.println(“输入一个介于1-1000之间的数字:”);
int n=reader.nextInt();

如果(90您可以使用while循环,并且仅当输入的数字小于1或大于1000时才可以中断该循环

import java.util.Scanner;

public class Test {



public static void main(String[] args) throws java.io.IOException {


    Scanner reader = new Scanner(System. in );
    Boolean exit = false;

    while (!exit) {
        System.out.println("Enter a number between 1-1000: ");
        int n = reader.nextInt();
        if (n < 0 || n > 1000) {

            System.out.println("That number was invalid");
        } else {
            exit = true;
            if (90 <= n && n <= 110) System.out.println(n + " is close to 100");
            if (190 <= n && n <= 210) System.out.println(n + " is close to 200");
            if (290 <= n && n <= 310) System.out.println(n + " is close to 300");
            if (390 <= n && n <= 410) System.out.println(n + " is close to 400");
            if (490 <= n && n <= 510) System.out.println(n + " is close to 500");
            if (590 <= n && n <= 610) System.out.println(n + " is close to 600");
            if (690 <= n && n <= 710) System.out.println(n + " is close to 700");
            if (790 <= n && n <= 810) System.out.println(n + " is close to 800");
            if (890 <= n && n <= 910) System.out.println(n + " is close to 900");
        }
    }
}

}
import java.util.Scanner;
公开课考试{
公共静态void main(字符串[]args)引发java.io.IOException{
扫描仪阅读器=新扫描仪(System.in);
布尔退出=假;
当(!退出){
System.out.println(“输入一个介于1-1000之间的数字:”);
int n=reader.nextInt();
如果(n<0 | | n>1000){
System.out.println(“该数字无效”);
}否则{
退出=真;

如果(90)检查你的文档中的while(){}结构。我真的很抱歉,但是我的“文档”是什么?我在哪里可以找到它?问你的老师或者在java while loop上搜索web。就是这样,谢谢!你能解释一下为什么你在“while(!flag)”的开头加了一个感叹号吗?所以基本上我说while(exit)与表示while(exit==true)相同!是not运算符。因此表示while(!exit)与while(exit!=true)相同。总之,表示while exit的值不为true,然后执行循环。这就是为什么将exit设置为true会停止循环。
import java.util.Scanner;

public class Test {



public static void main(String[] args) throws java.io.IOException {


    Scanner reader = new Scanner(System. in );
    Boolean exit = false;

    while (!exit) {
        System.out.println("Enter a number between 1-1000: ");
        int n = reader.nextInt();
        if (n < 0 || n > 1000) {

            System.out.println("That number was invalid");
        } else {
            exit = true;
            if (90 <= n && n <= 110) System.out.println(n + " is close to 100");
            if (190 <= n && n <= 210) System.out.println(n + " is close to 200");
            if (290 <= n && n <= 310) System.out.println(n + " is close to 300");
            if (390 <= n && n <= 410) System.out.println(n + " is close to 400");
            if (490 <= n && n <= 510) System.out.println(n + " is close to 500");
            if (590 <= n && n <= 610) System.out.println(n + " is close to 600");
            if (690 <= n && n <= 710) System.out.println(n + " is close to 700");
            if (790 <= n && n <= 810) System.out.println(n + " is close to 800");
            if (890 <= n && n <= 910) System.out.println(n + " is close to 900");
        }
    }
}

}