Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/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_Parameters - Fatal编程技术网

如何在Java中设置参数?

如何在Java中设置参数?,java,parameters,Java,Parameters,情况如下: 装修师需要输入房间的高度(2到6米之间),然后输入所有四面墙的长度(最小1米;最大25米) 我编写了扫描仪来接收用户的输入,但不知道如何为扫描仪设置参数。我想让程序做的是接收用户的输入,如果(例如,房间的高度为9米)输入不在打印错误的参数范围内。如果我理解正确,您必须创建所谓的参数。扫描器不能执行您想要的操作 同样,如果我理解正确,您应该创建If条件来检查用户是否给了您正确的输入 而且您只需要一个扫描器实例。因此: Scanner scannerToUsAll = new Scann

情况如下:

装修师需要输入房间的高度(2到6米之间),然后输入所有四面墙的长度(最小1米;最大25米)


我编写了扫描仪来接收用户的输入,但不知道如何为扫描仪设置参数。我想让程序做的是接收用户的输入,如果(例如,房间的高度为9米)输入不在打印错误的参数范围内。

如果我理解正确,您必须创建所谓的参数。
扫描器
不能执行您想要的操作

同样,如果我理解正确,您应该创建If条件来检查用户是否给了您正确的输入

而且您只需要一个
扫描器
实例。因此:

Scanner scannerToUsAll = new Scanner(System.in);

System.out.println("Enter Height of the room");
int height = scannerToUsAll.nextInt();

//here you check
if ( height < 2 && height > 6  ){
     System.out.println("The Height is not within the parameters (2 and 6)");
}
Scanner scannerToUsAll=新扫描仪(System.in);
System.out.println(“输入房间高度”);
int height=scannerToUsAll.nextInt();
//这是您的支票
如果(高度<2和高度>6){
System.out.println(“高度不在参数(2和6)范围内”);
}
如果需要获得其他输入,只需使用相同的扫描仪
int length=scannertusall.nextInt()

您需要控制应用程序的流程才能退出或返回到同一问题。我在这里的提示:
系统.out.println(“输入房间高度”);
System.out.println("Enter Height of the room");
  Scanner sc = new Scanner(System.in); 
    int height = sc.nextInt();
    if (height < 2 || height > 6)
    {
        System.out.println("Error: height is invalid");
    }

System.out.println("Enter Length1 of the room");
        int length1 = sc.nextInt();
        if (length1 < 1 || length1 > 25)
        {
            System.out.println("Error: length1 is invalid");
        }

System.out.println("Enter Length2 of the room");
            int length2 = sc.nextInt();
            if (length2 < 1 || length2 > 25)
            {
                System.out.println("Error: length2 is invalid");
            }
扫描仪sc=新的扫描仪(System.in); int height=sc.nextInt(); 如果(高度<2 | |高度>6) { System.out.println(“错误:高度无效”); } System.out.println(“输入房间长度1”); int length1=sc.nextInt(); 如果(长度1<1 | |长度1>25) { System.out.println(“错误:length1无效”); } System.out.println(“输入房间长度2”); int length2=sc.nextInt(); 如果(长度2<1 | |长度2>25) { System.out.println(“错误:长度2无效”); }

。。。等等…

它真的需要创建这么多的
扫描仪吗。这会导致不可预测的结果。使用
if
条件检查用户输入。是的,您真正需要的只是一台
扫描仪
扫描仪in=新扫描仪(System.in)
然后可以在.nextInt()中调用
任意次数。使用
扫描仪时,请确保
in.close()
。另外,设置参数是什么意思?好的,干杯。。。我将编辑我的帖子以删除它@彼得拉维
System.out.println("Enter Height of the room");
  Scanner sc = new Scanner(System.in); 
    int height = sc.nextInt();
    if (height < 2 || height > 6)
    {
        System.out.println("Error: height is invalid");
    }

System.out.println("Enter Length1 of the room");
        int length1 = sc.nextInt();
        if (length1 < 1 || length1 > 25)
        {
            System.out.println("Error: length1 is invalid");
        }

System.out.println("Enter Length2 of the room");
            int length2 = sc.nextInt();
            if (length2 < 1 || length2 > 25)
            {
                System.out.println("Error: length2 is invalid");
            }