import java.util.Scanner;
public class JavaApplication1 {
public static void main(String[] args) {
double mark = 0;
Scanner input = new Scanner(System.in);
int NoOfQuestions;
boolean playing = true;
//input number of questions
while(playing){
System.out.print("Number of Questions: ");
NoOfQuestions = input.nextInt();
if(NoOfQuestions == 0){
System.out.println("Good Bye!");
return;//stop the whole program
}
for (int i = 0; i < NoOfQuestions; i++){
System.out.print("Q"+(i+1)+": ");//(i+1) is to make sure the Question number do start with 1
mark += AddOrSubOrMultiOrDiv(); //count the mark and show the Four questions randomly.
}
resultSentance(NoOfQuestions, mark);//count the percentage and show the closing sentences
mark = 0;
}
}
public static double AddOrSubOrMultiOrDiv(){
switch (( RandNumber(1000) % 4)){ //*1000 is to make the posiblity answer more equally
case 0:
return Addtion();
case 1:
return Subtraction();
case 2:
return Mutliplication();
}
return Division();//if the answer is 3, jump to division
}
public static double Addtion(){
int add1 = RandNumber(50);
int add2 = RandNumber(50);
int sum = AskInputForCalculation(add1, add2, "+"); //input
if(sum == add1 + add2){
return 1;
}
return 0;
}
public static double Subtraction(){
int Sub1 = RandNumber(99);//which is larger
int Sub2;
do{
Sub2 = RandNumber(99);//which is smaller or equal to Sub1
}while(Sub2 > Sub1);
int difference = AskInputForCalculation(Sub1, Sub2, "-"); //input
if(difference == Sub1 - Sub2){ //if the answer from the player is true, +1, else 0
return 1;
}
return 0;
}
public static double Mutliplication(){
int Multi1 = RandNumber(10);
int Multi2 = RandNumber(10);
int product = 0;
product = AskInputForCalculation(Multi1, Multi2, "*"); //input
if(product == Multi1 * Multi2){
return 1;
}
return 0;
}
public static double Division(){
int divisor = Divisor();
int division = DivisionSmallerOne(divisor);
int quotient = AskInputForCalculation(divisor, division, "/"); //input
if(quotient == divisor / division){ //if the answer from the player is true, +1, else 0
return 1;
}
return 0;
}
public static int Divisor(){
boolean BoolDivisor = false;
int divisor = 0;
while (BoolDivisor==false){
divisor = RandNumber(99);
if(divisor == 1 || divisor == 2 || divisor == 3 ||divisor == 5 || divisor == 7){
// the 1,2,3,5,7 is prime, so the while loop will continue to run.
}else {
for (int i = 2; i < 9; i++){
if(divisor % i == 0){
BoolDivisor = true; //since all the composite number must be divisble by 2/3/4/...../7, then stop the while loop
break;
}
}
}
}
return divisor;
}
public static int DivisionSmallerOne(int divisor){
int division;
//Random a num as a division which must be divisble by divisor
do{
division = RandNumber(40);
}while(divisor % division != 0 || division == 1);
return division;
}
public static int RandNumber(int RanRange){
return ((int)(Math.random() * RanRange + 1));
}
public static int AskInputForCalculation(int Number1, int Number2, String MathSymbols){
Scanner input = new Scanner(System.in);
System.out.print(Number1 + " " + MathSymbols + " " + Number2 +" =? ");
int playerAnswer = input.nextInt();
return playerAnswer;
}
public static void resultSentance(int NoOfQuestions, double mark){
System.out.println("You've answered " + NoOfQuestions +" questions.\n" + (int)mark + " of them are correct.");
System.out.printf("Your mark is %.2f%%\n" ,mark * 100 / NoOfQuestions);
}
}
public class JavaApplication1 {
public static void main(String[] args) {
double mark = 0;
Scanner input = new Scanner(System.in);
int NoOfQuestions;
boolean playing = true;
//input number of questions
while(playing){
System.out.print("Number of Questions: ");
NoOfQuestions = input.nextInt();
if(NoOfQuestions == 0){
System.out.println("Good Bye!");
return;//stop the whole program
}
for (int i = 0; i < NoOfQuestions; i++){
System.out.print("Q"+(i+1)+": ");//(i+1) is to make sure the Question number do start with 1
mark += AddOrSubOrMultiOrDiv(); //count the mark and show the Four questions randomly.
}
resultSentance(NoOfQuestions, mark);//count the percentage and show the closing sentences
mark = 0;
}
}
public static double AddOrSubOrMultiOrDiv(){
switch (( RandNumber(1000) % 4)){ //*1000 is to make the posiblity answer more equally
case 0:
return Addtion();
case 1:
return Subtraction();
case 2:
return Mutliplication();
}
return Division();//if the answer is 3, jump to division
}
public static double Addtion(){
int add1 = RandNumber(50);
int add2 = RandNumber(50);
int sum = AskInputForCalculation(add1, add2, "+"); //input
if(sum == add1 + add2){
return 1;
}
return 0;
}
public static double Subtraction(){
int Sub1 = RandNumber(99);//which is larger
int Sub2;
do{
Sub2 = RandNumber(99);//which is smaller or equal to Sub1
}while(Sub2 > Sub1);
int difference = AskInputForCalculation(Sub1, Sub2, "-"); //input
if(difference == Sub1 - Sub2){ //if the answer from the player is true, +1, else 0
return 1;
}
return 0;
}
public static double Mutliplication(){
int Multi1 = RandNumber(10);
int Multi2 = RandNumber(10);
int product = 0;
product = AskInputForCalculation(Multi1, Multi2, "*"); //input
if(product == Multi1 * Multi2){
return 1;
}
return 0;
}
public static double Division(){
int divisor = Divisor();
int division = DivisionSmallerOne(divisor);
int quotient = AskInputForCalculation(divisor, division, "/"); //input
if(quotient == divisor / division){ //if the answer from the player is true, +1, else 0
return 1;
}
return 0;
}
public static int Divisor(){
boolean BoolDivisor = false;
int divisor = 0;
while (BoolDivisor==false){
divisor = RandNumber(99);
if(divisor == 1 || divisor == 2 || divisor == 3 ||divisor == 5 || divisor == 7){
// the 1,2,3,5,7 is prime, so the while loop will continue to run.
}else {
for (int i = 2; i < 9; i++){
if(divisor % i == 0){
BoolDivisor = true; //since all the composite number must be divisble by 2/3/4/...../7, then stop the while loop
break;
}
}
}
}
return divisor;
}
public static int DivisionSmallerOne(int divisor){
int division;
//Random a num as a division which must be divisble by divisor
do{
division = RandNumber(40);
}while(divisor % division != 0 || division == 1);
return division;
}
public static int RandNumber(int RanRange){
return ((int)(Math.random() * RanRange + 1));
}
public static int AskInputForCalculation(int Number1, int Number2, String MathSymbols){
Scanner input = new Scanner(System.in);
System.out.print(Number1 + " " + MathSymbols + " " + Number2 +" =? ");
int playerAnswer = input.nextInt();
return playerAnswer;
}
public static void resultSentance(int NoOfQuestions, double mark){
System.out.println("You've answered " + NoOfQuestions +" questions.\n" + (int)mark + " of them are correct.");
System.out.printf("Your mark is %.2f%%\n" ,mark * 100 / NoOfQuestions);
}
}