间距为

时间:2018-05-08 23:47:04

标签: java arrays string java.util.scanner

我的程序会提示用户他们班上有多少孩子。输入号码后,他们将输入所有学生的姓名(第一名)和(最后一名)。因此我输入了扫描Next Line语句而不是scan.next。因为你输入的这个号码,程序会提示你少一个。请帮忙。

 public class studentRoster {

public static void main(String[] args) {

     Scanner scan = new Scanner (System.in);


     String [] students;
     int size;

     System.out.println("Enter the amount of students in your class: ");
     size = scan.nextInt();

     students = new String[size];

     for (int i = 0; i < students.length; i++ ){
            System.out.println("Enter a student name: ");
            students [i] = scan.next();


       }

                 System.out.println("Student Roster");

     for ( int i = 0;  i < students.length; i++ ){
            System.out.println(students[i]);

           }
        }
     }

3 个答案:

答案 0 :(得分:2)

使用scan.next()只能捕获遇到的第一个空格,因此如果用户同时输入名字和姓氏,您将要使用.nextLine()。

要使此代码生效,请在将scan.nextLine();分配给用户输入后添加size。然后,将students [i] = scan.next();更改为students [i] = scan.nextLine();

您需要执行此操作的原因是因为.nextInt()没有收到用户输入的最后一行换行符,因此您需要调用.nextLine()来解释该问题

public class StudentRoster {

public static void main(String[] args) {

     Scanner scan = new Scanner (System.in);


     String [] students;
     int size;

     System.out.print("Enter the amount of students in your class: ");
     size = scan.nextInt();
     scan.nextLine();
     students = new String[size];

     for (int i = 0; i < students.length; i++ ){
            System.out.print("Enter a student name: ");
            students [i] = scan.nextLine();

       }

                 System.out.println("Student Roster");

     for ( int i = 0;  i < students.length; i++ ){
            System.out.println(students[i]);

           }
        }
     }

测试输出

Enter the amount of students in your class: 4 Enter a student name: john Q Enter a student name: albert E Enter a student name: tyler D Enter a student name: mickey M Student Roster john Q albert E tyler D mickey M

答案 1 :(得分:0)

问题在于这一行

size = scan.nextInt();

因为 nextInt()方法不消耗所有输入缓冲区,它会留下最后的(\ n)字符。当你之后调用nextLine()时,它不会等待用户输入任何东西,但它会消耗缓冲区中留下的(\ n)字符,作为前一个 nextInt的残留()方法
所以要纠正这个,你有两个选择:

  1. 在每个scan.nextInt()方法之后直接添加scan.nextLine()以消耗(\ n)
    size = scan.nextInt(); scan.nextLine(); students = new String[size]; //your code
  2. 获取字符串大小,然后将其转换为int
    String temp = scan.nextLine(); int size = Integer.parseInt(temp);

答案 2 :(得分:0)

我建议您使用更适合您目的的对象(我认为保存数据和提高可读性更容易):

import java.util.Scanner;

public class StudentRoster {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        Student[] students;
        int size;
        String name;
        String lastname;

        System.out.println("Enter the amount of students in your class: ");
        size = scan.nextInt();

        students = new Student[size];
        Student student;
        for (int i = 0; i < students.length; i++) {
            student = new Student();
            System.out.println("Enter a student name: ");
            name = scan.next();

            System.out.println("Enter a student lastname: ");
            lastname = scan.next();

            student.setName(name);
            student.setLastname(lastname);
            students[i] = student;
        }

        System.out.println("Student Roster");
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].getName());
            System.out.println(students[i].getLastname());
        }

    }

    static class Student {
        String name;
        String lastname;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getLastname() {
            return lastname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    }
}