簡體   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