如何更改数据的数量和内容。

huangapple go评论66阅读模式
英文:

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(&quot;-------------------data search and sort-------------------&quot;);
System.out.println(&quot;&quot;);
System.out.println(&quot;&quot;);
System.out.println(&quot;Hai &quot;);
System.out.print(&quot;How many datas do you want? : &quot;);
n=s.nextInt();
int bil[]=new int[n]; //bil is array
System.out.println(&quot;input the data:&quot;);
for(int i=1;i&lt;=n;i++){
System.out.print(&quot; data -&quot; +i+ &quot; = &quot;);
bil[i-1]=s.nextInt();
}
System.out.println(&quot;&quot;);
System.out.println(&quot;&quot;);
while(true){
System.out.println(&quot;Displaying data(s) : &quot;);
for(int i: bil){
System.out.print(i+&quot; &quot;);
}
System.out.println(&quot;&quot;);
System.out.println(&quot;Menu:&quot;);
System.out.println(&quot;\t1. Search data&quot;);
System.out.println(&quot;\t2. Bubblesort ascending&quot;);
System.out.println(&quot;\t3. SelectionSort descending&quot;);
System.out.println(&quot;\t4. Edit&quot;);
System.out.println(&quot;\t5. Exit&quot;);
System.out.print(&quot;What&#39;s your choice? : &quot;);
int pilih=s.nextInt();
switch(pilih){
case 1:
System.out.println(&quot;Enter the number you are looking for = &quot;);
int srch = s.nextInt();
boolean found = false;
for(int index=0; index&lt;bil.length; index++) {
if(bil[index] == srch){
found = true;
}
}
if(found == true) {
System.out.println(&quot;Found &#39;&quot;+srch + &quot;&#39; in data collection!&quot;);
} else {
System.out.println(srch + &quot;there is no data you&#39;re looking for here&quot;);
}
System.out.println(&quot;&quot;);
System.out.println(&quot;Displaying data : &quot;);
for(int i: bil){
System.out.print(i+&quot; &quot;);
}
System.out.println(&quot;&quot;);
System.out.print(&quot;Continue? (y/n)&quot;);
String conti = s.next();
boolean nue;
switch(con){
case &quot;y&quot;:
nue = false;
break;
case &quot;n&quot;: 
return;
}
break;
case 2: 
System.out.println(&quot;BubbleSorting is done!&quot;);
System.out.println(&quot;&quot;);
System.out.println(&quot;Displaying data(s) :&quot;);
int i, j, te;
for (i = 0; i &lt; n; i++)
for (i = 0; i &lt; ( n - 1 ); i++) {
for (j = 0; j &lt; n - i - 1; j++) {
if (bil[j] &gt; bil[j+1]){
te = bil[j];
bil[j] = bil[j+1];
bil[j+1] = te;
}
}
}
for (i = 0; i &lt; n; i++) {
System.out.print(bil[i]);
}
System.out.print(&quot;Continue? (y/n)&quot;);
conti = s.next();
switch(conti){
case &quot;y&quot;:
nue = false;
break;
case &quot;n&quot;: 
return;
}
break;
case 3:
for (i = 0; i &lt; n; i++){
for (j = i + 1; j &lt; n; j++){
if (bil[i] &lt; bil[j]){
te = bil[i];
bil[i] = bil[j];
bil[j] = te;
}
}
}
System.out.print(&quot;SelectionSort is done&quot;);
System.out.println(&quot;&quot;);
System.out.println(&quot;Displaying data(s):&quot;);
for (i = 0; i &lt; n - 1; i++){
System.out.print(bil[i] + &quot; &quot;);
}
System.out.print(bil[n - 1]);
System.out.println(&quot;&quot;);
System.out.print(&quot;Continue? (y/n)&quot;);
conti = s.next();
switch(conti){
case &quot;y&quot;:
nue = false;
break;
case &quot;n&quot;: 
return;
}
break;
case 4:
System.out.print(&quot;WARNING!!! the data will be change!&quot;);
System.out.print(&quot;continue? (y/t)&quot;);
String change = s.next();
boolean edit;
switch(change){
case &quot;y&quot;:
System.out.print(&quot;How many datas do you want? = &quot;);
n=s.nextInt();
System.out.println(&quot;Input the data&quot;);
for(i=1;i&lt;=n;i++){
System.out.print(&quot; data -&quot; +i+ &quot; = &quot;);
bil[n]=s.nextInt();
edit = false;
}
break;
case &quot;n&quot;: 
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转义字符(如&quot;)更改为正常的引号以提高可读性。

英文:

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(&quot;WARNING!!! the data will be change!&quot;);
System.out.print(&quot;continue? (y/t)&quot;);
String change = s.next();
boolean edit;
switch (change) {
case &quot;y&quot;:
System.out.print(&quot;How many datas do you want? = &quot;);
n = s.nextInt();
// Create a new array and assign it to the same variable bil
bil = new int[n];
System.out.println(&quot;Input the data&quot;);
for (i = 1; i &lt;= n; i++) {
System.out.print(&quot; data -&quot; + i + &quot; = &quot;);
// Add data point to the (i-1)th position of the array
bil[i - 1] = s.nextInt();
edit = false;
}
break;
case &quot;n&quot;:
return;
}

huangapple
  • 本文由 发表于 2020年8月6日 15:51:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63279092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定