gpt4 book ai didi

java - 即使调用了break,循环仍在运行;

转载 作者:行者123 更新时间:2023-12-01 18:35:41 26 4
gpt4 key购买 nike

好吧,我有这个服务器,它创建一个随机生成的数组,然后告诉用户删除一个数字。但是我添加了一个部分,以便如果用户选择一个不在数组中的数字,那么它会告诉他们再次选择...它成功地告诉用户不在数组中并再次选择,然后它只允许我输入自由数字,无需继续程序...这是我的代码,这是我得到的响应

    import java.util.Random;
import java.util.Scanner;
public class Server {
public static boolean checked = false;
public static int removed;
public static int inserted;
public static int index;
public static int[] array = new int[50];
public static void generateArray(){
Random random = new Random();
for(int i=0; i<array.length; i++){
int x = random.nextInt(101);
if(x>0){
array[i]= x;}
else{
i--;}
for (int j=0; j < i; j++) {
if (array[i] == array[j]) {
i--;
}
}

}
for(int i=0; i<array.length; i++){
System.out.print(array[i] + " ");
}
System.out.println();
}

public static void insertRemove(){
Scanner input = new Scanner(System.in);
if(checked == false){
System.out.println("Select a number to be removed.");
checked = true;
}
removed = input.nextInt();
Server.checkValid();
if(checked == true){
System.out.println("Select a new number to be inserted.");
inserted = input.nextInt();
System.out.println("Select an index for the number to be inserted at.");
index = input.nextInt();
}
}
public static void checkValid(){
boolean repeat = false;
loop:
for(int i=0; i<array.length; i++){
for(int j=0; j<array.length; j++){
if(array[i] == removed){
checked = true;
Server.insertRemove();
}
else{
System.out.println("That number isn't in the array.");
checked = false;
repeat = true;
if(checked == false){
break loop;
}
else{}
}
}
}
if(repeat == true){
Server.insertRemove();
}
}
}

回应
由于else... break;,它也只检查数组的第一个数字。但如果我使用 for(int j=0; j<array.length j++) 进行双循环然后它打印 That number isn't in the array. 50 次。

43 27 62 44 85 15 61 17 59 20 84 73 60 49 26 54 41 5 64 96 32 68 86 7 52 53 47 19 58 18 70 74 50 28 38 91 89 1 36 42 78 45 97 57 9 29 55 2 40 90 
Select a number to be removed.
0
That number isn't in the array.
Select a number to be removed.
43
43
43 // <----as you can see it's still allowing me to just type in 43
43

有一个 main 方法调用这个...

public class Main {
public static void main(String[] args){
Server.generateArray();
Server.insertRemove();
}
}

基本上,我怎样才能修复这个当前允许我插入数字的循环,这样,如果我插入不在数组中的东西,然后插入实际上在数组中的东西,那么它将允许我继续正常的程序并要求我提供一个要插入的编号、要插入的索引等。

最佳答案

您的“包含”逻辑中有一个初学者常见的错误。它看起来像这样:

for(/* elements in array */){
if(/* check one element */){
// do contains action
} else {
// do not contains action
}
}

它不起作用,因为它只检查数组中的第一个元素。 “else”操作需要在循环之后。需要在检查整个数组后完成。

(这个错误也是为什么它在删除分隔符时重复“不在数组中”消息的原因,因为它会为每个不匹配的数字输入 else 子句。)

但更重要的是,您使用状态来管理这两种方法并不好。您的模式如下所示:

method1 calls method2
if not method2 call method1
method1 calls method2
method2 completes normally
...also returns to method1
...also returns to method2
...also returns to method1

这种困惑的逻辑产生了一个错误,即您的状态变量 checked 永远不会重置。最后添加 checked = false; 语句可能会纠正该错误,但在这里我建议重写。

更结构化的方法如下所示,使用 do-while 循环来验证输入:

public static void insertRemove() {
Scanner in = new Scanner(System.in);
int selection;
do {
selection = in.nextInt();
} while(!checkValid(selection));

// selection is now valid so do whatever you need to
}

public static boolean checkValid(int selection) {
for(int i = 0; i < array.length; i++) {
if(array[i] == selection) {
return true;
}
}

return false;
}

checkValid 返回索引也可能很有用。如果找到该号码,您将返回 i;如果未找到该号码,则在末尾返回 -1。这将使您更方便地执行替换。可以这样重写:

public static void insertRemove() {
Scanner in = new Scanner(System.in);
int selection;
int index;
do {
selection = in.nextInt();
index = getIndexOf(selection);
} while(index < 0);

// ask user for a new number
selection = in.nextInt();
array[index] = selection;
}

非常结构化,更容易遵循。

关于java - 即使调用了break,循环仍在运行;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com