数组猜谜游戏 - 如何设置和获取数组?
Array Guessing game - How to set and get an array?
我正在尝试创建一个猜谜游戏,用户在其中输入姓名并且
提示选号 5 次。我试图将数字存储在一个数组中,并使用 set 和 get 方法打印出数组的属性。我已经为名称实现了这一点,但我无法设置和获取数组。
import java.util.Scanner;
public class StudentStore
{
private String n1;//store's the player's name
//private int [] s10; //stores the player's score
private int i;
private int noOfPlayers = 5;
private int[] s10 = new int[noOfPlayers];
Scanner kboardIn = new Scanner(System.in);
private int index;
int newInt = 0;
public StudentStore(String n1, int [] s10, int i )
{
this.n1 = n1;
this.s10[i] = s10[i];
this.setName(n1);
//this.i = i;
this.setScore(s10,i);
//this.index = index;
// PlayerScore.incNumberScores();
}
public void setScore( int []s10, int i)
{
//takes an integer score, sets variables and checks topScore
this.s10[i] = s10[i];
//this.checkAndSetTopScore();
}
public int [] getScore()
{
return this.s10;
}
public void setName(String n1)
{
this.n1 = n1;
}
public String getName()
{
return this.n1;
}
}
import java.util.Scanner;
public class StudentTester
{
public static void main(String args[])
{
Scanner kboardIn = new Scanner(System.in);
int noOfStudents;
System.out.print("Enter the number of students: ");
noOfStudents = kboardIn.nextInt();
//int[] noOfStudents;
//noOfStudents = new int[100];
StudentStore[] student1 = new StudentStore[noOfStudents];
//int s1 = 0;
int noOfPlayers = 5;
int[] s10 = new int[noOfPlayers];
//s10 = 0;
String n1 = "something";
int i = 0;
for (int index = 0; index < student1.length; index++)
student1[index] = new StudentStore(n1, s10, i);
for (int index = 0; index <= student1.length; index++)
{
System.out.print("What is name of student no "+(index+1)+" ?");
n1 = kboardIn.next();
student1[index].setName(n1);
System.out.print("What is mark for student no "+(index+1)+" ?");
for(i=0; i < noOfPlayers; i++)
{
s10[i] = kboardIn.nextInt();
student1[index].setScore(s10, i);
System.out.println("This is it" + s10[i]);
}
System.out.println(student1[index].getScore( ));
System.out.println(student1[index].getName());
System.out.println(index);
System.out.println(s10[index]);
System.out.println(i);
//System.out.println(s10[]);
}
for (int index = 0; index < student1.length; index++)
System.out.println("\nTotal Mark for " + student1[index].getName()+ " is\t" + student1[index].getScore());
}
}
System.out.println(student1[index].getScore());您正在获取数组,但打印数组不会在数组中提供存储值。您应该遍历数组并单独打印所有值。
感谢您的回复。在程序中 System.out.println(student1[index].getScore()) 没有打印数字。它正在打印 [I@137c6OD。我不确定如何遍历数组,但我会查找如何进行。非常感谢
看来你想知道如何遍历数组。您可以通过以下方式实现它:
String[] strings = {"hi","yo","test"};
for(String s : strings){
System.out.println(s);
}
我正在尝试创建一个猜谜游戏,用户在其中输入姓名并且
提示选号 5 次。我试图将数字存储在一个数组中,并使用 set 和 get 方法打印出数组的属性。我已经为名称实现了这一点,但我无法设置和获取数组。
import java.util.Scanner;
public class StudentStore
{
private String n1;//store's the player's name
//private int [] s10; //stores the player's score
private int i;
private int noOfPlayers = 5;
private int[] s10 = new int[noOfPlayers];
Scanner kboardIn = new Scanner(System.in);
private int index;
int newInt = 0;
public StudentStore(String n1, int [] s10, int i )
{
this.n1 = n1;
this.s10[i] = s10[i];
this.setName(n1);
//this.i = i;
this.setScore(s10,i);
//this.index = index;
// PlayerScore.incNumberScores();
}
public void setScore( int []s10, int i)
{
//takes an integer score, sets variables and checks topScore
this.s10[i] = s10[i];
//this.checkAndSetTopScore();
}
public int [] getScore()
{
return this.s10;
}
public void setName(String n1)
{
this.n1 = n1;
}
public String getName()
{
return this.n1;
}
}
import java.util.Scanner;
public class StudentTester
{
public static void main(String args[])
{
Scanner kboardIn = new Scanner(System.in);
int noOfStudents;
System.out.print("Enter the number of students: ");
noOfStudents = kboardIn.nextInt();
//int[] noOfStudents;
//noOfStudents = new int[100];
StudentStore[] student1 = new StudentStore[noOfStudents];
//int s1 = 0;
int noOfPlayers = 5;
int[] s10 = new int[noOfPlayers];
//s10 = 0;
String n1 = "something";
int i = 0;
for (int index = 0; index < student1.length; index++)
student1[index] = new StudentStore(n1, s10, i);
for (int index = 0; index <= student1.length; index++)
{
System.out.print("What is name of student no "+(index+1)+" ?");
n1 = kboardIn.next();
student1[index].setName(n1);
System.out.print("What is mark for student no "+(index+1)+" ?");
for(i=0; i < noOfPlayers; i++)
{
s10[i] = kboardIn.nextInt();
student1[index].setScore(s10, i);
System.out.println("This is it" + s10[i]);
}
System.out.println(student1[index].getScore( ));
System.out.println(student1[index].getName());
System.out.println(index);
System.out.println(s10[index]);
System.out.println(i);
//System.out.println(s10[]);
}
for (int index = 0; index < student1.length; index++)
System.out.println("\nTotal Mark for " + student1[index].getName()+ " is\t" + student1[index].getScore());
}
}
System.out.println(student1[index].getScore());您正在获取数组,但打印数组不会在数组中提供存储值。您应该遍历数组并单独打印所有值。
感谢您的回复。在程序中 System.out.println(student1[index].getScore()) 没有打印数字。它正在打印 [I@137c6OD。我不确定如何遍历数组,但我会查找如何进行。非常感谢
看来你想知道如何遍历数组。您可以通过以下方式实现它:
String[] strings = {"hi","yo","test"};
for(String s : strings){
System.out.println(s);
}