Java //循环某些扫描仪输入x次并打印空

时间:2016-03-15 16:14:26

标签: java

package test2;

public class Student {

private String Username;
private String Game;
private int score;
private int time;
private String Game2;
private int score2;
private int time2;

public Student() {
    this.Username = null;
    this.Game = null;
    this.score = -1;
    this.time = -1;
    this.Game2 = null;
    this.score2 = -1;
    this.time2 = -1;
}

public Student(String nName, String nGame, int nScore, int nTime,      String nGame2, int nScore2, int nTime2) {
    this.Username = nName;
    this.Game = nGame;
    this.score = nScore;
    this.time = nTime;
    this.Game = nGame2;
    this.score = nScore2;
    this.time = nTime2;
}

public void setUsername(String newUsername) {
    this.Username = newUsername;
}

public void setGame(String newGame) {
    this.Game = newGame;
}

public void setScore(int newScore) {
    this.score = newScore;
}

public void setTime(int newTime) {
    this.time = newTime;
}

public void setGame2(String newGame2) {
    this.Game = newGame2;
}

public void setScore2(int newScore2) {
    this.score = newScore2;
}

public void setTime2(int newTime2) {
    this.time = newTime2;
}

public String getUsername() {
    return Username;
}

public String getGame() {
    return Game;
}

public int getScore() {
    return score;
}

public int getTime() {
    return time;
}
public String getGame2() {
    return Game2;
}

public int getScore2() {
    return score2;
}

public int getTime2() {
    return time2;
}
@Override
public String toString() {
    return "Username: " + this.getUsername() + 
           ", Game: " + this.getGame() +
           ", Time: " + this.getScore() +
           ", Achievement Score: " + this.getTime() +
           ", Game: " + this.getGame2() +
           ", Time: " + this.getScore2() +
           ", Achievement Score: " + this.getTime2();

}

}

package test2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);



    List<Student> Students = new ArrayList();


    Student student1 = new Student();

    student1.setUsername("Bob Marley");
    student1.setGame("GTA 1");
    student1.setScore(1200);
    student1.setTime(150);
    student1.setGame2("GTA 2");
    student1.setScore2(1200);
    student1.setTime2(150);

    Students.add(student1);

    Student student2 = new Student();

    student2.setUsername("Bill Harvey");
    student2.setGame("Minecraft");
    student2.setScore(12001);
    student2.setTime(15);
    student2.setGame2("SHAFT");
    student2.setScore2(12001);
    student2.setTime2(15);

    Students.add(student2);

    Student student3 = new Student();

    student3.setUsername("Dan Marley");
    student3.setGame("The Division");
    student3.setScore(12000);
    student3.setTime(150);
    student3.setGame2("Minecraft");
    student3.setScore2(12001);
    student3.setTime2(15);

    Students.add(student3);


    System.out.println("Add new students: ");
    System.out.println("Enter number of students to add: ");
    int countStudents = input.nextInt();

    for (int i = 0; i < countStudents; i++) {
        Student newStudents = new Student();


        System.out.println("Enter details for student: " + (i + 1));

        System.out.println("Enter Username: ");
        newStudents.setUsername(input.next());

        System.out.println("Enter Game: ");
        newStudents.setGame(input.next());

        System.out.println("Enter Achievement Score: ");
        newStudents.setScore(input.nextInt());

        System.out.println("Enter Time played (Minutes): ");
        newStudents.setTime(input.nextInt());

        System.out.println("Enter Game 2: ");
        newStudents.setGame2(input.next());

        System.out.println("Enter Achievement Score: ");
        newStudents.setScore2(input.nextInt());

        System.out.println("Enter Time played (Minutes): ");
        newStudents.setTime2(input.nextInt());


        Students.add(newStudents);
    }
    System.out.println(Students); 
}
} 

到目前为止,这是我的编码,我遇到的问题是控制台返回&#34; null&#34;从arraylist,每个用户应该有2个输入游戏,但第二个游戏返回为null,我不能看到问题是什么。

Add new students: 
Enter number of students to add: 
0
[Username: Bob Marley, Game: GTA 2, Time: 1200, Achievement Score: 150,   Game: null, Time: -1, Achievement Score: -1, Username: Bill Harvey, Game:   SHAFT, Time: 12001, Achievement Score: 15, Game: null, Time: -1,  Achievement Score: -1, Username: Dan Marley, Game: Minecraft, Time: 12001, Achievement Score: 15, Game: null, Time: -1, Achievement Score: -1]

3 个答案:

答案 0 :(得分:1)

您只是一次又一次地将属性设置为单个对象student1。因此,对于其他对象,它显示null

我认为你需要做

    Student student1 = new Student();

    student1.setUsername("Bob Marley");
    student1.setGame("GTA V");
    student1.setScore(1200);
    student1.setTime(150);

    students.add(student1);

    Student student2 = new Student();

    student2.setUsername("Bill Harvey");
    student2.setGame("Minecraft");
    student2.setScore(12001);
    student2.setTime(15);

    students.add(student2);

    student student3 = new Student();

    student3.setUsername("Dan Marley");
    student3.setGame("The Division");
    student3.setScore(12000);
    student3.setTime(150);

    students.add(student3);

此外,您应该使用lowercaseStartingCamelCase作为变量名称。阅读Naming conventions

答案 1 :(得分:0)

使用空值来引用您的问题。尝试使用input.nextLine()代替input.next()

答案 2 :(得分:0)

有一件事是肯定的。如果在控制台询问您要添加多少学生时输入0,那么主循环将永远不会运行。因此,如果您希望它运行3次,请输入3作为输入。此外,对于学生2和3,您只需重置student1的属性。

Student student2 = new Student();

    student2.setUsername("Bill Harvey");//error occurred here before fix
    student2.setGame("Minecraft");
    student2.setScore(12001);
    student2.setTime(15);

    Students.add(student2);

    Student student3 = new Student();

    student3.setUsername("Dan Marley");//error occurred here before fix
    student3.setGame("The Division");
    student3.setScore(12000);
    student3.setTime(150);

    Students.add(student3);