This question already has an answer here:
- How do I compare strings in Java? 23 answers
**I'm trying to solve this question :
Allow the user to put in complaints using standard input until they put in the phrase “That is all”. If they say that they “hate” something, then say that you hate that same item. If they say that someone “did” something, respond with the phrase “OH NO name didn’t!?!?”. Keep in mind that “I” should be transformed to “you”. If they say “I don’t know what is worse, phrase1 or phrase2”, respond that one is worse than the other but both are pretty bad. Pick randomly which will be indicated to be worse.**
I wrote the code below, but for some weird reason anything I input is not getting processed. According to the question anything other than that is all should stop the program, but the program still continues to run even after inputing 'that is all'. Moreover it doesn't print out anything whenever I input 'I hate something' or any of the conditions in the question above. Can you please tell me what is wrong with my code. Thanks in advance.:)
public static void airingOfGrievances(){
Scanner input = new Scanner(System.in);
String userInput = input.next() + input.nextLine();
while (userInput != "that is all")
{
String[] split = userInput.split(" ");
String[] split2 = userInput.split(",");
if (userInput == "testing")
{
System.out.println("working");
}
for (int i = 0; i < split.length; i++)
{
if (split[i] == "hate")
{
System.out.println ("I also hate " + split[split.length - 1]);
break;
}
else if (split[i] == "did")
{
System.out.println ("OH NO " + split[0] + " didn't!?!?");
break;
}
else if (split[0] == "I don’t know what is worse,")
{
String[] split3 = split2[1].split(" ");
double random = Math.random();
if (random >= 0.5)
{
System.out.println(split3[2] + " are worse but both are pretty bad");
}
else if (random <= 0.5)
{
System.out.println(split3[0] + " are worse but both are pretty bad");
}
break;
}
}
userInput = input.next() + input.nextLine();
System.out.println(userInput);
}
}
Aucun commentaire:
Enregistrer un commentaire