简体   繁体   English

具有固定长度的Java Iterate数组,并使用Scanner类获取值

[英]Java Iterate array with a fixed length and take values with Scanner class

How can i get the value of a java array using Scanner class with a fixed length of 2, and which will iterate until its value is equal to a given value? 我如何使用固定长度2的Scanner类获取Java数组的值,并且它将迭代直到其值等于给定值? For example; 例如; for the following inputs, 对于以下输入,

   A G 

   N H

   D F      

I wrote a for loop to take the values of fixed array road, in which the length is 2 using Scanner class. 我编写了一个for循环来获取固定数组road的值,其中使用Scanner类的长度为2。

 for(int i = 0; i<block.length; i++){
        System.out.println("enter number");
        block[i]=input2.next().charAt(0);
 }

I want to iterate this loop while the user input is {'C','C'}. 我想在用户输入为{'C','C'}时迭代此循环。 THat means the array loop shpuld stop if the inputs are as follow; THat表示如果输入如下,则数组循环应停止;

AG AG

NH NH

DF DF

CC CC

How can i write the code to take user input values using Scanner class and to iterate the array? 我如何编写代码以使用Scanner类获取用户输入值并迭代数组? And the user input values should be copied without replacing them with newly entered values. 并且应该复制用户输入值,而不用新输入的值替换它们。 Thank you! 谢谢!

assuming that your block and input2 variables are already set up and your loop as shown is working, put that loop inside a controller loop 假设您的block和input2变量已经设置并且您的循环如图所示,请将该循环放入控制器循环中

 do {
    for(int i = 0; i<block.length; i++){
        System.out.println("enter number");
        block[i]=input2.next().charAt(0);
    }

 } while (block[0] != 'C" && block[1] != 'C' )

Try this way: 尝试这种方式:

        Scanner input2 = new Scanner(System.in);
        char[] block = new char[2];
        ArrayList<String> arrayList = new ArrayList<String>();
        int i = 0;
        o:
        while (block[0] != 'C' && block[1] != 'C') {
            System.out.println("enter character");
            block[i % 2] = input2.next().charAt(0);
            i++;
            arrayList.add(input2.next());
            if(arrayList.size()>=2){
            if(arrayList.get(arrayList.size()-1).equals("C") && arrayList.get(arrayList.size()-2).equals("C"))
            {
                break o;
            }
            }
        }
   System.out.println(arrayList);

All you need is this 您需要的就是这个

char[] block = new char[2];

while (block[0] != 'C' && block[1] != 'C') {
    System.out.println("enter number");
    block[0]=input2.next().charAt(0);
    System.out.println("enter number");
    block[1]=input2.next().charAt(0);
}

I assume the following from your question 我从你的问题中假设以下

  1. You have an array of fixed length into which you would like to read values using a Scanner 您有一个固定长度的数组,您想使用扫描仪在其中读取值
  2. Once you read values into the array,you would like to compare this array with values from another array and do something if the input array matches your array. 将值读入数组后,您想将此数组与另一个数组中的值进行比较,如果输入数组与您的数组匹配,则可以执行一些操作。

This is a simple program that does this: 这是一个执行以下操作的简单程序:

    String[] scannedValues=new String[2];
    String[] matchValue={"X","Y"};
    boolean isMatched=false;
    Scanner s=new Scanner(System.in);
    while(!isMatched)
    {

        for(int i=0;i<scannedValues.length;i++)
        {
            scannedValues[i]=s.nextLine();
        }
        for(int i=0;i<scannedValues.length;i++)
        {
            if(matchValue[i].equals(scannedValues[i]))
                isMatched=true;
            else
                isMatched=false;
        }
            if(isMatched)
                s.close();
    }

You can use some of the Scanner methods such as nextInt() etc for finding various types of values.You can also pass regular expressions to the Scanner such as next("[AZ]{1}") However,in case you use a regular expression be aware that a mismatch between the input provided by the user and your expression will cause an InputMismatchException. 您可以使用某些Scanner方法(例如nextInt()等)来查找各种类型的值。还可以将正则表达式传递给Scanner,例如next("[AZ]{1}")但是,如果使用正则表达式请注意,用户提供的输入与您的表达式之间的不匹配将导致InputMismatchException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM