This is a program where you are asked what operation you would like (addition, subtraction etc), what range of numbers you'd like, and then it creates an exercise then it asks you for the answer.
Feel free to comment about anything. It is my first time showing my code to other people so it's bound to be crappy.
import java.util.Scanner;
import java.util.Random;
public class mathWork {
public static void main(String[] args) {
SetUp setUp = new SetUp();
UserChoices user = new UserChoices();
setUp.chooseNumberRange(user);
System.out.println("Your max range is! " + user.getUserMaxRange());
System.out.println("Your chosen operator is " + user.getUserOperator());
CreateExercise create = new CreateExercise();
create.generateExercise(user);
}
}
class SetUp {
public void chooseNumberRange(UserChoices user) {
//These are the possible number ranges for a math exercise.
int min1 = 0;
int max1 = 9;
int min2 = 10;
int max2 = 100;
Scanner scan = new Scanner(System.in);
//Here we prompt the user to choose his type of math exercise.
System.out.println("Welcome to Math Work! Please select your "
+ "choice of operation: \n" + " 1.Addition"
+ "\n 2.Subtraction" + "\n 3.Multiplicaiton"
+ "\n 4.Division");
int userOperatorChoice = scan.nextInt();
switch (userOperatorChoice) {
case 1:
user.setUserOperator('+');
break;
case 2:
user.setUserOperator('-');
break;
case 3:
user.setUserOperator('*');
break;
case 4:
user.setUserOperator('/');
break;
}
//Here we prompt the user to choose what range of numbers he'd want in a math drill
System.out.println("Now please select preferred number"
+ " range. Your options are: \n 1." + min1 + " to " + max1
+ "\n 2." + min2 + " to " + max2);
int userRangeChoice = scan.nextInt();
switch(userRangeChoice) {
case 1 :
user.setUserMinRange(min1);
user.setUserMaxRange(max1);
break;
case 2 :
user.setUserMinRange(min2);
user.setUserMaxRange(max2);
break;
}
}
}
class UserChoices {
//Here we store the user choices
private int userMinRange = 0;
private int userMaxRange = 0;
private char operator;
public void setUserMinRange(int u) {
userMinRange = u;
}
public int getUserMinRange() {
return userMinRange;
}
public void setUserMaxRange(int u) {
userMaxRange = u;
}
public int getUserMaxRange() {
return userMaxRange;
}
public void setUserOperator(char o) {
operator = o;
}
public char getUserOperator() {
return operator;
}
}
class CreateExercise {
//Here we create the exercises and prompt the user for an answer
public void generateExercise(UserChoices user) {
Random rng = new Random();
Scanner scan = new Scanner(System.in);
int max = user.getUserMaxRange();
int min = user.getUserMinRange();
int i = 0;
while(i < 10) {
int firstNum = rng.nextInt((max + 1) - min) + min ;
int secondNum = rng.nextInt((max + 1) - min) + min ;
System.out.println("What is the answer to: \n" +
firstNum + user.getUserOperator() + secondNum);
switch(user.getUserOperator()){
case '+':
if (scan.nextInt() == (firstNum+secondNum)) {
System.out.println("Correct!");
break;
} else {System.out.println("Wrong");}
case '-':
if (scan.nextInt() == (firstNum-secondNum)) {
System.out.println("Correct!");
break;
} else {System.out.println("Wrong");}
case '*':
if (scan.nextInt() == (firstNum*secondNum)) {
System.out.println("Correct!");
break;
} else {System.out.println("Wrong");}
case '/':
if (scan.nextInt() == (firstNum/secondNum)) {
System.out.println("Correct!");
break;
} else {System.out.println("Wrong");}
i++;
}
}
}
}