All pastes #814451 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-12-13 09:40 UTC
rendered paste body

import java.util.*;    // scanner
public class week11

{

	public static void main (String args[]) {

		Scanner kybd = new Scanner(System.in);
		
		int choice;
		int count = 0;  //setting all values too 0
		int trueCount = 0;
		double percentage = 0;
		double [] highscores = {0,0,0,0,0,0}; //sets up arrays with values of 0
		String [] highscoresName = {"empty","empty","empty","empty","empty","empty"}; //sets up an array with the default values of "Nobody"
		
		System.out.print("Please Enter your User Name Account: ");
		String name = kybd.next(); //sets the string name to user input

		System.out.println(); //inserts a blank line

		System.out.println("Hello " + name + ", Welcome to the program");

		System.out.println(); //inserts another blank line
		
		do
		{
			System.out.println("1\tMultiplication Test");
			System.out.println("2\tView " + name + "'s Results");
			System.out.println("3\tSwitch User");
			System.out.println("4\tCheck your Highest score");
			System.out.println("5\tExit Program");
			System.out.println();
			System.out.print("Press 1 - 5 to choose your choice ");

			choice = kybd.nextInt();
			
			switch (choice)  //switch statement 
			{
			case 1: 
				for (int i = 0 ; i < 10 ; ++i)
				{
				boolean multiplication = testMultiplication(); //assigns the return value of the method to the variable multiplication
				
				if (multiplication == true)
				{
					System.out.println("Well Done, that is Correct");
					++trueCount; //counts the number of correct answers
					++count; //counters the number of total correct answers
				}
				else
				{
					System.out.println("Sorry that was incorrect, try the next question");
					++count; //counts the total of incorrect answers
				}
				}
				break;
			case 2:
				percentage = (((double)trueCount/(double)count)*100);

				System.out.println();

				System.out.println("User Name " + name);
				System.out.println(count + " Questions Answered");
				System.out.println(trueCount + " Questions Answered Correctly");
				System.out.println(percentage + "% of Questions Answered Correctly");

				System.out.println();

				break;
			case 3:
				if (count >= 10) //greater than or equal to 10 attempts
				{
					percentage = (((double)trueCount/(double)count)*100);
					highscores[5] = percentage; //current score to position 5 
					highscoresName[5] = name; //current name to positon 5
					for (int i = 5; i > 0; --i) //for loop from 5 - 1
					{
						if (highscores[i] > highscores[i-1])
						{
							double temp = highscores[i]; //stores the value at position i in a temporary variable
							highscores[i] = highscores[i-1]; //sets the value at position i to the value at i-1
							highscores[i-1] = temp; //sets the value at position i-1 to the value in the temporary variable
						
							String temp1 = highscoresName[i]; //same as above but for the name array
							highscoresName[i] = highscoresName[i-1]; 
							highscoresName[i-1] = temp1;
						}
					}
				}
				System.out.print("Please Enter your User Name ");
				name = kybd.next();
				count = 0;
				trueCount = 0;
				break;
			case 4:
				System.out.println();
				System.out.println("Highscores");
				System.out.println();
				for (int i = 0; i < 5; ++i) //loops 5 times from 0 - 4, this will display positions 1 - 5 on the highscores
				{
					System.out.println((i+1) + " " + highscoresName[i] + " " + highscores[i] );
				}

				break;
			case 5:
				System.out.println("Exiting Program");
				break;
			default: 
				System.out.println("Invalid Menu Choice");
			}
		
		}while (choice != 5);
		System.out.println("Thankyou for using this program.");
	}


 /**
   * generates 2 random numbers between 1 and 12
   *the user is asked to multiply the 2 numbers and input the answer<dr>
   * if the answer is correct then true is returned else false
   * @return  multiplication boolean - The True or False value of the answer
   */

	public static boolean testMultiplication()
	{
		Scanner kybd = new Scanner(System.in);
		Random rn = new Random();    // rn is an object that can generate random numbers
		int num1 = Math.abs(rn.nextInt()%12) + 1; //generates a random number between 1 and 12 and stores as the first number
		int num2 = Math.abs(rn.nextInt()%12) + 1; //generates a random number between 1 and 12 and stores as the second number
		int result = (num1 * num2); // works out what the actual result should be		
		System.out.println(num1 + " x " + num2 + " = ?"); //shows the user the 2 numbers
		int userResult = kybd.nextInt(); //stores the users answer
		
		if (result == userResult) //compares the 2 results
		{
			return true; //returns true if they match
		}
		else 
		{
			return false; //returns false if they don't match
		}
		
	}
}