My code is a "guessing game" and so if you enter a southeast asian country to the input it will display a "you are correct" text but if not then otherwise, my problem is that the else loops whatever times the length of my array is that i put the countries in, how to stop loop plz?
Code--
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.");
}
}
}
CodePudding user response:
Currently your code takes user input, then within a loop it compares user input to one of the coutries and
- if match gives a message
- if not match gives a message
That means if your list is 20 entries long, in case of incorrect enty the user would be told so 20 times. For a correct entry, the user would be told 19 times that he is wrong and one time that he is correct.
What you need to do is:
- take user input
- loop over the list and find out if there is a match. Do not print a result during the loop
- after the loop has finished, print whether some match was found
You could set a variable
boolean found = false;
and set it to true
once you found a match. Later your output can look like
if (found) {
System.out.println("correct");
} else {
System.out.println("incorrect");
}
If you want to tune your code terminate the loop as soon as you found a match.
CodePudding user response:
You need to add break to your loop and a flag to can know if the user answer incorrect. Check the comments in my code. I hope the code below will help you!
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.");
}
}
}
CodePudding user response:
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.");
CodePudding user response:
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.");
}
}
here you go
CodePudding user response:
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.");
}
}
}