I have a basic greeting program that is supposed to come up with a response based on the user's input. I am trying to accomplish it using for loops; however, whenever the user replies with the "bad" or "you" values stored in different arrays, the console prints out the same response multiple times. How could this problem be approached? I'm a noob here, and in java. I apologize if this answer has been previously answered. I searched but couldn't find. Thank you for your time.
import java.util.Scanner;
public class Greeter {
public static void main(String[] args) {
String[] greetings = { "well", "good", "great", "awesome", "fabulous" };
String[] bad_greetings = { "bad", "awful" };
String[] responses = { "you", "yourself" };
System.out.println("Hello there, how are you?");
String response;
Scanner scan = new Scanner(System.in);
response = scan.nextLine();
for (String greeting : greetings) {
if (response.contains(greeting)) {
System.out.println("Well, good for you!");
}
for (String b_greet : bad_greetings) {
if (response.contains(b_greet)) {
System.out.println("At least you have me.");
}
}
for (String reply : responses) {
if (response.contains(reply)) {
System.out.println("I'm well, thank you.");
// } else {
// System.out.println("Let's move on then.");
// }
}
}
}
}
}