简体   繁体   English

在代码中找不到错误

[英]Can't find the error in the code

I am writing some code to help teach me how code in java and I have been using arrays. 我正在编写一些代码来帮助教我如何使用Java编写代码,而我一直在使用数组。 I have an error that I can't work out why it is occurring. 我有一个错误,无法弄清为什么会发生。 The Code: 编码:

import java.util.Scanner;
public class pubQuizArray {
private static Scanner kb = new Scanner (System.in);
static String[] questions;
static String[][] answers;
static char[] realAnswers;
static char ans;
static char yn;
static int questionNum;
static int questionNumArray;
static int numQ;
static int score = 0;

public static void writeQuiz()
{
    getQNum();
    getQ();
}

public static void getQNum()
{
    System.out.println("How many Questions?");
    numQ = kb.nextInt();
    questions = new String[numQ];
}

public static void getAns()
{
    questionNumArray = questionNum - 1;
    answers = new String[numQ][];
    System.out.println("What are the answers?");

    System.out.println("a: ");
    answers[questionNumArray][0] = kb.nextLine();

    System.out.println("b: ");
    answers[questionNumArray][1] = kb.nextLine();

    System.out.println("c: ");
    answers[questionNumArray][2] = kb.nextLine();

    System.out.println("d: ");
    answers[questionNumArray][4] = kb.nextLine();

    realAnswers = new char[numQ];
    System.out.println("What is the correct Answer?");
    realAnswers[questionNum] = kb.next().charAt(0);

}

public static void getQ()
{
    questionNum = 0;
    System.out.println("What is the First Question?");
    questions[questionNum] = kb.nextLine();
    getAns();
    questionNum ++;
    while(questionNum < numQ)
    {
        System.out.println("What is the next Question?");
        questions[questionNum] = kb.nextLine();
        getAns();
        questionNum ++;
    }
}

public static void askQ()
{
    questionNum = 0;
    while(questionNum < numQ)
    {
        System.out.println("Q1: " + questions[questionNum]);

        System.out.println("a: " + answers[questionNum][0]);
        System.out.println("b: " + answers[questionNum][1]);
        System.out.println("c: " + answers[questionNum][2]);
        System.out.println("d: " + answers[questionNum][3]);

        ans = kb.next().charAt(0);
        if(ans == realAnswers[questionNum])
        {
            System.out.println("That was correct");
            score ++;
        }
    }
}

public static void menu()

{
    System.out.println("Would you like to write a new Quiz? y/n");
    yn = kb.next().charAt(0);
    while(yn == 'y')
    {
        writeQuiz();
        System.out.println("Would you like to play the Quiz? y/n");
        yn = kb.next().charAt(0);
        while(yn == 'y')
        {
            askQ();
            System.out.println("Would you like to play again? y/n");
            yn = kb.next().charAt(0);
        }
    }
}

public static void main(String[] args)
{
    menu();
}
}

The error is this: 错误是这样的:

Would you like to write a new Quiz? y/n
y
How many Questions?
10
What is the First Question?
What are the answers?
a: 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at pubQuizArray.getAns(pubQuizArray.java:34)
    at pubQuizArray.getQ(pubQuizArray.java:56)
    at pubQuizArray.writeQuiz(pubQuizArray.java:17)

Thanks in advance for any help that you can give. 在此先感谢您提供的任何帮助。 Please bear in mind that this is just a trial program and that I am still learning java. 请记住,这只是一个试用程序,我仍在学习Java。

OK I have another problem this time it says: 好吧,这次我有另一个问题说:

Would you like to write a new Quiz? y/n
y
How many Questions?
1
What is the First Question?
and
What are the answers?
a: a
Exception in thread "main" java.lang.NullPointerException
    at pubQuizArray.getAns(pubQuizArray.java:34)
    at pubQuizArray.getQ(pubQuizArray.java:57)
    at pubQuizArray.writeQuiz(pubQuizArray.java:17)
    at pubQuizArray.menu(pubQuizArray.java:96)
    at pubQuizArray.main(pubQuizArray.java:110)

and i've updated the other previous code. 并且我已经更新了其他先前的代码。

At this point, inside getAns() 此时,在getAns()

questionNumArray = questionNum - 1;
answers = new String[numQ][];
System.out.println("What are the answers?");

System.out.println("a: ");
answers[questionNumArray][0] = kb.nextLine();

questionNumArray contains the value -1 which is an invalid index for an array. questionNumArray包含值-1 ,它是数组的无效索引。

It comes from 它来自

public static void getQ()
{
    questionNum = 0; // set to 0
    System.out.println("What is the First Question?");
    questions[questionNum] = kb.nextLine();
    getAns(); // still 0 
    questionNum ++; // too late
    ...
}

Edit 编辑

The NPE you are getting boils down to NPE归结为

System.out.println("a: ");
answers[questionNumArray][0] = kb.nextLine();

you haven't initialized answers[questionNumArray] so it is null . 您尚未初始化answers[questionNumArray]所以它为null Do the following first 请先执行以下操作

answers[questionNumArray] = new String[someSize]; // someSize should probably be 5 looking at your code

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

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