数组返回Null值

时间:2010-06-02 16:28:39

标签: java arrays

Dunno为什么会这样......继承人的编码。

http://pastebin.org/301343

/**********************************\
*                                  *
*       Multiplication Quiz        *
*    Developed and Written by:     *
*          Nick Gibson             *
*             CIT                  *
*       ***Finished Code***        *
*                                  *
\**********************************/

import javax.swing.JOptionPane;
import java.awt.*;
import java.lang.Math.*;
import java.util.*;
import javax.swing.*;
import java.text.*;
import java.text.DecimalFormat.*;
import java.io.*;
import java.lang.Integer.*;



public class TestMaker
{
    public static void main(String[] args)
    {
        int y = 1, x, quesNum = 1, z, zz, qq, g, u = 1;

        while (y == 1)
        {
            String xx = JOptionPane.showInputDialog(null,"How many questions is this test? \n(May be 1 - 99 questions)");
                x = Integer.parseInt(xx);

            if(x > 99 || x < 1)
            {
                JOptionPane.showMessageDialog(null,"Invalid number. Please try again.", "Invalid Number", JOptionPane.ERROR_MESSAGE);
            }
            else
            {

                String[] questions = new String[25];
                String[] answers = new String[25];
                String[] a = new String[25];
                String[] b = new String[25];
                String[] c = new String[25];
                String[] d = new String[25];

                while(u == 1)
                {

                    questions[1] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[1] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[1] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[1] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[1] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[1] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[2] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[2] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[2] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[2] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[2] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[2] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[3] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[3] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[3] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[3] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[3] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[3] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[4] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[4] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[4] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[4] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[4] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[4] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[5] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[5] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[5] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[5] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[5] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[5] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[6] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[6] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[6] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[6] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[6] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[6] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[7] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[7] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[7] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[7] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[7] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[7] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[8] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[8] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[8] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[8] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[8] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[8] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[9] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[9] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[9] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[9] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[9] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[9] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                    questions[10] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+quesNum);
                    a[10] = JOptionPane.showInputDialog(null,"What is the answer for a?");
                    b[10] = JOptionPane.showInputDialog(null,"What is the answer for b?");
                    c[10] = JOptionPane.showInputDialog(null,"What is the answer for c?");
                    d[10] = JOptionPane.showInputDialog(null,"What is the answer for d?");
                    answers[10] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
                    quesNum++;
                    if(quesNum > x)
                    {
                    break;
                    }

                }//while

                    zz = JOptionPane.showConfirmDialog(null,"All questions are completed. \nWould you like to review any questions?", "Question Review", JOptionPane.YES_NO_OPTION);

                    if(zz == JOptionPane.YES_OPTION)
                    {
                        while(u == 1)
                        {
                            String q = JOptionPane.showInputDialog(null,"Which question would you like to review? \n\nCurrently on Question "+quesNum+"\n\nPress 0 to exit.");
                            qq = Integer.parseInt(q);

                            if(qq == 0)
                            {
                                break;
                            }

                            JOptionPane.showMessageDialog(null,"You are now going to edit question "+qq);
                            questions[qq] = JOptionPane.showInputDialog(null,"Question: "+questions[qq]+"\nType in the correct question.");
                            a[qq] = JOptionPane.showInputDialog(null,"A: "+a[qq]+"\nType in answer A");
                            b[qq] = JOptionPane.showInputDialog(null,"B: "+b[qq]+"\nType in answer B");
                            c[qq] = JOptionPane.showInputDialog(null,"C: "+c[qq]+"\nType in answer C");
                            d[qq] = JOptionPane.showInputDialog(null,"D: "+d[qq]+"\nType in answer D");
                            answers[qq] = JOptionPane.showInputDialog(null,"Answer: "+answers[qq]+"\nPlease type the correct answer. \nType in lowercase a - d");

                            JOptionPane.showMessageDialog(null,questions[qq]+"\n\nA: "+a[qq]+"\nB: "+b[qq]+"\nC: "+c[qq]+"\nD: "+d[qq]+"\n\nCorrect Answer: "+answers[qq]);

                        }//while

                    }//if

                if(quesNum > x)
                {
                    JOptionPane.showMessageDialog(null,"That concludes the creation portion of the test. \nIt is now time to take the test.");
                }

                JOptionPane.showMessageDialog(null,"When you ready for the test, press OK.");

                double number1 = (Math.random() * 10);
                double number2 = (Math.random() * 10);
                double number3 = (Math.random() * 10);

                long aa, bb, cc, ss;
                int abc;

                aa = Math.round(number1);
                bb = Math.round(number2);
                cc = Math.round(number3);

                ss = aa;

                String str = Long.toString(ss);
                abc = Integer.parseInt(str);

                JOptionPane.showMessageDialog(null,questions[abc]+"\n\n"+a[abc]+"\n"+b[abc]+"\n"+c[abc]+"\n"+d[abc]);

            }//else

        }//while y = 1

    }//public main

}//public class

我知道编码中有很多重复性...但是因为数组重新调整为null所以我厌倦了搞乱它们。

一切正常,直到到达第224行,返回空值。

3 个答案:

答案 0 :(得分:2)

1

Nick,您是否意识到Java中的数组索引从0开始?

因此:

String[] questions = new String[3];

意味着:

questions[0] = "a";
questions[1] = "b";
questions[2] = "c";

有效,但是:

questions[3] = "d";

不是。

2

这会产生0到10之间的数字我认为:

Math.round(Math.random()*10);

但我认为你只需要1到10,我认为这是:

Math.round((Math.random() * 9) + 1);

答案 1 :(得分:1)

null是数组的有效值。指向数组的指针和数组中的内容都可以为null。我花时间阅读你太长的代码。看起来你的代码没有完全填充数组。首先,你不要在插槽0中放置任何东西(questions[0]总是为空)。

此外,您的代码应该是for循环。您应该将数组分配给允许的问题数,而不是固定数。这样您就可以确定您的数组没有null元素(假设如果用户没有输入任何值,则JOptionPane不会返回null)。

String[] questions = new String[x];
String[] answers = new String[x];
String[] a = new String[x];
String[] b = new String[x];
String[] c = new String[x];
String[] d = new String[x];

for (int i = 0; i < x; i++)
{

  questions[i] = JOptionPane.showInputDialog(null,"Please Enter Question Number "+i);
  a[i] = JOptionPane.showInputDialog(null,"What is the answer for a?");
  b[i] = JOptionPane.showInputDialog(null,"What is the answer for b?");
  c[i] = JOptionPane.showInputDialog(null,"What is the answer for c?");
  d[i] = JOptionPane.showInputDialog(null,"What is the answer for d?");
  answers[i] = JOptionPane.showInputDialog(null,"What is the correct answer to the question. \nType in lowercase a - d.");
}

答案 2 :(得分:0)

你的数组,a,b,c,d,问题,答案被初始化为大小25.这意味着它们最初都保持空值 - 其中25个。您的代码填写索引1..10,然后查看随机问题。

编辑: 根本原因是您如何生成随机数,可以是0,而索引0未填充。数组元素1..10被填充,但0和11..24不是。如果随机数为0,则会出现NullPointerException。

修复是设置从0开始的问题编号,而不是Math.round(),将随机数转换为int,截断法定部分,因此范围为0..9。