I am making a program in Jgrasp that stores words from a text file in arrays. There are 2 files and 2 arrays, one stores words spelled correctly and the other words spelled incorrectly. The program then randomly selects a word from one of the 2 arrays and asks the user if it is spelled correctly. I've got everything running great, but need a way to prevent words from being selected more than once. Code is posted below:
import java.util.*;
import java.io.*;
public class wordGame
{
public static void main(String[] args) throws Exception
{
boolean more = true;
Scanner kb = new Scanner(System.in);
while(more)
{
run();
System.out.println("Another player?");
String answer = kb.next();
if(answer.equalsIgnoreCase("no"))
more = false;
}
System.out.println("Good Bye");
}
public static void run() throws Exception
{
String[] correct = new String[10];
String[] incorrect = new String[10];
//static void description()
description();
//static void fillArray(String[] correct, String[] incorrect)
fillArray(correct, incorrect);
//static void play(String[] correct, String[] incorrect)
play(correct, incorrect);
}
//actual play
public static void play(String[] correct, String[] incorrect)
{
Scanner kb = new Scanner(System.in);
Random rand = new Random();
int points = 0;
for(int i = 1; i <= 10; i++)
{
//genreate a random number either 0 or 1
int r = rand.nextInt(2);
if(r == 0)
{
//generate a random number between 0-9 inclusive (rand.nextInt(10))
int index = rand.nextInt(10);
//display the word on the string from the correct array at the given index
System.out.println("Is the word ***"+correct[index]+"*** spelled correctly?");
//ask the the user to enter if the word is spelled correctly or not y/n
System.out.print("y/n: ");
String answer = kb.next();
//if the users answer is yes increment points by 1, otherwise decrement by 1
if(answer.equalsIgnoreCase("y"))
points++;
else
points--;
}
else
{
int index = rand.nextInt(10);
//display the word on the string from the correct array at the given index
System.out.println("Is the word ***"+incorrect[index]+"*** spelled correctly?");
//ask the the user to enter if the word is spelled correctly or not y/n
System.out.print("y/n: ");
String answer = kb.next();
//if the users answer is yes increment points by 1, otherwise decrement by 1
if(answer.equalsIgnoreCase("n"))
points++;
else
points--;
}
}
System.out.println("Your score: "+points);
}
public static void fillArray(String[] correct, String[] incorrect) throws Exception
{
//create a file object
File f1 = new File("correct.txt");
//create a pointer to the file
Scanner input1 = new Scanner(f1);//input: name of scanner pulling info from file
int index1 = 0;
//fill in the array correct
while(input1.hasNextLine() && index1 < correct.length)
{
//read one word
String word = input1.nextLine();
//store it in the array
correct[index1] = word;
index1++;
}
//create a file object
File f2 = new File("incorrect.txt");
//create a pointer to the file
Scanner input2 = new Scanner(f2);//input: name of scanner pulling info from file
int index2 = 0;
//fill in the array incorrect
while(input2.hasNextLine() && index2 < incorrect.length)
{
//read one word
String word = input2.nextLine();
//store it in the array
incorrect[index2] = word;
index2++;
}
}
// displays instructions for the game
public static void description()
{
System.out.printf("%40s", "Welcome to Misspelled!!");
System.out.printf("\n%41s", "Please play resonbsibly!!");
System.out.println("");
System.out.printf("\n%39s", "THE RULES OF THE GAME\n");
System.out.println("You will be shown a series of 10 words that are spelled correctly or incorrectly");
System.out.println("Answer y if it is spelled correctly, n if it is spelled incorrectly");
System.out.println("Correct answers will award you 1 point, incorrect answers will lose you 1 point");
System.out.println("");
}
}
Aucun commentaire:
Enregistrer un commentaire