Results 1 to 3 of 3
- 08-13-2011, 01:00 PM #1
Searching array elements for keywords
Hi I have a simple question and answer game which I am trying to improve. At the moment everything is working how I'd like functionality wise.
I have 2 string arrays 'questions' and 'answers'. When the correct word is typed in exactly the same, it works. Here's my code:
Basically, when the user enters and extra letter, for instance in question 3, if the user enters 'bones' instead of 'bone' (the actual answer stored in the answers array), it tells them that is wrong. How could I search my answers array for keywords?Java Code:import java.util.Scanner; public class MyProgram { public static void main(String[] args) { arraysMethod(); } public static void arraysMethod() { String input; int tracker = 0; String[] questions = new String[11]; questions[1] = "What is the largest continent?"; questions[2] = "Which animal has black and white stripes?"; questions[3] = "What is a skeleton made of?"; questions[4] = "Where do bats live?"; questions[5] = "Where does a king and queen live?"; questions[6] = "What colour does a rainbow start with?"; questions[7] = "What sport does Wayne Rooney play?"; questions[8] = "What animal goes 'oink oink'?"; questions[9] = "Which planet is the nearest to Earth?"; questions[10] = "What colour is a banana?"; String[] answers = new String[11]; answers[1] = "asia"; answers[2] = "zebra"; answers[3] = "bone"; answers[4] = "cave"; answers[5] = "castle"; answers[6] = "red"; answers[7] = "football"; answers[8] = "pig"; answers[9] = "moon"; answers[10] = "yellow"; for (int x=1;x<=questions.length-1;x++) { System.out.println(questions[x]); Scanner scan = new Scanner(System.in); input = scan.nextLine(); if (input.equals(answers[x])) { System.out.println("Correct!"); tracker++; } else { System.out.println("Wrong!"); } } System.out.println("You scored " + tracker + "/10."); } }
I hope I have put my point across well and look forward to any feedback you may have.
Thank you.
- 08-13-2011, 01:07 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You could use the contains method. You may also be able to more easily write your code by creating some classes. for questions and answers. Which can have a question and a list of acceptable answers. If the user guesses one of the acceptable answers, he gets it correct; otherwise it's false.
- 08-13-2011, 01:46 PM #3
Similar Threads
-
Searching through Array of Objects
By coopc in forum New To JavaReplies: 6Last Post: 04-26-2011, 03:32 PM -
searching for a name in my 2d array and printing the info that is on its row
By ziongio in forum New To JavaReplies: 3Last Post: 03-15-2011, 12:24 PM -
Searching for keywords only instead of full-text search with Lucene
By DetRRG in forum LuceneReplies: 0Last Post: 02-25-2011, 08:13 AM -
Searching and comparing Array elements
By jmanswrd in forum New To JavaReplies: 5Last Post: 02-15-2011, 06:06 AM -
Searching In a String Array - Problem
By DillMan in forum New To JavaReplies: 4Last Post: 12-07-2008, 09:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks