如何更改其数据的数量和内容
How to change the amount and the content of its data
我正在尝试使用 switch case 进行数据搜索和排序。我必须输入数据量及其成员。例如:
数据量://例如:2
输入数据:
data num-1 : //用户输入
数据 num-2 : //用户输入
选择
案例一://数据搜索
案例 2://冒泡排序
案例3://selectionsort
案例 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
嘿,这里的问题是您已经将数组声明为
int bil = new int[n];
因此,在第 4 种情况下,当您将另一个 "n" 作为大于前一个 "n" 的输入时,它抛出 ArrayIndexOutOfBoundsException.You 需要在 it.Add 中存储更多值之前增加数组的大小在将新的“n”作为输入后,在第 4 个案例中使用以下代码:
bil = Arrays.copyOf(bil,n);
您应该参考这个答案以获取更多信息:Resize an Array while keeping current elements in Java?
由于您需要丢弃最初输入的数据并获得一组全新的数据点,因此您首先需要使用新的数据点数量创建一个新数组。然后您需要将用户输入分配给该数组。
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();
// Create a new array and assign it to the same variable bil
bil = new int[n];
System.out.println("Input the data");
for (i = 1; i <= n; i++) {
System.out.print(" data -" + i + " = ");
// Add data point to the (i-1)th position of the array
bil[i - 1] = s.nextInt();
edit = false;
}
break;
case "n":
return;
}
我正在尝试使用 switch case 进行数据搜索和排序。我必须输入数据量及其成员。例如:
数据量://例如:2
输入数据:
data num-1 : //用户输入
数据 num-2 : //用户输入
选择
案例一://数据搜索
案例 2://冒泡排序
案例3://selectionsort
案例 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
嘿,这里的问题是您已经将数组声明为
int bil = new int[n];
因此,在第 4 种情况下,当您将另一个 "n" 作为大于前一个 "n" 的输入时,它抛出 ArrayIndexOutOfBoundsException.You 需要在 it.Add 中存储更多值之前增加数组的大小在将新的“n”作为输入后,在第 4 个案例中使用以下代码:
bil = Arrays.copyOf(bil,n);
您应该参考这个答案以获取更多信息:Resize an Array while keeping current elements in Java?
由于您需要丢弃最初输入的数据并获得一组全新的数据点,因此您首先需要使用新的数据点数量创建一个新数组。然后您需要将用户输入分配给该数组。
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();
// Create a new array and assign it to the same variable bil
bil = new int[n];
System.out.println("Input the data");
for (i = 1; i <= n; i++) {
System.out.print(" data -" + i + " = ");
// Add data point to the (i-1)th position of the array
bil[i - 1] = s.nextInt();
edit = false;
}
break;
case "n":
return;
}