Java Iterate array with a fixed length and take va

2019-05-27 22:00发布

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? 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(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'}. THat means the array loop shpuld stop if the inputs are as follow;

A G

N H

D F

C C

How can i write the code to take user input values using Scanner class and to iterate the array? And the user input values should be copied without replacing them with newly entered values. Thank you!

4条回答
在下西门庆
2楼-- · 2019-05-27 22:09

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

 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' )
查看更多
爷、活的狠高调
3楼-- · 2019-05-27 22:09

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);
查看更多
迷人小祖宗
4楼-- · 2019-05-27 22:09

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);
}
查看更多
Viruses.
5楼-- · 2019-05-27 22:26

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("[A-Z]{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.

查看更多
登录 后发表回答