英文:
Create a basic chat bot from the command line
问题
public class Main {
public static void main(String[] args) {
if (args.length == 0)
System.out.println("Nothing to say?");
else {
StringBuilder input = new StringBuilder();
for (String data : args)
input.append(data).append(" ");
input = new StringBuilder(input.toString().toLowerCase());
int indexOfAre = input.indexOf("are");
int indexOfYou = input.indexOf("you");
int indexOfFavourite = input.indexOf("favorite");
if (indexOfAre != -1 && indexOfYou != -1 && indexOfAre < indexOfYou)
System.out.println("Fine, but could use some more power.");
else if (indexOfFavourite != -1) {
int spaceIndex = input.indexOf(" ", indexOfFavourite);
int qmIndex = input.indexOf("?", spaceIndex);
String str = input.substring(spaceIndex + 1, qmIndex);
System.out.println(str.trim() + "? I'm only interested in transistors.");
} else
System.out.println("I didn't understand that.");
}
}
}
The issue with the "transistors" answer seems to be caused by the fact that you are checking for the presence of the word "favorite" in the input string, but you are interested in the word "favourite" (with the British spelling). To fix this, you should change the line:
int indexOfFavourite = input.indexOf("favorite");
to:
int indexOfFavourite = input.indexOf("favourite");
英文:
I have tried everywhere else to get help with my homework question, I really just need some help with this one. I'm supposed to have a no-Scanner ChatBot with:
-If the user entered nothing at the command line, show Nothing to say?
-If the user entered some variation of how are you today? show Fine, but could use some more power. As long as the input has the word "are" before the word "you", print this message. Make your program smart enough to accept variations of these words (see output).
-If the user entered some variation of what is your favorite something? Display something? I'm only interested in transistors.
-In all other cases, show I didn't understand that.
Here's my code so far:
public class Main
{
public static void main(String[] args)
{
if(args.length == 0)
System.out.println("Nothing to say?");
else
{
StringBuilder input = new StringBuilder();
for(String data: args)
input.append(data).append(" ");
input = new StringBuilder(input.toString().toLowerCase());
int indexOfAre = input.indexOf("are");
int indexOfYou = input.indexOf("you");
int indexOfFavourite = input.indexOf("favourite");
if( indexOfAre != -1 && indexOfYou != -1 && indexOfAre < indexOfYou)
System.out.println("Fine, but could use some more power.");
else if(indexOfFavourite != -1)
{
int spaceIndex = input.indexOf(" ", indexOfFavourite);
int qmIndex = input.indexOf("?", spaceIndex);
String str = input.substring(spaceIndex+1,qmIndex);
System.out.println(str.trim()+"? I'm only interested in transistors.");
}
else
System.out.println("I didn't understand that.");
}
}
}
The transistors answer doesn't seem to be working and I can't figure out why, please help
Thank you
专注分享java语言的经验与见解,让所有开发者获益!
评论