Reputation: 1
I am a newbie in Java and my assignment asks me to do a word guessing console game that implements conditions, loops and arrays.
If I put an input, it should determine if my input is inside the array.
import java.util.Scanner;
public class LearnCode {
public static void main(String[] args) {
String[] guessTheFood = {"burger", "fries", "steak", "chicken", "pizza"};
String guess = "";
Scanner input = new Scanner(System.in);
System.out.println("Guess the food word.");
guess = input.nextLine();
for (int i = 0; i < guessTheFood.length; i++) {
if (guess.equals(guessTheFood[i])) {
System.out.println("You guessed it");
} else {
System.out.println("Oops, you guessed it wrong");
}
}
}
}
However, my output goes
Guess the food word.
steak
Oops, you guessed it wrong
Oops, you guessed it wrong
You guessed it
Oops, you guessed it wrong
Oops, you guessed it wrong
How do I fix or solve this?
Upvotes: 0
Views: 998
Reputation: 465
Like maloomeister said. You can use List:
public class LearnCode {
public static void main(String[] args) {
String[] guessTheFood = {"burger", "fries", "steak", "chicken", "pizza"};
List<String> guessFood = new ArrayList<>(Arrays.asList(guessTheFood));
Scanner input = new Scanner(System.in);
System.out.println("Guess the food word.");
String guess = input.nextLine();
if (guessFood.contains(guess.toLowerCase())) {
System.out.println("Match");
} else {
System.out.println("No Match");
}
}
}
Upvotes: -1
Reputation: 1316
You don't need to print negative response for every index. If found return 'SUCCESS RESPONSE' or if end of array return 'Failed Response'
for (int i = 0;; i++) {
if(i == guessTheFood.length) {
System.out.println("Oops, you guessed it wrong");
break;
}
if (guess.equals(guessTheFood[i])) {
System.out.println("You guessed it");
break;
}
}
Here the for-loop condition check is done inside the loop. This is to remove extra Flags and condition check outside the loop.
Upvotes: 1
Reputation: 3027
according to your code you are trying to print the result of condition in every iteration of for loop
and it cause to multiple print, you can define a vairable for example: isFound
and check the array and if it founds make it to true
then print the result according to isFound
:
String[] guessTheFood = { "burger", "fries", "steak", "chicken", "pizza" };
String guess = "";
Scanner input = new Scanner(System.in);
boolean isFound = false;
System.out.print("Guess the food word: ");
guess = input.nextLine();
for (int i = 0; i < guessTheFood.length; i++) {
if (guess.equals(guessTheFood[i])) {
isFound = true;
}
}
if (isFound) {
System.out.print("Guess the food word: ");
} else {
System.out.println("Oops, you guessed it wrong");
}
you can add another good option to your code to end the loop after the element has been found by adding the break
keyword:
for (int i = 0; i < guessTheFood.length; i++) {
if (guess.equals(guessTheFood[i])) {
isFound = true;
break;
}
}
Upvotes: 1
Reputation: 2165
You can do it by adding a boolean flag
to see if you have found the value or not.
Then on the basis of the flag you can print the result instead of printing it in each iteration. Refer code below.
public static void main(String []args){
String[] guessTheFood = {"burger", "fries", "steak", "chicken", "pizza"};
String guess = "";
Scanner input = new Scanner(System.in);
System.out.println("Guess the food word.");
guess = input.nextLine();
boolean flag = false; // <==== creating flag here
for (int i = 0; i < guessTheFood.length; i++) {
if (guess.equals(guessTheFood[i])) {
flag = true; // <==== changing it to true if value is found
}
}
if(flag)System.out.println("You guessed it"); // <==== using it's value to print result
else System.out.println("Oops, you guessed it wrong");
}
Upvotes: 0
Reputation: 9427
Only print the result when all are checked:
for (int i = 0; i < guessTheFood.length; i++) {
if (guess.equals(guessTheFood[i])) {
System.out.println("You guessed it");
} else {
System.out.println("Oops, you guessed it wrong");
}
}
This will print "guessed" or "wrong" for every check. You don't want that. Two things you want to change:
To print after, do something like this:
boolean result = false;
for (int i = 0; i < guessTheFood.length; i++) {
if (guess.equals(guessTheFood[i])) {
result = true;
}
}
// take the printing out of the loop, so it won't iterate with the rest
if ( result ) {
System.out.println("You guessed it");
} else {
System.out.println("Oops, you guessed wrong");
}
In order to check only what you need to check, break from the loop once the element is found:
boolean result = false;
for (int i = 0; i < guessTheFood.length; i++) {
if (guess.equals(guessTheFood[i])) {
result = true;
break; // break out of the loop, you've found your answer
}
}
if ( result ) {
System.out.println("You guessed it");
} else {
System.out.println("Oops, you guessed wrong");
}
Upvotes: 1