Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
猜测用户对java的想法(数组)_Java_Arrays_For Loop - Fatal编程技术网

猜测用户对java的想法(数组)

猜测用户对java的想法(数组),java,arrays,for-loop,Java,Arrays,For Loop,我对编程有点陌生,我们的老师让我们制作一个程序,可以猜测用户使用数组时想到的数字。我这样做: import java.util.Scanner; public class Exercise11 { public static void main(String[] args) { Scanner entrada = new Scanner(System.in); System.out.println("Think a number between 1 a

我对编程有点陌生,我们的老师让我们制作一个程序,可以猜测用户使用数组时想到的数字。我这样做:

import java.util.Scanner;

public class Exercise11 {

    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        System.out.println("Think a number between 1 and 100");
        int array[] = new int[100];

        for (int x = 0; x < 100; x++) {
            array[x] = (int) ((Math.random() * 100) + 1);
        }
        //This allow us to fill the array with random numbers, without caring if they are repeated or not.
        for (int i = 0; i < 100; i++) {
            for (int j = 0; i < 100; j++) {
                while (true) {
                    if (i != j && array[i] == array[j]) {
                        array[j] = (int) ((Math.random() * 100) + 1);
                    } else
                        break;
                }
                //If a number is repeated, this will swap that number with another number.
            }
        }

        //Now we have filled the array. We ask the user:
        for (int y = 0; y < 100; y++) {
            System.out.println("¿Is it your number " + array[y] + "?");
            String respuesta = entrada.next();
            switch (respuesta) {
                case "Yes":
                    System.out.println("I knew it! I only needed " + y + " trys!");
                    break;
                case "No":
                    break;
            }
        }
    }
}

我试过调试它,但我仍在学习如何去做,我找不到错误。有人能帮我确定错误在哪里以及如何修复它吗?非常感谢

内部for循环的条件应为

for (int j = 0; j < 100; j++){
for(int j=0;j<100;j++){

(条件是j<100,而不是i<100)

内部for循环的条件应该是

for (int j = 0; j < 100; j++){
for(int j=0;j<100;j++){

(条件是j<100,不是i<100)

非常感谢!我没有意识到,我检查了四次!现在它工作了,谢谢!编辑:我还意识到try的数量应该是(y+1)。非常感谢!我没有意识到,我检查了四次!现在它工作了,谢谢!编辑:我还意识到try的数量应该是(y+1)。如果您不知道解释此异常的提示:您得到了ArrayIndexOutOfBoundsException-这通常意味着您尝试在数组上使用一个不存在的索引(例如,您的数组为空,您尝试使用第三个元素)。它还告诉您是哪个方法抛出了它(这里是您的main)如果你不知道解释这个异常的提示:你得到了一个ArrayIndexOutOfBoundsException——这通常意味着你试图在数组上使用一个不存在的索引(例如,你的数组是空的,你尝试使用第三个元素)。它还告诉您哪个方法抛出了它(这里是您的main)以及在哪个行号发生了错误(这里是25)。非常感谢,我感谢您的提示。