This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated!
import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu(); if (menuChoice == 1) { displayRules(); } else if (menuChoice == 2) { //Declare a File object File inFile = new File ("questions.txt"); //Declare Scanner Scanner in = new Scanner(inFile); for(int i = 1; i<=8;i++) { question = in.nextLine(); answerA = in.nextLine(); answerB = in.nextLine(); answerC = in.nextLine(); correctAnswer = in.nextInt(); in.nextLine(); points = in.nextInt(); in.nextLine(); score+=processQuestion(question, answerA, answerB, answerC, points, correctAnswer); displayScore(score); } in.close(); highscore = readInHighScore(); compareScore(highscore, score); JOptionPane.showMessageDialog(null, "Thanks for playing!"); } else if (menuChoice == 3) { System.exit(0); } }while(menuChoice!=3); } //Return type: int //Parameters: None //Purpose: Display intro and prompt user for name and greet them by name public static void displayIntro() { String name = null; // Display an Introduction to the Game. JOptionPane.showMessageDialog(null,"\n Welcome to Who Wants to Be a Java Programmer! "); //Use an input dialog box to prompt the user for his/her name. name= JOptionPane.showInputDialog("\n Please enter your name: "); //Use an message dialog box to say hello. JOptionPane.showMessageDialog(null,"\n Hello " + name + "!"); } //Return type: char //Parameters: None //Purpose: To display main menu and prompt user for their choice and validate. Method returns menu choice public static int displayMainMenu() { String input = null; int menuChoice = 0; do { input = JOptionPane.showInputDialog("\n ===== Main Menu: =====\n >>> Enter 1 to See Rules" + "\n >>> Enter 2 to Play Game\n >>> Enter 3 to Exit Game\n" + "\n Please select an option from the main menu: "); menuChoice = int.parseInt(input); if (menuChoice < 1 || menuChoice > 3) { JOptionPane.showMessageDialog(null, "Invalid input! Please enter a number from 1-3 for your menu choice."); } }while(menuChoice < 1 || menuChoice > 3); return menuChoice; } //Return type: void //Parameters: None //Purpose: To display the rules of the game public static void displayRules() { JOptionPane.showMessageDialog(null, "\n =====Game Rules===== \n You will be asked 10 questions " + "\n If answered correctly, you will receive however many points each question is worth. " + "\n If answered incorrectly, you will not receive any points.\n" + "\n These questions will be multiple choice, by which you will use the corresponding letters (A, B or C) to choose your answer .\n"); } //Return type: int //Parameters: 5 Strings and 2 ints //Purpose: To display the question and prompt for answer choice and validate. Returns the points the user earned depending on if they were correct or incorrect public static int processQuestion(String q1, String ansA, String ansB, String ansC, int pointValue, int correctAns) throws IOException { int answerChoice = 0; String input = null; do { input = JOptionPane.showInputDialog(null, q1 + "\n" + "1. " + ansA + "\n" + "2. " + ansB + "\n" + "3. " + ansC + "\n" ); answerChoice = Integer.parseInt(input); if (answerChoice < 1 || answerChoice > 3) { JOptionPane.showMessageDialog(null, "Invalid input! Please enter a number from 1-3."); } }while (answerChoice < 1 || answerChoice > 3); if (answerChoice == correctAns) { JOptionPane.showMessageDialog(null, "You are correct!"); return pointValue; } else { JOptionPane.showMessageDialog(null, "Incorrect! Try again"); return 0; } } //Return type: int //Parameters: None //Purpose: To read in the highscore from text file and return that value public static int readInHighScore() throws IOException { int highscore = 0; File outFile = new File ("highscore.txt"); // This is to open the highscore file to compare with user score Scanner currentScore = new Scanner(outFile); // This is to read in the current highscore to compare with user score highscore = currentScore.nextInt(); currentScore.close(); return highscore; } //Return type: void //Parameters: 2 ints //Purpose: To compare the user score to highscore and update the highscore file if user attained high score public static void compareScore(int highScore, int userScore) throws IOException { if (userScore > highScore) { //Declare a PrintWriter object for output PrintWriter out = new PrintWriter("highscore.txt"); out.print(userScore); out.close(); } } //Return type: void //Parameters: 1 int //Purpose: To display the user's score public static void displayScore(int score) { JOptionPane.showMessageDialog(null, "Your total score is: " + score); } }
I am not sure what issue you were getting but i am able to run the code with slight changes. I have updated the code and pasted below. Also i have attached output screenshots.
Here is the code.
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Scanner;
public class javaGamev5 {
public static void main(String[] args) throws
IOException {
String question = null, answerA =
null, answerB = null, answerC = null;
int menuChoice = 0, correctAnswer =
0, points = 0, score = 0, highscore = 0;
displayIntro();
do {
menuChoice =
displayMainMenu();
if (menuChoice
== 1) {
displayRules();
} else if
(menuChoice == 2) {
// Declare a File object
File inFile = new File("questions.txt");
// Declare Scanner
Scanner in = new Scanner(inFile);
for (int i = 1; i <= 8; i++) {
if(in.hasNextLine()) {
question =
in.nextLine();
answerA =
in.nextLine();
answerB =
in.nextLine();
answerC =
in.nextLine();
correctAnswer = in.nextInt();
in.nextLine();
points =
in.nextInt();
in.nextLine();
score +=
processQuestion(question, answerA, answerB, answerC, points,
correctAnswer);
displayScore(score);
}
}
in.close();
highscore = readInHighScore();
compareScore(highscore, score);
JOptionPane.showMessageDialog(null, "Thanks for
playing!");
} else if
(menuChoice == 3) {
System.exit(0);
}
} while (menuChoice != 3);
}
// Return type: int
// Parameters: None
// Purpose: Display intro and prompt user for name and
greet them by name
public static void displayIntro() {
String name = null;
// Display an Introduction to the
Game.
JOptionPane.showMessageDialog(null,
"\n Welcome to Who Wants to Be a Java Programmer! ");
// Use an input dialog box to
prompt the user for his/her name.
name =
JOptionPane.showInputDialog("\n Please enter your name: ");
// Use an message dialog box to say
hello.
JOptionPane.showMessageDialog(null,
"\n Hello " + name + "!");
}
// Return type: char
// Parameters: None
// Purpose: To display main menu and prompt user for
their choice and validate.
// Method returns menu choice
public static int displayMainMenu() {
String input = null;
int menuChoice = 0;
do {
input =
JOptionPane.showInputDialog("\n ===== Main Menu: =====\n
>>> Enter 1 to See Rules"
+ "\n >>> Enter 2 to
Play Game\n >>> Enter 3 to Exit Game\n"
+ "\n Please select an option
from the main menu: ");
menuChoice =
Integer.parseInt(input);
if (menuChoice
< 1 || menuChoice > 3) {
JOptionPane.showMessageDialog(null,
"Invalid
input! Please enter a number from 1-3 for your menu
choice.");
}
} while (menuChoice < 1 ||
menuChoice > 3);
return menuChoice;
}
// Return type: void
// Parameters: None
// Purpose: To display the rules of the game
public static void displayRules() {
JOptionPane.showMessageDialog(null,
"\n =====Game Rules===== \n You will be asked 10 questions "
+ "\n If answered correctly, you will receive
however many points each question is worth. "
+ "\n If answered incorrectly, you will not
receive any points.\n"
+ "\n These questions will be multiple choice,
by which you will use the corresponding letters (A, B or C) to
choose your answer .\n");
}
// Return type: int
// Parameters: 5 Strings and 2 ints
// Purpose: To display the question and prompt for
answer choice and validate.
// Returns the points the user earned depending on if
they were correct or
// incorrect
public static int processQuestion(String q1, String
ansA, String ansB, String ansC, int pointValue, int
correctAns)
throws
IOException {
int answerChoice = 0;
String input = null;
do {
input =
JOptionPane.showInputDialog(null,
q1 + "\n" + "1. " + ansA +
"\n" + "2. " + ansB + "\n" + "3. " + ansC + "\n");
answerChoice =
Integer.parseInt(input);
if (answerChoice
< 1 || answerChoice > 3) {
JOptionPane.showMessageDialog(null, "Invalid
input! Please enter a number from 1-3.");
}
} while (answerChoice < 1 ||
answerChoice > 3);
if (answerChoice == correctAns)
{
JOptionPane.showMessageDialog(null, "You are correct!");
return
pointValue;
} else {
JOptionPane.showMessageDialog(null, "Incorrect! Try again");
return 0;
}
}
// Return type: int
// Parameters: None
// Purpose: To read in the highscore from text file
and return that value
public static int readInHighScore() throws IOException
{
int highscore = 0;
File outFile = new
File("highscore.txt"); // This is to open the highscore file to
compare with user score
Scanner currentScore = new
Scanner(outFile); // This is to read in the current highscore to
compare with user
// score
highscore =
currentScore.nextInt();
currentScore.close();
return highscore;
}
// Return type: void
// Parameters: 2 ints
// Purpose: To compare the user score to highscore and
update the highscore file
// if user attained high score
public static void compareScore(int highScore, int
userScore) throws IOException {
if (userScore > highScore)
{
// Declare a
PrintWriter object for output
PrintWriter out
= new PrintWriter("highscore.txt");
out.print(userScore);
out.close();
}
}
// Return type: void
// Parameters: 1 int
// Purpose: To display the user's score
public static void displayScore(int score) {
JOptionPane.showMessageDialog(null,
"Your total score is: " + score);
}
}
--------------------------------------------------------OUTPUT------------------------------------------------------------
Please upvote if my answered helped you.
BEST OF LUCK!
Get Answers For Free
Most questions answered within 1 hours.