[英]how to display the input from another class(java)
我正在开发有关创建学生记录系统的硬件,但我不知道为什么我的代码无法显示所有输入。 你们能给我一些解决这个问题的建议吗? 我的预期输出是:0:名字,姓氏,身份证,学生编号,分数
这是我的代码:
import java.util.Scanner;`
public class semifinal{
static String[][] records = new String[50][50];
static int num = 0;
public static void main(String args[]){
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to the Student record management system!");
System.out.println();
int i, n = 0;
while(true){
System.out.println("enter 'input' for inputting student's records");
System.out.println("enter 'update' for updating the student's records");
System.out.println("enter 'search' for searching the student's records");
System.out.println("enter 'display' for displaying all students according to the final exam scores.");
String word = kb.nextLine();
if(word.equals("input")){
System.out.println("Input student["+num+"]:");
System.out.println("input <first name> ");
String firstname = kb.nextLine();
System.out.println("input <last name> ");
String lastname = kb.nextLine();
System.out.println("input <ID> ");
String id = kb.nextLine();
System.out.println("input <Student Number>");
String num = kb.nextLine();
insert(firstname, lastname, id, num);
}else if(word.equals("update")){
System.out.println("Enter the student number to update the student records");
String num = kb.nextLine();
System.out.println("Update the final exam score:");
String score = kb.nextLine();
update(num, score);
}else if(word.equals("search")){
System.out.println("Enter the student number to search the student records");
String number = kb.nextLine();
String[] input = search(number);
System.out.println();
System.out.println(input[0]+" "+input[1]+" "+input[2]+" "+input[3]+" "+input[4]+" ");
}else if(word.equals("display")){
Display();
}else{
System.out.println();
System.out.println("Please enter a valid input again!");
}
}
}
public static void insert(String firstname, String lastname, String id, String num){
for(int i = 0; i<records.length; i++){
records[i][0] = firstname;
records[i][1] = lastname;
records[i][2] = id;
records[i][3] = num;
i++;
}
}
public static void update(String num, String score){
for(int i = 0; i<records.length; i++){
records[i][4] = score;
String[] update = search(num);
update[4] = score;
}
}
public static String[] search(String number){
for(int i = 0; i<records.length; i++){
if(number.equals(records[i][3])){
return records[i];
}
}
return null;
}
public static void Display(){
sort();
for(int i = 0; i<num; i++){
System.out.println(num+" "+records[i][0]
+" "+records[i][1]+" "+records[i][2]+" "+records[i][3]+" "+records[i][4]);
}
}
public static void sort(){
int[] sort = new int[num];
for(int i = 0; i<num; i++){
sort[i] = Integer.parseInt(records[i][4]);
}
for(int i = 0; i<num-1; i++){
for(int j = i+1; j<num; j++){
if(sort[j]<sort[i]){
int temp = sort[j];
String[] x = records[j];
records[j] = records[i];
records[i] = x;
sort[j] = sort[i];
sort[i] = temp;
}
}
}
}
我的代码有什么问题吗? 我不知道如何解决它。
您正在隐藏num
变量
您已将其设置为字段
int num = 0;
但是在您的代码中,您也有
String num = kb.nextLine();
可能应该用
num = Integer.valueof(kb.nextLine());
该代码存在许多逻辑错误。 我在评论中强调了其中一些。 我已经修复了部分问题,您可以继续工作。
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to the Student record management system!");
System.out.println();
int i, n = 0;
while(true){
System.out.println("enter 'input' for inputting student's records");
System.out.println("enter 'update' for updating the student's records");
System.out.println("enter 'search' for searching the student's records");
System.out.println("enter 'display' for displaying all students according to the final exam scores.");
String word = kb.nextLine();
if(word.equals("input")){
System.out.println("Input student["+ ++num +"]:");
System.out.println("input <first name> ");
String firstname = kb.nextLine();
System.out.println("input <last name> ");
String lastname = kb.nextLine();
System.out.println("input <ID> ");
String id = kb.nextLine();
System.out.println("input <Student Number>");
String studNum = kb.nextLine();
insert(firstname, lastname, id, studNum);
}else if(word.equals("update")){
System.out.println("Enter the student number to update the student records");
String num = kb.nextLine();
System.out.println("Update the final exam score:");
String score = kb.nextLine();
update(num, score);
}else if(word.equals("search")){
System.out.println("Enter the student number to search the student records");
String number = kb.nextLine();
String[] input = search(number);
System.out.println();
System.out.println(input[0]+" "+input[1]+" "+input[2]+" "+input[3]+" "+input[4]+" ");
}else if(word.equals("display")){
Display();
}else{
System.out.println();
System.out.println("Please enter a valid input again!");
}
}
}
插入时无需循环。
public static void insert(String firstname, String lastname, String id, String studNum){
int index = num -1;
records[index][0] = firstname;
records[index][1] = lastname;
records[index][2] = id;
records[index][3] = studNum;
}
无需在显示中打印索引4(注释排序,因为没有时间修复该索引)。
public static void Display(){
//sort();
for(int i = 0; i<num; i++){
System.out.println(num+" "+records[i][0]
+" "+records[i][1]+" "+records[i][2]+" "+records[i][3]);
}
}
现在它将至少打印记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.