当我尝试在java程序中打印出数组的值时,会打印“Null”

问题描述 投票:2回答:1

我的问题是,当我想打印出我的2D数组的任何元素时,会显示Null而不是数组的元素值。我该如何解决这个问题?为什么我得到这个输出尽管我的阵列有成员? (我是编程新手)

我试图通过直接写入元素编号打印出一个数组成员,但仍然存在一些问题,因此随机数不是问题。我还尝试在pickCard()方法中定义一个新数组,以便将cardList数组复制到它,但它也没有帮助。

我有两个不同的类,第一个类叫做Tutorial,它包含main()方法,另一个叫做Kortlek,我的所有代码都在那里。

这是Tutorial类

package tutorial;

import java.util.Arrays;
import java.util.Scanner;

public class Tutorial {
    public static void main(String[] args) {

        // Using Scanner class to get in the input from user
        Scanner input = new Scanner(System.in);

        // We initialize our class Kortlek
        Kortlek newKortlek = new Kortlek();

        // Here we choose a nickname for user: 
        System.out.print("Please choose a name: ");
        String Username = input.next();
        String pcName = newKortlek.nickNamePC();


        String userAnswer;
        int userScore = 1;
        int pcScore = 2;
        do {
            System.out.println("You picked up: " + newKortlek.pickCard());
            System.out.println(pcName + " has picked up: "  + newKortlek.pickCard());
            System.out.println("Do you want to continue? write yes or no");
            userAnswer = input.next();

        } while (!userAnswer.equals("no"));
       System.out.println("Your score is: " + userScore);
       System.out.println(pcName + "'s score is: " + pcScore);
    }
}

这是Kortlek课程

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tutorial;

import java.util.Random;

/**
 *
 * @author hezarehee
 */
public class Kortlek {

    Random r = new Random();
    /**
     * In this method we create 2D array in order to group each card color and its cards (1-13)
     */
    String cardList[][] = new String[3][12];

    public String[][] buildCardGame () {
        String[] farg = {"Spader", "Hjarter", "Ruter", "Klover"};
        String[] nummer = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "Ace"};


        for (int i=0; i < farg.length; i++) {
            for (int j=0; j < nummer.length; j++) {
                cardList[i][j] = farg[i] + " " + nummer[j];
            }

        }
        return cardList;
    }

    /**
     * Here we make a method that let computer to choose a name from given names in array
     */
    public String nickNamePC () {
        String[] nickName = {"Daivd", "Rahim", "Michael", "Sara", "Marie", "Jenny"};

        int low = 0; 
        int high = 5; 
        int result =  r.nextInt(high-low) + low;
        String chosenName = nickName[result];
        return chosenName;
    }

    /**
     * Here we each time pick up a card from our 2D Array cardList[][] 
     */
    public String pickCard() {
        int takeColor = r.nextInt(3-1) + 1;
        int takeNumber = r.nextInt(12-1) + 1;

        // we put our random numbers into the array carDList
        String pickedCard = cardList[takeColor][takeNumber];
        return pickedCard;
    }

}

我需要制作一个纸牌游戏(等级和颜色),在这个游戏中用户对抗程序,首先在阵列cardList中,我试图在4组(俱乐部,钻石,心脏,黑桃)中创建52种不同的卡片。我在内部方法中创建了一个名为buildCardGame()的数组。

通过方法pickCard(),我尝试通过在0-3中为颜色添加2个随机数并为等级添加0-12来挑选随机卡。但是当我把它打印出来以便我得到null。

java arrays loops 2d
1个回答
3
投票

First, You need to modify where cardlist is initialized.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tutorial;

import java.util.Random;

/**
 *
 * @author hezarehee
 */
public class Kortlek {

    Random r = new Random();
    /**
     * In this method we create 2D array in order to group each card color and its cards (1-13)
     */
    String cardList[][] = new String[4][13];

    // ArrayIndexOutOfBoundsException
    // String cardList[][] = new String[3][12];

    public String[][] buildCardGame () {
        String[] farg = {"Spader", "Hjarter", "Ruter", "Klover"};
        String[] nummer = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "Ace"};


        for (int i=0; i < farg.length; i++) {
            for (int j=0; j < nummer.length; j++) {
                cardList[i][j] = farg[i] + " " + nummer[j];
            }

        }
        return cardList;
    }

    /**
     * Here we make a method that let computer to choose a name from given names in array
     */
    public String nickNamePC () {
        String[] nickName = {"Daivd", "Rahim", "Michael", "Sara", "Marie", "Jenny"};

        int low = 0;
        int high = 5;
        int result =  r.nextInt(high-low) + low;
        String chosenName = nickName[result];
        return chosenName;
    }

    /**
     * Here we each time pick up a card from our 2D Array cardList[][] 
     */
    public String pickCard() {
        int takeColor = r.nextInt(3-1) + 1;
        int takeNumber = r.nextInt(12-1) + 1;

        // we put our random numbers into the array carDList
        String pickedCard = cardList[takeColor][takeNumber];
        return pickedCard;
    }

}

Second, buildCardGame method must be called

package tutorial;

// import java.util.Arrays; // not used
import java.util.Scanner;

public class Tutorial {
    public static void main(String[] args) {

        // Using Scanner class to get in the input from user
        Scanner input = new Scanner(System.in);

        // We initialize our class Kortlek
        Kortlek newKortlek = new Kortlek();

        // initialize the members
        newKortlek.buildCardGame(); 

        // Here we choose a nickname for user:
        System.out.print("Please choose a name: ");
        String Username = input.next();
        String pcName = newKortlek.nickNamePC();


        String userAnswer;
        int userScore = 1;
        int pcScore = 2;
        do {
            System.out.println("You picked up: " + newKortlek.pickCard());
            System.out.println(pcName + " has picked up: "  + newKortlek.pickCard());
            System.out.println("Do you want to continue? write yes or no");
            userAnswer = input.next();

        } while (!userAnswer.equals("no"));
        System.out.println("Your score is: " + userScore);
        System.out.println(pcName + "'s score is: " + pcScore);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.