编程“添加方法”数组似乎不起作用

时间:2012-12-09 19:55:53

标签: java

嗨,我是java的新手,我正在尝试创建一个学生名称和标记菜单,但我的添加方法有问题,我认为这与我的阵列有关,但我可以'弄明白,任何帮助将不胜感激。

以下是我的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"约一百次..

2 个答案:

答案 0 :(得分:0)

狂野猜测,当您使用菜单并键入 1 然后 ENTER 回车符(\n ENTER 调用ScannernextInt()仍然存在。

因此,对readLine()的下一次调用将使用\n(剩余 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声明

答案 1 :(得分: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);