zh3036 / codingQuestions

um, ask in pull request
7 stars 0 forks source link

Why would this code skip all the if statements before and directly run the "string blank" part? #3

Closed Emilysea closed 6 years ago

Emilysea commented 6 years ago

/**

testZH0718 commented 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;
}
}
testZH0718 commented 6 years ago
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

Emilysea commented 6 years ago

But I also assigned value of response every single one before, why wouldn't the code run those before and run this line?

zh3036 commented 6 years ago

because you did not print, you only assign, and you return at last

Emilysea commented 6 years ago

So does it mean the computer would regard the lines below "string blank" as another part , it would only print the last response?

zh3036 commented 6 years ago

where do you think it print things?

zh3036 commented 6 years ago

does the computer print everything you assigned to a variable or when you write system.out.print

Emilysea commented 6 years ago

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.

zh3036 commented 6 years ago

sorta... i am not sure about the "seperate part" thing, eveyrthing is connected...

Emilysea commented 6 years ago
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.

zh3036 commented 6 years ago

consider this example

a=1
a=2
a=3
a=4
return a

what do you think it will return

Emilysea commented 6 years ago

I think all four.

zh3036 commented 6 years ago

what do you think return mean, how does it return all four

zh3036 commented 6 years ago

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?

Emilysea commented 6 years ago

Yes, I think it returns all values ''a'' have.

zh3036 commented 6 years ago

but , you see, the programs runs linearly, how come i print once, but print 4 numbers?

Emilysea commented 6 years ago

So a's value changes until a == 4 and a is 4?

zh3036 commented 6 years ago

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

Emilysea commented 6 years ago

Yea got it;)