繁体   English   中英

无法从 Array 中获取真正的价值,它只是说“NULL”

[英]can't get the real value out of Array, it just says “NULL”

当我尝试从 Array VoteListVVD中获取值时,它显示“NULL”,但我认为其中应该有一些东西,而不是 NULL。 Package中的Line72选了问题! (只是为了概览性,我把其他方排除在外)

怎么了?

package Elections;

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class Voting
{
    Scanner scanner = new Scanner(System.in);   

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

    public Stemmen()
    {
        System.out.println("Casting 50 random votes (Y/N)?");
        System.out.println();

        String vote = scanner.nextLine();   

        switch (vote) 
        {
            case ("Y"):
            {
                int candidate=0;

                for (int i=1; i<=50; i++)
                {
                    int voteparty = ThreadLocalRandom.current().nextInt(0,3);

                    switch (voteparty)
                    {
                    case (0):
                        candidate = ThreadLocalRandom.current().nextInt(0,5);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty]candidate]+1;

                    break;
                    case (1):   
                        candidate = ThreadLocalRandom.current().nextInt(0,4);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;

                    break;
                    case (2):   
                        candidate = ThreadLocalRandom.current().nextInt(0,6);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;
                    }
                }
            }                   
        }

        for (int j=0; j<6; j++)
        {
LINE72  System.out.format(VoteList.VoteListVVD[j][0]+"\n");  \\This results in NULL?????
        System.out.println(ReSult.result[2][j]+" ");         \\This works!!! (no question about this)
        }
    }   
}

package Elections;

public class Votelist
{
    static String[][] VoteListVVD  = new String [6][1];

    public VoteList()
    {
        VoteListVVD[0][0] = "Lubbers";
        VoteListVVD[1][0] = "Kok";
        VoteListVVD[2][0] = "Hans";
        VoteListVVD[3][0] = "Paula";
        VoteListVVD[4][0] = "Sientje";
        VoteListVVD[5][0] = "Martie";
    }
}

我认为问题在于您的Stemmen -Class 中还没有Votelist 尝试做Votelist votelist = new Votelist(); 在您从中获取VoteListVVD之前创建一个 Votelist。

package Elections;

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class Voting
{
    Scanner scanner = new Scanner(System.in);   

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

    public Stemmen()
    {
        System.out.println("Casting 50 random votes (Y/N)?");
        System.out.println();

        String vote = scanner.nextLine();   
        Votelist votelist = new Votelist();

        switch (vote) 
        {
            case ("Y"):
            {
                int candidate=0;

                for (int i=1; i<=50; i++)
                {
                    int voteparty = ThreadLocalRandom.current().nextInt(0,3);

                    switch (voteparty)
                    {
                    case (0):
                        candidate = ThreadLocalRandom.current().nextInt(0,5);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty]candidate]+1;

                    break;
                    case (1):   
                        candidate = ThreadLocalRandom.current().nextInt(0,4);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;

                    break;
                    case (2):   
                        candidate = ThreadLocalRandom.current().nextInt(0,6);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;
                    }
                }
            }                   
        }

        for (int j=0; j<6; j++)
        {
        System.out.format(votelist.VoteListVVD[j][0]+"\n");  
        System.out.println(ReSult.result[2][j]+" "); 
        }
    }   
}

暂无
暂无

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

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