英文:
How to change the amount and the content of its data
问题
Here's the translated code portion you requested:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
System.out.println("-------------------数据搜索和排序-------------------");
System.out.println("");
System.out.println("");
System.out.println("你好 ");
System.out.print("你想要多少个数据? : ");
n = s.nextInt();
int bil[] = new int[n]; // bil是数组
System.out.println("输入数据:");
for (int i = 1; i <= n; i++) {
System.out.print(" 数据 -" + i + " = ");
bil[i - 1] = s.nextInt();
}
System.out.println("");
System.out.println("");
while (true) {
System.out.println("显示数据:");
for (int i : bil) {
System.out.print(i + " ");
}
System.out.println("");
System.out.println("菜单:");
System.out.println("\t1. 搜索数据");
System.out.println("\t2. 冒泡排序升序");
System.out.println("\t3. 选择排序降序");
System.out.println("\t4. 编辑");
System.out.println("\t5. 退出");
System.out.print("你的选择是什么? : ");
int pilih = s.nextInt();
switch (pilih) {
case 1:
System.out.println("输入你要查找的数字 = ");
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("在数据集合中找到了'" + srch + "'!");
} else {
System.out.println(srch + "这里没有你要找的数据");
}
System.out.println("");
System.out.println("显示数据:");
for (int i : bil) {
System.out.print(i + " ");
}
System.out.println("");
System.out.print("继续吗? (y/n)");
String conti = s.next();
boolean nue;
switch (conti) {
case "y":
nue = false;
break;
case "n":
return;
}
break;
case 2:
System.out.println("冒泡排序完成!");
System.out.println("");
System.out.println("显示数据:");
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("继续吗? (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("选择排序完成");
System.out.println("");
System.out.println("显示数据:");
for (i = 0; i < n - 1; i++) {
System.out.print(bil[i] + " ");
}
System.out.print(bil[n - 1]);
System.out.println("");
System.out.print("继续吗? (y/n)");
conti = s.next();
switch (conti) {
case "y":
nue = false;
break;
case "n":
return;
}
break;
case 4:
System.out.print("警告!!! 数据将被更改!");
System.out.print("继续吗? (y/t)");
String change = s.next();
boolean edit;
switch (change) {
case "y":
System.out.print("你想要多少个数据? = ");
n = s.nextInt();
System.out.println("输入数据");
for (i = 1; i <= n; i++) {
System.out.print(" 数据 -" + i + " = ");
bil[n] = s.nextInt();
edit = false;
}
break;
case "n":
return;
}
break;
}
}
}
Please note that I've translated the code and kept the structure and logic intact.
英文:
i'm trying to make data search and sort with switch case. i have to input the amount of data and its members. for example:
amount of data: //for example: 2
input the data(s):
data num-1 : //user input
data num-2 : //user input
selection
case 1: //data search
case 2: //bubblesort
case 3: //selectionsort
case 4: //edit
i stuck at case 4. i've tried my code but the amount of data and the current doesn't changes at all
and if i want to change the amount of data with bigger number than before, the index out of the bound.
here's the code.
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: i do not forget to import java.util.Scanner
答案1
得分: 0
问题在于你已经声明了数组为
int bil = new int[n];
所以在你的第四个情况下,当你再次输入一个比之前的“n”大的“n”时,它会抛出ArrayIndexOutOfBoundsException。在存储更多值之前,你需要增加数组的大小。在输入新的“n”后,在你的第四个情况下添加以下代码:
bil = Arrays.copyOf(bil, n);
你可以参考这个答案获取更多信息:https://stackoverflow.com/questions/13197702/resize-an-array-while-keeping-current-elements-in-java
英文:
Hey the issue here is that you are already declaring the array as
int bil = new int[n];
So in your 4th case when you take another "n" as input which is larger than the previous "n" , it throws the ArrayIndexOutOfBoundsException.You need to increase the size of array before storing more values in it.Add the following code in your 4th case after taking the new "n" as input:
bil = Arrays.copyOf(bil,n);
You should refer to this answer for more info:https://stackoverflow.com/questions/13197702/resize-an-array-while-keeping-current-elements-in-java
答案2
得分: 0
由于您需要放弃最初输入的数据并获取一组全新的数据点,首先需要创建一个具有新数据点数量的新数组。然后,您需要将用户输入分配给该数组。
case 4:
System.out.print("警告!!! 数据将被更改!");
System.out.print("是否继续? (y/t)");
String change = s.next();
boolean edit;
switch (change) {
case "y":
System.out.print("您需要多少个数据点? = ");
n = s.nextInt();
// 创建一个新数组并将其分配给变量bil
bil = new int[n];
System.out.println("输入数据");
for (i = 1; i <= n; i++) {
System.out.print(" 数据 -" + i + " = ");
// 将数据点添加到数组的第(i-1)个位置
bil[i - 1] = s.nextInt();
edit = false;
}
break;
case "t":
return;
}
请注意,我已将代码中的HTML转义字符(如"
)更改为正常的引号以提高可读性。
英文:
Since you need to discard the originally entered data and get a completely new set of data points, first you need to create a new array with the new number of data points. Then you need to assign the user inputs to that array.
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论