Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确保用户输入在给定的有效范围内?_Java_Loops_Validation - Fatal编程技术网

Java 如何确保用户输入在给定的有效范围内?

Java 如何确保用户输入在给定的有效范围内?,java,loops,validation,Java,Loops,Validation,我目前正在编写一个代码,制作一个由1-20个星号组成的空心正方形。到目前为止,我已经知道了这些,但是如果用户输入一个大于20的数字,我不知道如何在代码本身上循环。例如,如果用户要输入21,代码仍然会创建星号方块,并在下方弹出“无效大小。输入1-20之间的大小:”文本以重新提示用户,但新数字不会产生任何结果。我需要它不创建正方形(如果>20或循环,直到您获得满意的输入: int x=0; /* ... */ while (x < 1 || x > 20) { System.

我目前正在编写一个代码,制作一个由1-20个星号组成的空心正方形。到目前为止,我已经知道了这些,但是如果用户输入一个大于20的数字,我不知道如何在代码本身上循环。例如,如果用户要输入21,代码仍然会创建星号方块,并在下方弹出“无效大小。输入1-20之间的大小:”文本以重新提示用户,但新数字不会产生任何结果。我需要它不创建正方形(如果>20或循环,直到您获得满意的输入:

int x=0;
/*   ... */
while (x < 1 || x > 20)
{
    System.out.printf("Enter the Size of the Asterisk Square from 1-20: "); // prompt user
    x = input.nextInt();  
}
intx=0;
/*   ... */
而(x<1 | | x>20)
{
System.out.printf(“在1-20之间输入星号方块的大小:”;//提示用户
x=input.nextInt();
}

以下是一些解决您问题的代码

它使用一个递归函数,如果不成功,它会再次调用自己

if(诸如此类);不执行任何操作(它执行分号之前的表达式=不执行任何操作。)

ifs的工作原理如下:

如果(诸如此类){

代码

代码

代码

}

请注意花括号

在python中使用identiation时,通常使用java中的花括号

import java.util.Scanner; // program uses class Scanner

public class HollowSquare {
    public static void main(String[] args){
        drawSquare();
    }
    public static void drawSquare() {

        // variable declaration
        int x;
        int s;
        int p;
        int r = 0;
        int y = 1;
        int z = 1;
        int f = 1;

        Scanner input = new Scanner(System.in);

        System.out.printf("Enter the Size of the Asterisk Square from 1-20: "); // prompt user
        x = Integer.parseInt(input.nextLine()); 
        if (x > 0 && x <= 20){

        // read and store data from user
        s = x + 2;
        p = x - 2;

        // print output
        while (y <= x) 
        {
            System.out.printf(" * ");
            y++;
        }
        while (r < p) 
        {
            System.out.print("\n * ");
            while (z <= p) 
            {
                System.out.print("   ");
                z++;
            }
            System.out.print(" * ");
            r++;
            z=1;
        }
        System.out.print("\n");
        if (x > 1)
        {
            while (f <= x) 
            {
                System.out.printf(" * ");
                f++;
            }
            System.out.printf("\n");
        }
        else
                System.out.printf("\n");

     // conditions
    }else{
            System.out.println("Invalid Size. Enter Size from 1-20: ");
            drawSquare();
}
    } // end main method
} // end class HollowSquare
import java.util.Scanner;//程序使用类扫描程序
公共级霍洛斯广场酒店{
公共静态void main(字符串[]args){
drawSquare();
}
公共静态空间{
//变量声明
int x;
int-s;
INTP;
int r=0;
int y=1;
intz=1;
int f=1;
扫描仪输入=新扫描仪(System.in);
System.out.printf(“在1-20之间输入星号方块的大小:”;//提示用户
x=Integer.parseInt(input.nextLine());

如果(x>0 & & x)你考虑循环,直到你得到满意的输入吗?<代码>(x>0)x抱歉,我仍然很新的编码。我可以删除那个if语句,我想这就是把参数放在这个代码上的。那个循环可以工作。但是我不确定如何添加。请在方便的时候取下。
import java.util.Scanner; // program uses class Scanner

public class HollowSquare {
    public static void main(String[] args){
        drawSquare();
    }
    public static void drawSquare() {

        // variable declaration
        int x;
        int s;
        int p;
        int r = 0;
        int y = 1;
        int z = 1;
        int f = 1;

        Scanner input = new Scanner(System.in);

        System.out.printf("Enter the Size of the Asterisk Square from 1-20: "); // prompt user
        x = Integer.parseInt(input.nextLine()); 
        if (x > 0 && x <= 20){

        // read and store data from user
        s = x + 2;
        p = x - 2;

        // print output
        while (y <= x) 
        {
            System.out.printf(" * ");
            y++;
        }
        while (r < p) 
        {
            System.out.print("\n * ");
            while (z <= p) 
            {
                System.out.print("   ");
                z++;
            }
            System.out.print(" * ");
            r++;
            z=1;
        }
        System.out.print("\n");
        if (x > 1)
        {
            while (f <= x) 
            {
                System.out.printf(" * ");
                f++;
            }
            System.out.printf("\n");
        }
        else
                System.out.printf("\n");

     // conditions
    }else{
            System.out.println("Invalid Size. Enter Size from 1-20: ");
            drawSquare();
}
    } // end main method
} // end class HollowSquare