【问题标题】:How to set condition for the input from command line? (java)如何设置命令行输入的条件? (爪哇)
【发布时间】:2016-01-29 15:15:00
【问题描述】:

所以我需要编写一个程序,它有 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
  }

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

【问题讨论】:

  • 设置是什么意思,你的意思是用用户输入填充它?如果您在 RGBtoCMyk.java 中有该文件,请使用命令行:javac RGBtoCMyk.javajava RGBtoCMyk <arg1> <arg2> <arg3>
  • 那么,如果用户使用与条件不匹配的参数运行程序,您想做什么?提示:它们甚至可能不是数字,用户可以使用java RGBtoCMyk foo bar baz 运行,甚至可以尝试不带参数运行。
  • 或者您是否试图确保三个值在 0-255 之间,在这种情况下,您可以只使用 if 语句,如果数字超出范围则抛出无效参数异常
  • 尝试谷歌搜索if声明
  • 如果用户输入除数字以外的任何内容,Integer.parseInt() 将抛出异常,您只需处理检查它是否为 0-255,您可以使用 new Color(r, g, b) 进行检查,因为这将检查输入并为您抛出异常

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


【解决方案1】:

您需要处理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");
    }
}

【讨论】:

    【解决方案2】:

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

    如果他们输入的值 > 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");
         }
        }
       }
      

    【讨论】:

      【解决方案3】:

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

      使用说明

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

      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
      

      【讨论】:

        猜你喜欢
        • 2016-06-12
        • 2015-01-30
        • 1970-01-01
        • 2014-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-27
        相关资源
        最近更新 更多