Created
July 9, 2015 20:25
-
-
Save ayakimchuk322/63a15e35d3f872660795 to your computer and use it in GitHub Desktop.
just a tiny program from a newcomer to java to test a bit your brain power in basic mathematics operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package trainyourbrain; | |
import java.util.*; | |
/** | |
* | |
* @author Lalabol | |
*/ | |
public class TrainYourBrain { | |
/** | |
* @param args the command line arguments | |
*/ | |
String userChoice; | |
double argumentMaxNumber; | |
int maxNumber; | |
int question1; | |
int question2; | |
int question3; | |
boolean userWant; | |
int rightAnswers; | |
int numberOfTries; | |
int numberOfSkips; | |
public static void main(String[] args) { | |
System.out.println("Hello and welome to Train Your Brain!"); | |
System.out.println("Press 1-5 to choose desired algebraic operator:"); | |
System.out.println("1.Addition"); | |
System.out.println("2.Subtraction"); | |
System.out.println("3.Multiplication"); | |
System.out.println("4.Division"); | |
System.out.println("5.Exponential"); | |
TrainYourBrain game = new TrainYourBrain(); | |
game.availableOperators(); | |
}//end of a main method | |
public String getUserChoice(){ | |
String[] arrayOfOperators = {"Addition", "Subtraction", "Multiplication", "Division", "Exponential"}; | |
int numberOperator; | |
Scanner inputUserChoice = new Scanner (System.in); | |
System.out.println("Please enter valid number from 1 to 5"); | |
String userChoice = inputUserChoice.next(); | |
try{ | |
numberOperator = Integer.parseInt(userChoice); | |
System.out.println("You have chose " + userChoice + "." + arrayOfOperators[numberOperator - 1]); | |
return userChoice; | |
}catch (java.lang.ArrayIndexOutOfBoundsException outOfRange){ | |
getUserChoice(); | |
}catch (java.lang.NumberFormatException numberFormat){ | |
getUserChoice(); | |
} | |
return userChoice; | |
}//end of a method getUserChoice() | |
public int getUserMaxNumber(){ | |
try{ | |
Scanner inputMaxNumber = new Scanner(System.in); | |
System.out.println("Enter maximum available number:"); | |
int maxNumber = inputMaxNumber.nextInt(); | |
System.out.println("You have entered " + maxNumber); | |
return maxNumber; | |
}catch (java.util.InputMismatchException notANumber){ | |
System.out.println("You have entered not a valid number"); | |
getUserMaxNumber(); | |
} | |
return maxNumber; | |
}//end of a method getUserMaxNumber() | |
public void availableOperators(){ | |
String c = getUserChoice(); | |
switch (c){ | |
case "1": | |
argumentMaxNumber = getUserMaxNumber(); | |
operatorAddition(argumentMaxNumber); | |
break; | |
case "2": | |
argumentMaxNumber = getUserMaxNumber(); | |
operatorSubtraction(argumentMaxNumber); | |
break; | |
case "3": | |
argumentMaxNumber = getUserMaxNumber(); | |
operatorMultiplication(argumentMaxNumber); | |
break; | |
case "4": | |
argumentMaxNumber = getUserMaxNumber(); | |
operatorDivision(argumentMaxNumber); | |
break; | |
case "5": | |
argumentMaxNumber = getUserMaxNumber(); | |
operatorPow(argumentMaxNumber); | |
break; | |
default: | |
System.out.println("Choose the right option:"); | |
availableOperators(); | |
} | |
}//end of a method availableOperators() | |
public void operatorAddition(double o){ | |
do{ | |
question1 = (int)(Math.random() * argumentMaxNumber); | |
question2 = (int)(Math.random() * argumentMaxNumber); | |
question3 = question1 + question2; | |
System.out.println("How much is " + question1 + " + " + question2 + " ?"); | |
System.out.println("Enter 0 to skip this question"); | |
evaluateUserAnswer(); | |
} while(userWantToPlay()); | |
finishGame(); | |
}//end of a method operatorAddition() | |
public void operatorSubtraction(double o){ | |
do{ | |
question1 = (int)(Math.random() * argumentMaxNumber); | |
question2 = (int)(Math.random() * argumentMaxNumber); | |
question3 = question1 - question2; | |
System.out.println("How much is " + question1 + " - " + question2 + " ?"); | |
System.out.println("Enter 0 to skip this question"); | |
evaluateUserAnswer(); | |
} while(userWantToPlay()); | |
finishGame(); | |
}//end of a method operatorSubtraction() | |
public void operatorMultiplication(double o){ | |
do{ | |
question1 = (int)(Math.random() * argumentMaxNumber); | |
question2 = (int)(Math.random() * argumentMaxNumber); | |
question3 = question1 * question2; | |
System.out.println("How much is " + question1 + " x " + question2 + " ?"); | |
System.out.println("Enter 0 to skip this question"); | |
evaluateUserAnswer(); | |
} while(userWantToPlay()); | |
finishGame(); | |
}//end of a method operatorMultiplication() | |
public void operatorDivision(double o){ | |
do{ | |
question1 = (int)(Math.random() * argumentMaxNumber); | |
question2 = (int)(Math.random() * argumentMaxNumber); | |
try{ | |
question3 = Math.max(question1, question2) / Math.min(question1, question2); | |
System.out.println("What is the algebraic quotient of division of " + Math.max(question1, question2) + " by " + Math.min(question1, question2) + " ?"); | |
} catch(java.lang.ArithmeticException divisionByZero){ | |
continue; | |
} | |
System.out.println("Enter 0 to skip this question"); | |
evaluateUserAnswer(); | |
} while(userWantToPlay()); | |
finishGame(); | |
}//end of a method operatorDivision() | |
public void operatorPow(double o){ | |
do{ | |
question1 = (int)(Math.random() * argumentMaxNumber); | |
question2 = (int)(Math.random() * 3 +1); | |
question3 = (int)(Math.pow(question1, question2)); | |
System.out.println("How much is " + question1 + " raised to the power of " + question2 + " ?"); | |
System.out.println("Enter 0 to skip this question"); | |
evaluateUserAnswer(); | |
} while(userWantToPlay()); | |
finishGame(); | |
}//end of a method operatorPow() | |
public boolean userWantToPlay(){ | |
System.out.println("Do you want to play more? Y/N"); | |
Scanner userWantPlay = new Scanner(System.in); | |
String wantPlay = userWantPlay.next(); | |
switch (wantPlay){ | |
case ("y"): | |
userWant = true; | |
break; | |
case ("n"): | |
userWant = false; | |
break; | |
default: | |
System.out.println("Please enter Y/N"); | |
userWantToPlay(); | |
} | |
return userWant; | |
}//end of a method userWantToPlay() | |
public void evaluateUserAnswer(){ | |
boolean want = true; | |
while (want){ | |
try{ | |
Scanner scUserAnswer = new Scanner(System.in); | |
int userAnswer = scUserAnswer.nextInt(); | |
if (userAnswer == question3){ | |
System.out.println("Congrats! You are right!"); | |
++rightAnswers; | |
break; | |
} else if (userAnswer == 0){ | |
++numberOfSkips; | |
break; | |
} else{ | |
System.out.println("Ouch! Try again!"); | |
System.out.println("Enter 0 to skip this question"); | |
++numberOfTries; | |
} | |
} catch(java.util.InputMismatchException notANumber){ | |
System.out.println("You have entered not a valid number"); | |
System.out.println("Please enter valid answer"); | |
} | |
} | |
}//end of a method evaluateUserAnswer() | |
public void finishGame(){ | |
System.out.println("Game over"); | |
if(rightAnswers > 1 && numberOfTries == 0 && numberOfSkips == 0){ | |
System.out.println("Your have scored " + rightAnswers + " right answers without wrong tries or skips!!!"); | |
} else{ | |
switch (rightAnswers){ | |
case 0: | |
System.out.println("Your have scored 0 right answers with " + numberOfTries + " wrong try(s) and " + numberOfSkips + " skip(s)!"); | |
System.out.println("Go learn some math!"); | |
break; | |
case 1: | |
System.out.println("Your have scored 1 right answer with " + numberOfTries + " wrong try(s) and " + numberOfSkips + " skip(s)!"); | |
break; | |
default: | |
System.out.println("Your have scored " + rightAnswers + " right answers with " + numberOfTries + " wrong try(s) and " + numberOfSkips + " skip(s)!"); | |
} | |
} | |
}//end of a method finishGame() | |
}//end of a class TrainYourBrain |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment