【发布时间】:2021-03-24 01:42:13
【问题描述】:
import java.util.Scanner;
public class Search {
static Scanner scanner = new Scanner(System.in);
static Scanner kb = new Scanner(System.in);
static Scanner kb2 = new Scanner(System.in);
public static void main (String[] args)
{
int choice;
System.out.print("Choose a number of students: ");
int n = scanner.nextInt();
String name[] = new String[n+1];
String course[] = new String[n+1];
int ID[] = new int[n+1];
for(int i=1;i <= n; i++)
{
System.out.print("Enter ID number " + i + ": ");
ID[i] = scanner.nextInt();
System.out.print("Enter Student name " + i + ": ");
name[i] = kb.nextLine();
System.out.print("Enter Student course " + i + ": ");
course[i] = kb2.nextLine();
System.out.println("----------------------------------------");
}
do
{
choice = menu();
if(choice == 1)
{
sortID(ID);
printValues(ID);
}else if(choice == 2)
{
nameSort(name,n);
printName(name,n);
}else if(choice == 3)
{
}
}while(choice !=0);
}
public static int menu()
{
System.out.print("\n1. Sort by ID\n2. Sort by Name\n3. Search by ID\n4. Search by Name\n5. Search by Course\n6. Display Records In table Form.\nYour Choice: ");
return scanner.nextInt();
}
public static void sortID(int []id)
{
int temp;
int index, counter;
for (counter=0; counter < id.length -1; counter++) {
for (index=0; index < id.length - 1 - counter; index++) {
if (id[index] > id[index+1]) {
temp = id[index];
id[index]=id[index+1];
id[index+1]=temp;
}
}
}
}
public static void printValues (int[]array) {
System.out.println ("\nSorted Id Number: ");
for(int i = 1; i < array.length; i++){
System.out.print ("\n" + array[i]);
}
}
public static void printName (String[]array,int a) {
for (int i = 0; i <= a - 1; i++)
{
System.out.print(array[i] + ", ");
}
}
public static void nameSort(String[] name,int a)
{
String temp;
for (int i = 0; i < a; i++)
{
for (int j = i + 1; j < a; j++) {
if (name[i].compareTo(name[j])>0)
{
temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
}
}
}
排序在 id 上有效,但我的名字有问题,它不会推动冒泡排序并说它为空,我刚刚开始学习这种语言,这将是一个很大的帮助。从昨晚开始,我一直在研究这个问题,我尝试在 if else(choice == 2) 下转移它,但它仍然显示为 null。
选择学生人数:2 输入身份证号码1:123 输入学生姓名 1:Mark JAw 进入学生课程1:JSJS -------------------------------------- 输入身份证号 2:221 输入学生姓名 2:Ak akw 进入学生课程2:jdj ------------------------------------------
1. Sort by ID
2. Sort by Name
3. Search by ID
4. Search by Name
5. Search by Course
6. Display Records In table Form.
Your Choice: 1
Sorted Id Number:
123
221
1. Sort by ID
2. Sort by Name
3. Search by ID
4. Search by Name
5. Search by Course
6. Display Records In table Form.
Your Choice: 2
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.compareTo(String)" because "name[i]" is null
at Search.nameSort(Search.java:95)
at Search.main(Search.java:41)
PS C:\Users\Bingus\Documents\Projects>
【问题讨论】:
-
您不应该有三个扫描仪都指向 System.in。这是一个等待突袭的错误。
-
我也要改那个,唯一的问题是按名称排序不起作用
-
您应该包含完整的堆栈跟踪。还是这是编译器错误?
-
好的,所以错误表明你不能做你想做的事情,因为有些东西是空的。您是否尝试弄清楚为什么它为空?您是否尝试更改代码以使其不再为空?
-
提示:如果你做
String name[] = new String[n+1];并且n等于3,你认为name会变成什么?它将包含多少元素?初始值是多少?将 3 个字符串存储在其中后会是什么样子?在您写for(int i=1;i <= n; i++)的地方,您认为该循环中使用的i的值是多少?你认为name的有效索引是什么?符合吗?