如何使“else”不循环

6mzjoqzu  于 2022-10-15  发布在  Java
关注(0)|答案(5)|浏览(103)

我的代码是一个“猜谜游戏”,如果你输入一个东南亚国家,它会显示一个“youarecorrect”文本,但如果不是这样,我的问题是,else循环与我输入的国家数组的长度相乘,如何停止循环plz?
代码--

package guessinggame;
import java.util.Scanner;
public class GuessingGame {
    public static void main(String[] args) {

     String ans;
     String[] countries = {"Philippines", "Brunei", "Burma", "Cambodia", 
         "Timor Leste", "Indonesia", "Laos", "Malaysia", "Singapore", "Thailand", "Vietnam"};

    Scanner sc=new Scanner(System.in);  

     System.out.println("Please enter a Southeast Asian country");
        ans = sc.nextLine();

        for (int x=0; x<countries.length; x++) {

        if (ans.equals(countries[x])){

        System.out.println("Your answer (" + ans + ") is correct.");  

      }
        else System.out.println("Your answer (" + ans + ") is incorrect.");
}     
}
}
wljmcqd8

wljmcqd81#

目前,您的代码接受用户输入,然后在一个循环中将用户输入与其中一个国家进行比较

  • 如果match给出消息
  • 如果不匹配,则给出消息

这意味着如果你的列表有20个条目,如果输入不正确,用户会被告知20次。对于正确的输入,用户会被告知19次他错了,一次他是正确的。
您需要做的是:

  • 接受用户输入
  • 遍历列表并找出是否有匹配项。循环期间不打印结果
  • 循环完成后,打印是否找到匹配项

你可以设置一个变量

boolean found = false;

找到匹配项后,将其设置为true。稍后,您的输出可能如下所示

if (found) {
    System.out.println("correct");
} else {
    System.out.println("incorrect");
}

如果要优化代码,请在找到匹配项后立即终止循环。

6uxekuva

6uxekuva2#

您需要在循环中添加break和一个标志,以了解用户是否回答错误。检查代码中的注解。我希望下面的代码能帮助您!

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String ans;
        String[] countries = {"Philippines", "Brunei", "Burma", "Cambodia", "Timor Leste", "Indonesia", "Laos", "Malaysia", "Singapore", "Thailand", "Vietnam"};
        Boolean found = false; //Init the flag

        Scanner sc = new Scanner(System.in);  

        System.out.println("Please enter a Southeast Asian country");
        ans = sc.nextLine();

        for (int x = 0; x < countries.length; x++) {
            if (ans.equals(countries[x])){
                System.out.println("Your answer (" + ans + ") is correct.");
                found = true; //Change the flag to true if the answer is correct
                break; //Leave the loop so the flag can't change more
            }
        }

        // Check for the flag value. If the flag remains false, means that the answer is incorrect so will print the message below
        if (!found) {
            System.out.println("Your answer (" + ans + ") is incorrect.");
        }
    }

}
6psbrbz9

6psbrbz93#

for (int x = 0; x < countries.length; x++) {
        if (ans.equals(countries[x])) {
            System.out.println("Your answer (" + ans + ") is correct.");
            return;
        }
    }
    System.out.println("Your answer (" + ans + ") is incorrect.");
cngwdvgl

cngwdvgl4#

public static void main(String[] args) {

     String ans;
     String[] countries = {"Philippines", "Brunei", "Burma", "Cambodia", 
         "Timor Leste", "Indonesia", "Laos", "Malaysia", "Singapore", "Thailand", "Vietnam"};

    Scanner sc=new Scanner(System.in);  

     System.out.println("Please enter a Southeast Asian country");
        ans = sc.nextLine();

        int flag =0;
        for (int x=0; x<countries.length; x++) {

        if (ans.equals(countries[x])){           
           flag =1;
            break;
        }

    } 
    if(flag==1){
     System.out.println("Your answer (" + ans + ") is correct."); 
    }
    else {
      System.out.println("Your answer (" + ans + ") is incorrect.");
    }   

}

干得好

efzxgjgh

efzxgjgh5#

public class GuessingGame {
    public static void main(String[] args) {

        String ans;
        String[] countries = { "Philippines", "Brunei", "Burma", "Cambodia", "Timor Leste", "Indonesia", "Laos",
                "Malaysia", "Singapore", "Thailand", "Vietnam" };

        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter a Southeast Asian country");
        ans = sc.nextLine();

        boolean available = false;
        for (int x = 0; x < countries.length; x++) {

            if (ans.equals(countries[x])) {
                available = true;
                break;
            } 
        }

        if (available) {
            System.out.println("Your answer (" + ans + ") is correct.");
        } else {
            System.out.println("Your answer (" + ans + ") is incorrect.");
        }
    }
}

相关问题