简体   繁体   English

我不能在其中输入高于9的值

[英]I can't input values higher than 9 into this

I think this works perfectly when I input values lower than 9. But whenever I input a value higher than 9, program throws ArrayIndexOutOfBoundsException. 我认为当我输入的值小于9时,这是完美的。但是,当我输入的值大于9时,程序将引发ArrayIndexOutOfBoundsException。 I cant figure out which causes this problem. 我不知道是什么引起了这个问题。 Can someone simply explain me this problem and what is the solution for this? 有人可以简单地向我解释这个问题,对此有什么解决方案?

import java.util.Scanner;
public class Main{
public static void main(String[] args) {
    int[][] twoDAarray = new int[3][3];

    Scanner input = new Scanner(System.in);
    System.out.println("Enter values: ");
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            twoDAarray[i][j] = input.nextInt();
        }
    }

    System.out.println(checkLoShuSquare( twoDAarray ));
}

public static boolean checkLoShuSquare( int[][] twoDAarray ) {
    boolean[] isUnique = new boolean[twoDAarray.length*twoDAarray[0].length+1];
    for ( int i = 0; i < twoDAarray.length; i++ ) {
       for ( int j = 0; j < twoDAarray[0].length; j++ ) {
         if ( isUnique[twoDAarray[i][j]] ){
            return false;
         }
         isUnique[twoDAarray[i][j]] = true;
       }
    }

    int[] lessThan9 = new int[twoDAarray.length*twoDAarray[0].length+1];
    for ( int i = 0; i < twoDAarray.length; i++ ) {
        for ( int j = 0; j < twoDAarray[0].length; j++ ) {
            if (lessThan9[twoDAarray[i][j]] <= 9){
                return true;
            }
    }
  }
    return true;
 }
}

The isUnique array has size 10, instead it should be sized by the largest integer that has been inserted by the user. isUnique数组的大小为10,而应由用户插入的最大整数确定大小。

Of course, there are better ways to check for duplicate numbers of arbitrary size, like using a Set<Integer> . 当然,还有更好的方法来检查任意大小的重复数字,例如使用Set<Integer>

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

相关问题 我无法向数组输入值 - I can't input values to the array Java XMLStreamReader不能具有更高Unicode平面的属性值吗? - Can't Java XMLStreamReader have attribute values with higher Unicode planes? JSP:我可以渲染比当前编写器输出更高的字符串编写器吗? - Jsp: can i render out a stringwriter higher than the current writer output? Java-在BST中返回高于随机输入的所有元素 - Java - returning all elements in a BST higher than a random input 当我使用bluej尝试从数组列表中返回高于目标值的值时,它说错误: <identifier> 预期 - When I use bluej to try and return values higher than my target value from my Array List it says Error: <identifier> expected 从都比预定义距离更近的数组中查找更高的值 - Finding higher values from arrays that are all closer than predefined distances 如何检查数组中的所有值是否都超过特定值? - How to check if all values in an array are higher than a specific amount? 在BST中查找比给定值高的数量 - Find number of higher values than the given value in BST 无法检查用户输入的数字是否小于某个数字 - Can't check if user input is smaller than some number WebDriver无法在Firefox 4及更高版本上运行测试 - WebDriver can't run tests on Firefox 4 and higher
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM