简体   繁体   English

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

[英]Java // Loop certain scanner input x amount of times and null print

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); 
}
} 

Here is my coding thus far, problem I am having is the console return "null" from the arraylist, each user should have 2 game of input however the second game is returning as null and I can't see what the problem is. 到目前为止,这是我的编码,我遇到的问题是控制台从arraylist返回“ null”,每个用户应该有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]

You are just setting the attributes to a single object only student1 again and again. 您只是一次又一次地将属性设置为仅student1的单个对象。 Hence for other objects it is showing null . 因此,对于其他对象,它显示为null

I think you need to do 我认为你需要做

    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);

Also, You should use lowercaseStartingCamelCase for variable names. 另外,您应该对变量名使用lowercaseStartingCamelCase Read here Naming conventions 在这里阅读命名约定

Refering to your problem with the null values. 使用空值引用您的问题。 Try using input.nextLine() instead of input.next() 尝试使用input.nextLine()而不是input.next()

Well one thing's for sure. 好一件事是肯定的。 If you enter 0 when the console asks how many students you want to add, then your loop in the main will never run. 如果在控制台询问要添加多少学生时输入0,则主循环将永远不会运行。 So if you want it to run 3 times, enter 3 as an input. 因此,如果您希望它运行3次,请输入3作为输入。 Also, for students 2 and 3, you are just resetting the attributes of student1. 同样,对于学生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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM