【发布时间】:2020-08-06 07:51:12
【问题描述】:
我正在尝试使用 switch case 进行数据搜索和排序。我必须输入数据量及其成员。例如:
数据量://例如:2
输入数据:
data num-1 : //用户输入
data num-2 : //用户输入
选择
案例1://数据搜索
案例2://冒泡排序
案例3://选择排序
案例4://编辑
我停留在案例 4。 我已经尝试了我的代码,但数据量和电流根本没有改变 如果我想更改比以前更大的数据量,则索引超出范围。 这是代码。
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n;
System.out.println("-------------------data search and sort-------------------");
System.out.println("");
System.out.println("");
System.out.println("Hai ");
System.out.print("How many datas do you want? : ");
n=s.nextInt();
int bil[]=new int[n]; //bil is array
System.out.println("input the data:");
for(int i=1;i<=n;i++){
System.out.print(" data -" +i+ " = ");
bil[i-1]=s.nextInt();
}
System.out.println("");
System.out.println("");
while(true){
System.out.println("Displaying data(s) : ");
for(int i: bil){
System.out.print(i+" ");
}
System.out.println("");
System.out.println("Menu:");
System.out.println("\t1. Search data");
System.out.println("\t2. Bubblesort ascending");
System.out.println("\t3. SelectionSort descending");
System.out.println("\t4. Edit");
System.out.println("\t5. Exit");
System.out.print("What's your choice? : ");
int pilih=s.nextInt();
switch(pilih){
case 1:
System.out.println("Enter the number you are looking for = ");
int srch = s.nextInt();
boolean found = false;
for(int index=0; index<bil.length; index++) {
if(bil[index] == srch){
found = true;
}
}
if(found == true) {
System.out.println("Found '"+srch + "' in data collection!");
} else {
System.out.println(srch + "there is no data you're looking for here");
}
System.out.println("");
System.out.println("Displaying data : ");
for(int i: bil){
System.out.print(i+" ");
}
System.out.println("");
System.out.print("Continue? (y/n)");
String conti = s.next();
boolean nue;
switch(con){
case "y":
nue = false;
break;
case "n":
return;
}
break;
case 2:
System.out.println("BubbleSorting is done!");
System.out.println("");
System.out.println("Displaying data(s) :");
int i, j, te;
for (i = 0; i < n; i++)
for (i = 0; i < ( n - 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (bil[j] > bil[j+1]){
te = bil[j];
bil[j] = bil[j+1];
bil[j+1] = te;
}
}
}
for (i = 0; i < n; i++) {
System.out.print(bil[i]);
}
System.out.print("Continue? (y/n)");
conti = s.next();
switch(conti){
case "y":
nue = false;
break;
case "n":
return;
}
break;
case 3:
for (i = 0; i < n; i++){
for (j = i + 1; j < n; j++){
if (bil[i] < bil[j]){
te = bil[i];
bil[i] = bil[j];
bil[j] = te;
}
}
}
System.out.print("SelectionSort is done");
System.out.println("");
System.out.println("Displaying data(s):");
for (i = 0; i < n - 1; i++){
System.out.print(bil[i] + " ");
}
System.out.print(bil[n - 1]);
System.out.println("");
System.out.print("Continue? (y/n)");
conti = s.next();
switch(conti){
case "y":
nue = false;
break;
case "n":
return;
}
break;
case 4:
System.out.print("WARNING!!! the data will be change!");
System.out.print("continue? (y/t)");
String change = s.next();
boolean edit;
switch(change){
case "y":
System.out.print("How many datas do you want? = ");
n=s.nextInt();
System.out.println("Input the data");
for(i=1;i<=n;i++){
System.out.print(" data -" +i+ " = ");
bil[n]=s.nextInt();
edit = false;
}
break;
case "n":
return;
}
break;
}
}
ps:我不要忘记导入 java.util.Scanner
【问题讨论】:
-
当你问“你想要多少数据?”在第 4 种情况下,您是在询问除了最初输入的数据之外要添加的新数据点的数量吗?还是您希望丢弃最初输入的数据并获得一组全新的数据点?
-
我希望丢弃最初输入的数据并获得一组全新的数据点
-
那么请在下方查看我的回答
标签: java arrays netbeans switch-statement