如何设置命令行的输入条件? (Java)的

时间:2015-10-29 12:57:28

标签: java shell command command-line-arguments rgb

所以我需要编写一个程序,它有3个正整数,介于0和255之间作为命令行参数(输入),我该如何设置这些条件?到目前为止我写道:

public class RGBtoCMyk {
public static void main(String []args){
   int r = Integer.parseInt(args[0]);
   int b = Integer.parseInt(args[1]);
   int g = Integer.parseInt(args[2]);

  //now i must set the conditions, that r, g and b are between 0 and 255
  }

如果您有任何想法,请帮助我

3 个答案:

答案 0 :(得分:0)

您需要处理ArrayIndexOutOfBoundsException以确保您有足够的输入元素和NumberFormatException以确保您拥有int值。如果您没有其中一个例外,则可以检查所有值是否都在给定的时间间隔内。

public class RGBtoCMyk {
public static void main(String []args){

    try {
    int r = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    int g = Integer.parseInt(args[2]);

    if ((0 <= r) && (r < 256) && (0 <= b) && (b < 256) && (0 <= g) && (g < 256)) {
        System.out.println("Valid colors");
    } else {
        System.out.println("Invalid colors");
    }

    //now i must set the conditions, that r, g and b are between 0 and 255
    } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
        System.out.println("Not enough input");
    } catch (NumberFormatException numberFormatException) {
        System.out.println("Inputs are not integer values");
    }
}

答案 1 :(得分:0)

您可以添加检查条件,如果您的号码超出范围,则可以提示用户您可以重新输入。

如果他们输入值&gt;没有别的办法255你想减少它。您可以选择以下两个选项。

  1. 选项1,如果用户输入的数量超过255,则将其视为255,并将值小于零,将其视为零。如果他输入一个字符串会抛出异常。

    public class RGBtoCMyk {
    public static void main(String []args){
    try {
    int r = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    int g = Integer.parseInt(args[2]);
    
    r = r>255 ? 255:r;
    r = r<0 ? 0:r;
    
    g = g>255 ? 255:g;
    g = g<0 ? 0:g;
    
    b = b>255 ? 255:b;
    b = b<0 ? 0:b;
    }
    catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
    System.out.println("Not enough input");
    } catch (NumberFormatException numberFormatException) {
    System.out.println("Inputs are not integer values");
      }
     }
    }  
    
  2. 选项2:如果值不在0到255之间,则再次输入值

    public class RGBtoCMyk {
    public static void main(String []args){
    try {
    Scanner sc=new Scanner(System.in);
    int r = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    int g = Integer.parseInt(args[2]);
    
    If(r>255 || r<0){
    System.out.println("Value Not in Range. Please Enter between 0 and 255");
     int usrInput=sc.nextInt();
     }
    
    //Similarly for g and b.
    b = b>255 ? 255:b;
    b = b<0 ? 0:b;
    }
    
    catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
    System.out.println("Not enough input");
    } catch (NumberFormatException numberFormatException) {
    System.out.println("Inputs are not integer values");
       }
      }
     }
    

答案 2 :(得分:0)

有一些很好的方法可以解析输入数据。

使用陈述

如果检测到格式错误的输入,请正常退出

if(args.length < 3)
{
    printUsage();
    return;
}
int r, g, b;
try
{
    r = Integer.parseInt(args[0]);
    g = Integer.parseInt(args[1]);
    b = Integer.parseInt(args[2]);
}
catch(NumberFormatException e)
{
    printUsage();
    return;
}
if((r | g | b) >> 8 != 0) //same as these other two if statements
//if(((r | g | b) & 0xffffff00) != 0)
//if(r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255)
{
    printUsage();
    return;
}
//put your code here, you have r, g, and b 0-255

//add in the printUsage method somewhere
public void printUsage()
{
    System.err.println("Input is 3 numbers, each 0-255");
}

错误抛出

如果你不想捕捉错误并打印出来的东西,而只是让错误打印到控制台

int r = Integer.parseInt(args[0]);
int g = Integer.parseInt(args[1]);
int b = Integer.parseInt(args[2]);
//at this point the program will have exited if 3 non-ints were passed, now check values
if((r | g | b) >> 8 != 0) //same as the other two if statements from before
    throw new IllegalArguementException("Color parameters outside of expected range (0-255)"); //mostly copied format from java.awt.Color
//do stuff with r, g, and b being 0-255

处理无效参数

另一种选择是格式化它,这样你就可以传入255,0,0只为255

无论你给出什么样的输入错误,程序都会尝试继续处理

int r = 0;
int g = 0;
int b = 0;
try
{
    r = Integer.parseInt(args[0]);
    g = Integer.parseInt(args[1]);
    b = Integer.parseInt(args[2]);
}
catch(NumberFormatException e){}
if(r >> 8 != 0)
    r = 0;
if(g >> 8 != 0)
    g = 0;
if(b >> 8 != 0)
    b = 0;
//or you could make it so that it takes the last bits, 256 would be 0, 257 would be 1, and so on...
//r = r & 0xFF;
//g = g & 0xFF;
//b = b & 0xFF;

//do stuff with your r, g, and b being 0-255