1

嗨,我对 java 很陌生,我目前正在尝试创建一个学生姓名和标记菜单,但我的 add 方法有问题,我认为这与我的数组有关,但我想不通出来,任何帮助将不胜感激。

下面是我的unitResult方法

public class UnitResults 
{
    private String unitTitle;
    private String [] fName;
    private String [] surname;
    //private String [] UnitResults;
    private int [] Marks;
    private int Mark;

    private int pointer ;

    private static String course = "HND Computing"; 

    public UnitResults(int Size,String title)
    {            
        this.fName = new String [Size];
        this.surname = new String [Size];
        this.Marks = new int [Size];
        pointer = 0;

        fName[pointer] = "Daniel";
        surname[pointer] = "Scullion";

        Marks[pointer] = 60;
        unitTitle  = title;

        pointer ++;
    }

    public Boolean add( String tempfName, String tempsName, int newGrade)
    { 
        if (pointer == fName.length)
        {
            System.out.println("The Students Database is full");
            return false;
        }

        else
        {
            fName [pointer] = tempfName;
            surname [pointer]  = tempsName;
            Marks[pointer] = newGrade;

            pointer ++;
            return true;
        }
    }// end Add

但是当我尝试使用下面的菜单系统添加它时

int option = 0;
option = menuSystem();

while (option != 6)
{
    System.out.println("");
    switch(option)
    {
    case 1:
        System.out.println(" Please Enter The Students First Name");
        String tempfName = keyb.nextLine();

        System.out.println("Please Enter The Students Last Name");
        String tempsName = keyb.nextLine();

        System.out.println(" Please Enter The Students Mark");
        int newGrade = keyb.nextInt();
        myUnit.add(tempfName, tempfName,newGrade);   

        break;

当我输入选项 1 时,我得到的输出是:

Please Enter The Students First Name

Please Enter The Students Last Name

任何想法这里有什么问题已经搜索了很长时间,可能很简单,但我不知道:/

编辑:下面是我的菜单类

import java.util.Scanner;

public class MenuResults {
    static Scanner keyb = new Scanner(System.in);

    public static int menuSystem()
    {
        System.out.println("*********************************");
        System.out.println("                                 ");
        System.out.println("1.Add New Student");
        System.out.println("2.Display Students Details");
        System.out.println("3.Delete a Students");
        System.out.println("4.Update Student Details");
        System.out.println("5.Sort Students By Mark");
        System.out.println("6.Sort Students By Surname");
        System.out.println("7.Search For A Student");
        System.out.println("                                 ");
        System.out.println("**********************************");

        System.out.print("\n Enter choice:");
        int option = keyb.nextInt();
        return option;
    }

    public static void main(String[] args) {
        UnitResults myUnit = new UnitResults(3, "Java");

        int option = 0;
        option = menuSystem();

        while (option != 6)
        {
            System.out.println("");
            switch(option)
            {
            case 1:                   

                System.out.println(" Please Enter The Students First Name");
                String tempfName = keyb.nextLine();

                System.out.println("Please Enter The Students Last Name");
                String tempsName = keyb.nextLine();

                System.out.println(" Please Enter The Students Mark");
                int newGrade = keyb.nextInt();
                myUnit.add(tempfName, tempsName,newGrade);   

                break;
            case 2:
                myUnit.display();
                break;
            case 3:

                break;
            case 4:

                break;
            case 5:

            case 6:

                break;
            default:
                System.out.println(" Invalid Entry");
            }//end switch 
        }
    }
}

有我要求的整个菜单课。

编辑:当我忘记用户输入并使用硬输入时:myUnit.add("John","tommy",12);

我得到"Student Database is full"了大约一百次..

4

2 回答 2

0

大胆猜测,当您使用菜单并键入时,1回车ENTER符 ( \n)ENTER仍然存在于您的ScannerafternextInt()中。

因此,下一次调用readLine()将使用\n(remaining ENTER) 作为该行,并且不会等待用户输入。

可能的更正:

public static int menuSystem()
{
    final Scanner keyb = new Scanner(System.in);
    // ...
}

public static void main(String[] args) {
    final UnitResults myUnit = new UnitResults(3, "Java");
    int option = menuSystem();
    while (option != 6) {
        final Scanner keyb = new Scanner(System.in);
        // ...
        option = menuSystem();
    }
}

并删除静态kbd声明

于 2012-12-09T20:20:10.203 回答
0

更好的解决方案是在调用keyb.nextLine()后简单地调用keyb.nextInt()以处理行尾标记。很简单地改变这个:

System.out.println(" Please Enter The Students Mark");
int newGrade = keyb.nextInt();
myUnit.add(tempfName, tempsName,newGrade);  

对此:

System.out.println(" Please Enter The Students Mark");
int newGrade = keyb.nextInt();
keyb.nextLine();  // ****** add this *******
myUnit.add(tempfName, tempsName,newGrade);  
于 2012-12-09T21:20:31.523 回答