Closed Emilysea closed 6 years ago
Uses indexOf to find strings
Handles responding to simple words and phrases
This version uses a nested if to handle default responses.
@author Laurie White
@Version April 2012
/
public class Magpie2
{
/*
Get a default greeting
@return a greeting
*/
public String getGreeting()
{
return "Hello, let's talk.";
}
/**
Gives a response to a user statement
@Param statement
the user statement
@return a response based on the rules given
*/
public String getResponse(String statement)
{
String response = "";
if (statement.indexOf("no") >= 0)
{
response = "Why so negative?";
}
else if (statement.indexOf("mother") >= 0
|| statement.indexOf("father") >= 0
|| statement.indexOf("sister") >= 0
|| statement.indexOf("brother") >= 0)
{
response = "Tell me more about your family.";
}
else if(statement.indexOf("dog") >= 0
|| statement.indexOf("cat") >= 0)
{
response = "Tell me more about your pets.";
}
else if(statement.indexOf("Mr.") >= 0)
{
response = "He sounds very interesting.";
}
else if(statement.indexOf("Ms.") >= 0)
{
response = "She sounds very interesting.";
}
else if(statement.indexOf("day") >= 0)
{
response = "Tell me more about your day.";
}
else if(statement.indexOf("time") >= 0)
{
response = "Do you have a good time?";
}
else if(statement.indexOf("yes") >= 0)
{
response = "Ha, I like your attitude.";
}
String blank = statement.trim();
if(blank.length() ==0)
{
response = "Come on say something.";
}
else
{
response = getRandomResponse();
}
return response;
}
/**
Pick a default response to use if nothing else fits.
@return a non-committal string
*/
private String getRandomResponse()
{
final int NUMBER_OF_RESPONSES = 6;
double r = Math.random();
int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
String response = "";
if (whichResponse == 0)
{
response = "Interesting, tell me more.";
}
else if (whichResponse == 1)
{
response = "Hmmm.";
}
else if (whichResponse == 2)
{
response = "Do you really think so?";
}
else if (whichResponse == 3)
{
response = "You don't say.";
}
else if (whichResponse == 4)
{
response = "Yeah that is fun!";
}
else if (whichResponse == 5)
{
response = "Let me think...";
}
return response;
}
}
if(blank.length() ==0)
{
response = "Come on say something.";
}
else
{
response = getRandomResponse();
}
return response;
see here, you re assign the value of the variable response, no matter what it was assigned before
But I also assigned value of response every single one before, why wouldn't the code run those before and run this line?
because you did not print, you only assign, and you return at last
So does it mean the computer would regard the lines below "string blank" as another part , it would only print the last response?
where do you think it print things?
does the computer print everything you assigned to a variable or when you write system.out.print
Yea I got what you mean, when I wrote the code I wanted the "if(blank.length() ==0)" line to be identical to any "else if" before, and return the final response. But maybe it was a separate part starting from string blank.
sorta... i am not sure about the "seperate part" thing, eveyrthing is connected...
else if(statement.indexOf("yes") >= 0)
{
response = "Ha, I like your attitude.";
}
String blank = statement.trim();
if(blank.length() ==0)
{
response = "Come on say something.";
}
else
{
response = getRandomResponse();
}
return response;
I mean if it was connected, why would return response return only the last one? So I guess from String blank it was another "if else"statement different from the previous one. And the return response only work on this particular "if else" statement, exclude the ones before.
consider this example
a=1
a=2
a=3
a=4
return a
what do you think it will return
I think all four.
what do you think return mean, how does it return all four
suppose i have
Int getnumber(){
a=1
a=2
a=3
a=4
return a
}
main(){
system.out.println(getnumber);
}
you think it print 1 2 3 4 , right?
Yes, I think it returns all values ''a'' have.
but , you see, the programs runs linearly, how come i print once, but print 4 numbers?
So a's value changes until a == 4 and a is 4?
imagine a as a house, and the number is what is in the house. the number changes 4 times, and after forth change, the number inside the house is 4. now i return the house to someone else, and they found there is a 4 inside
Yea got it;)
/**
@version April 2012 */ public class Magpie2 { /**
/**
/**
@return a non-committal string / private String getRandomResponse() { final int NUMBER_OF_RESPONSES = 6; double r = Math.random(); int whichResponse = (int)(r NUMBER_OF_RESPONSES); String response = "";
if (whichResponse == 0) { response = "Interesting, tell me more."; } else if (whichResponse == 1) { response = "Hmmm."; } else if (whichResponse == 2) { response = "Do you really think so?"; } else if (whichResponse == 3) { response = "You don't say."; } else if (whichResponse == 4) { response = "Yeah that is fun!"; } else if (whichResponse == 5) { response = "Let me think..."; } return response; } }