一个数组只允许Java的某些值0-9和A-F

时间:2022-03-18 11:04:47

Full working now and code is complete Thanks for the help.

现在全部工作,代码完成感谢您的帮助。

2 个解决方案

#1


0  

You can restrict user to enter another value by this: (This program is for if You are taking values from user). This will asks for number until you enter number within 0 to 9.

您可以通过此限制用户输入另一个值:(此程序适用于您从用户获取值的情况)。这将询问数字,直到您输入0到9之间的数字。

You can make your code according to this. (This is just for your reference, How can you restrict user to enter wrong thing)

您可以根据此制作代码。 (这仅供您参考,如何限制用户输入错误的内容)

Scanner scan=new Scanner(System.in);
int i=-1;
i=scan.nextInt();
while(i<=0 && i>=9){
    i=scan.nextInt();
}

EDIT

As per your comment, In that case you need to change this as:

根据您的评论,在这种情况下,您需要将其更改为:

String s="";
while(!s.matches("^[0-9A-F]+$")){
    s=scan.nextLine();
}

#2


0  

I would create a class to hold the RGB values and have it check that the correct values are entered. See the test code below.... you can expand as you need to handle more cases.

我会创建一个类来保存RGB值,并检查输入的值是否正确。请参阅下面的测试代码....您可以扩展,因为您需要处理更多案例。

import java.util.*;

public class jtest
{
  public static void main(String args[])
  {
    new jtest();
  }

  public jtest()
  {
    ArrayList<RGB> RGBarray = new ArrayList<RGB>();

    try
    {
    RGBarray.add(new RGB("F"));
    RGBarray.add(new RGB("J"));
    }
    catch(BadRGBValueException BRGBVE)
    {
      BRGBVE.printStackTrace();
    }
  }

  class BadRGBValueException extends Exception
  {
    public BadRGBValueException(String message)
    {
      super(message);
    }
  }

  class RGB
  {
    public RGB(String input) throws BadRGBValueException
    {
      if (!input.matches("^[0-9A-F]+$"))
      {
        throw new BadRGBValueException(input + " is not a valid RGB value");
      }

      value = input;
    }

    private String value = null;
  }
}

#1


0  

You can restrict user to enter another value by this: (This program is for if You are taking values from user). This will asks for number until you enter number within 0 to 9.

您可以通过此限制用户输入另一个值:(此程序适用于您从用户获取值的情况)。这将询问数字,直到您输入0到9之间的数字。

You can make your code according to this. (This is just for your reference, How can you restrict user to enter wrong thing)

您可以根据此制作代码。 (这仅供您参考,如何限制用户输入错误的内容)

Scanner scan=new Scanner(System.in);
int i=-1;
i=scan.nextInt();
while(i<=0 && i>=9){
    i=scan.nextInt();
}

EDIT

As per your comment, In that case you need to change this as:

根据您的评论,在这种情况下,您需要将其更改为:

String s="";
while(!s.matches("^[0-9A-F]+$")){
    s=scan.nextLine();
}

#2


0  

I would create a class to hold the RGB values and have it check that the correct values are entered. See the test code below.... you can expand as you need to handle more cases.

我会创建一个类来保存RGB值,并检查输入的值是否正确。请参阅下面的测试代码....您可以扩展,因为您需要处理更多案例。

import java.util.*;

public class jtest
{
  public static void main(String args[])
  {
    new jtest();
  }

  public jtest()
  {
    ArrayList<RGB> RGBarray = new ArrayList<RGB>();

    try
    {
    RGBarray.add(new RGB("F"));
    RGBarray.add(new RGB("J"));
    }
    catch(BadRGBValueException BRGBVE)
    {
      BRGBVE.printStackTrace();
    }
  }

  class BadRGBValueException extends Exception
  {
    public BadRGBValueException(String message)
    {
      super(message);
    }
  }

  class RGB
  {
    public RGB(String input) throws BadRGBValueException
    {
      if (!input.matches("^[0-9A-F]+$"))
      {
        throw new BadRGBValueException(input + " is not a valid RGB value");
      }

      value = input;
    }

    private String value = null;
  }
}