Results 1 to 6 of 6
Thread: Arrays and if statements
- 11-02-2012, 01:39 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Arrays and if statements
hey everyone, im very new to java, and i thought it might be interesting to try to make an AI which you can talk casually to, ive made it so that when it asks you a yes or no question you can say things like yeah, sure, okay, nope, nah and so on, but the way ive done this is by making an if statement which looks somewhat like this:
if(timestable.equals("y")||timestable.equals("Y")| |timestable.equals("yes")||timestable.equals("Yes" )||timestable.equals("Yep")||timestable.equals("ye p")||timestable.equals("yeah")||timestable.equals( "Yeah")||timestable.equals("okay")||timestable.equ als("Okay")||timestable.equals("Sure")||timestable .equals("sure"))
i was wondering if it was maybe possible to put all of these strings into an array, and make it so that you can say any one of those, without needing to say all of them, and that will make it possible to make the if statement look something like this:
if(timestable.equals(array))
- 11-02-2012, 01:50 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Arrays and if statements
Use a Set that contains all synonyms for "yes", in, say, uppercase; you can do this:
kind regards,Java Code:if (yesSet.contains(answer.toUpperCase())) ...
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-02-2012, 02:00 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Arrays and if statements
I would map them to a single affirmative result, so all those would be "translated" into a single thing the rest of the code can understand (say "Y" for all the above).
This Translator can then hold all the unpleasantness, which you can add to or change later on to use a database maybe, or file or whatever.
So you get the user response and call translate() which gives you a far more limited set of results.
If it can't find a translation then it can simply return the original text.
Note, this is just off the top of my head. I usually hide these things away so the main code flow looks neater and I can always rework the messy stuff later as I get better ideas of what to do.Please do not ask for code as refusal often offends.
- 11-02-2012, 02:01 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Arrays and if statements
I write too much...
Please do not ask for code as refusal often offends.
- 11-02-2012, 02:03 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Re: Arrays and if statements
hey JosAH, thanks for the response, sets sound useful for what i'm doing, but im not sure how to use them D:
- 11-02-2012, 02:19 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Copying Single Arrays to 2-D Arrays
By jmscarlet9 in forum New To JavaReplies: 7Last Post: 04-02-2012, 11:17 PM -
If statements and arrays, problem
By Madlollipop in forum New To JavaReplies: 3Last Post: 11-12-2011, 12:30 PM -
Arrays and For statements for Images in Applet
By SummitWhiteMaro in forum Java AppletsReplies: 0Last Post: 03-07-2011, 03:59 AM -
Help with if else statements
By np2392 in forum New To JavaReplies: 2Last Post: 09-24-2010, 01:25 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks