gpt4 book ai didi

java - 编程 "Add Method"数组似乎不起作用

转载 作者:行者123 更新时间:2023-12-01 04:57:59 26 4
gpt4 key购买 nike

嗨,我对 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);

我收到“学生数据库已满”大约一百次..

最佳答案

大胆猜测,当您使用菜单并输入 1 时,然后 ENTER 回车 (\n) 表示 ENTER调用 nextInt() 后, 仍然存在于您的 Scanner 中。

因此,下次调用 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 声明

关于java - 编程 "Add Method"数组似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13791158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com