"Null" 当我尝试在 java 程序中打印出数组的值时打印出来
"Null" is printed when I try to print out the array's value in java program
我的问题是,当我想打印二维数组的任何元素时,显示的是 Null 而不是数组的元素值。
我该如何解决这个问题?为什么我的数组有成员但我得到这个输出? (我是编程新手)
我试图通过直接写入元素编号来打印出一个数组成员,但仍然存在一些问题,因此随机数不是问题。
我还尝试在 pickCard() 方法中定义一个新数组,以便将 cardList 数组复制到它,但它也没有帮助。
我有 2 个不同的 classes,第一个 class 称为 Tutorial,它包括 main() 方法,另一个叫做 Kortlek,我所有的代码都在那里。
这是教程class
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 class
/*
* 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 个组(Clubs、Diamonds、红心、黑桃)。我在名为 buildCardGame() 的方法中创建了数组。
通过 pickCard() 方法,我尝试通过添加 2 个随机数从 0-3 的颜色和 0-12 的等级来随机选择一张卡片。
但是当我打印出来时,我得到了空值。
首先,需要修改cardlist
的初始化位置
/*
* 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;
}
}
其次,必须调用buildCardGame
方法
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);
}
}
我的问题是,当我想打印二维数组的任何元素时,显示的是 Null 而不是数组的元素值。 我该如何解决这个问题?为什么我的数组有成员但我得到这个输出? (我是编程新手)
我试图通过直接写入元素编号来打印出一个数组成员,但仍然存在一些问题,因此随机数不是问题。 我还尝试在 pickCard() 方法中定义一个新数组,以便将 cardList 数组复制到它,但它也没有帮助。
我有 2 个不同的 classes,第一个 class 称为 Tutorial,它包括 main() 方法,另一个叫做 Kortlek,我所有的代码都在那里。
这是教程class
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 class
/*
* 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 个组(Clubs、Diamonds、红心、黑桃)。我在名为 buildCardGame() 的方法中创建了数组。
通过 pickCard() 方法,我尝试通过添加 2 个随机数从 0-3 的颜色和 0-12 的等级来随机选择一张卡片。 但是当我打印出来时,我得到了空值。
首先,需要修改cardlist
的初始化位置
/*
* 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;
}
}
其次,必须调用buildCardGame
方法
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);
}
}