d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Do This?
Add Reply New Topic New Poll
Member
Posts: 31,401
Joined: Mar 25 2009
Gold: 134.00
Dec 29 2020 07:43pm
So i have this math game i made, but i re-use the same code rather than uniting it into one method.... Wondering how you would do this? Could someone change the code so it only uses 1 method rather then using multiple methods for different math operations? Hopefully this makes sense, ty!

I have 4 methods for different math operations:
additionQuestions()
subtractionQuestions();
divisionQuestions();
multiplicationQuestions();

Wondering how i would put all that into 1 method? If you can adjust my code to do so, that'd be great, so i can understand it completely. ty!











Code
package com.company;

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class FerfsMathGame {




public static void main(String[] args) {


try {

mainMenu();

} catch (InputMismatchException e) {
System.out.println("Not a valid number!");
mainMenu();
} catch (Exception e) {
e.printStackTrace();
}


}


public static void mainMenu() {
boolean game = true;

while (game) {

System.out.println("Welcome to ferfys math game!!");
System.out.println("What subject of math would you like testing in?");
System.out.println("Choose 1-4: 1-Addition, 2-Subtraction, 3-Division, 4-Multiplication, 5-for answer statistics");
System.out.println("Or 0 to exit program");
Scanner scan = new Scanner(System.in);
int subjectNumber = scan.nextInt();


switch (subjectNumber) {
case 1:
additionQuestions();
break;
case 2:
subtractionQuestions();
break;
case 3:
divisionQuestions();
break;
case 4:
multiplicationQuestions();
break;
case 5:
totalQuestions = numCorrect + numWrong;
percentCorrect = Math.round(((float) numCorrect / totalQuestions) * 100);
System.out.println();
System.out.println("You had " + numCorrect + " correct answers, and " + numWrong + " wrong answers! Which would be: " + percentCorrect + "% correct!" + " There were " + totalQuestions + " total questions!!");
System.out.println();
break;
case 0:
System.out.println("Quitting ferfys math game...");
game = false;

}
}

}






public static int numCorrect;
public static int numWrong;
public static int totalQuestions;
public static float percentCorrect;







public static void additionQuestions() {
Scanner scan = new Scanner(System.in);


System.out.println("What difficulty would you like?: 1 for Beginner, 2 for intermediate, or 3 for advanced!");
int difficulty = scan.nextInt();


if (difficulty == 1) {

System.out.println("How many addition questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(10);
int num2 = random.nextInt(10);
System.out.println("What is " + num1 + " + " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 + num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
} else if (difficulty == 2) {
System.out.println("How many addition questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(89) + 10;
int num2 = random.nextInt(89) + 10;
System.out.println("What is " + num1 + " + " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 + num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}

} else if (difficulty == 3) {
System.out.println("How many addition questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(899) + 100;
int num2 = random.nextInt(899) + 100;
System.out.println("What is " + num1 + " + " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 + num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
}


}


public static void subtractionQuestions() {
Scanner scan = new Scanner(System.in);

System.out.println("What difficulty would you like?: 1 for Beginner, 2 for intermediate, or 3 for advanced!");
int difficulty = scan.nextInt();


if (difficulty == 1) {

System.out.println("How many subtraction questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(10);
int num2 = random.nextInt(10);
System.out.println("What is " + num1 + " - " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 - num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
} else if (difficulty == 2) {
System.out.println("How many subtraction questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(89) + 10;
int num2 = random.nextInt(89) + 10;
System.out.println("What is " + num1 + " - " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 - num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
} else if (difficulty == 3) {
System.out.println("How many subtraction questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(899) + 100;
int num2 = random.nextInt(899) + 100;
System.out.println("What is " + num1 + " - " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 - num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
}


}


public static void divisionQuestions() {
Scanner scan = new Scanner(System.in);

System.out.println("What difficulty would you like?: 1 for Beginner, 2 for intermediate, or 3 for advanced!");
int difficulty = scan.nextInt();


if (difficulty == 1) {
System.out.println("How many division questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(9) + 1;
int num2 = random.nextInt(9) + 1;
System.out.println("What is " + num1 + " / " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 / num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
} else if (difficulty == 2) {
System.out.println("How many division questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(89) + 10;
int num2 = random.nextInt(89) + 10;
System.out.println("What is " + num1 + " / " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 / num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
} else if (difficulty == 3) {
System.out.println("How many division questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(899) + 100;
int num2 = random.nextInt(899) + 100;
System.out.println("What is " + num1 + " / " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 / num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
}


}


public static void multiplicationQuestions() {
Scanner scan = new Scanner(System.in);

System.out.println("What difficulty would you like?: 1 for Beginner, 2 for intermediate, or 3 for advanced!");
int difficulty = scan.nextInt();

if (difficulty == 1) {

System.out.println("How many multiplication questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(10);
int num2 = random.nextInt(10);
System.out.println("What is " + num1 + " * " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 * num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
} else if (difficulty == 2) {
System.out.println("How many multiplication questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(89) + 10;
int num2 = random.nextInt(89) + 10;
System.out.println("What is " + num1 + " * " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 * num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
} else if (difficulty == 3) {
System.out.println("How many multiplication questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
Random random = new Random();
int num1 = random.nextInt(899) + 100;
int num2 = random.nextInt(899) + 100;
System.out.println("What is " + num1 + " * " + num2 + "?");
int answer = scan.nextInt();

if (answer == num1 * num2) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
}


}


}



Member
Posts: 7,444
Joined: Apr 29 2009
Gold: 1.04
Jan 6 2021 06:13pm
This should work give it a try. Also you need to finish up the calculateAnswer function.

Code
public static void mathQuestions(String typeOfQuestions){
System.out.println("What difficulty would you like?: 1 for Beginner, 2 for intermediate, or 3 for advanced!");
int difficulty = scan.nextInt();
Random random = new Random();
int numRange1 = difficulty == 1 ? random.nextInt(10) : difficulty == 2 ? random.nextInt(89) + 10 : random.nextInt(899) + 100;
int numRange2 = difficulty == 1 ? random.nextInt(10) : difficulty == 2 ? random.nextInt(89) + 10 : random.nextInt(899) + 100;

System.out.println("How many addition questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
int actualAnswer = calculateAnswer(typeOfQuestions, numRange1,numRange2)
System.out.println("What is " + num1 + " + " + num2 + "?");
int userAnswer = scan.nextInt();

if (userAnswer == actualAnswer) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
}

public int calculateAnswer(String typeOfQuestions, int r1, int r2){
//TODO: FINISH UP THIS CODE
}
Member
Posts: 31,401
Joined: Mar 25 2009
Gold: 134.00
Jan 9 2021 06:40pm
Quote (chanwolf @ Jan 6 2021 08:13pm)
This should work give it a try. Also you need to finish up the calculateAnswer function.

Code
public static void mathQuestions(String typeOfQuestions){
System.out.println("What difficulty would you like?: 1 for Beginner, 2 for intermediate, or 3 for advanced!");
int difficulty = scan.nextInt();
Random random = new Random();
int numRange1 = difficulty == 1 ? random.nextInt(10) : difficulty == 2 ? random.nextInt(89) + 10 : random.nextInt(899) + 100;
int numRange2 = difficulty == 1 ? random.nextInt(10) : difficulty == 2 ? random.nextInt(89) + 10 : random.nextInt(899) + 100;

System.out.println("How many addition questions, would you like?");
int numberOfQuestions = scan.nextInt();

for (int i = 1; i <= numberOfQuestions; i++) {
int actualAnswer = calculateAnswer(typeOfQuestions, numRange1,numRange2)
System.out.println("What is " + num1 + " + " + num2 + "?");
int userAnswer = scan.nextInt();

if (userAnswer == actualAnswer) {
System.out.println("Your solution was correct!!");
numCorrect++;
} else {
System.out.println("Your solution was wrong");
numWrong++;
}
}
}

public int calculateAnswer(String typeOfQuestions, int r1, int r2){
//TODO: FINISH UP THIS CODE
}


Thanks! makes sense... BUT, i'm confused about this?

System.out.println("What is " + num1 + " + " + num2 + "?");



^ where is num1/num2? I don't see them.......

This post was edited by ferf on Jan 9 2021 06:42pm
Member
Posts: 7,444
Joined: Apr 29 2009
Gold: 1.04
Jan 12 2021 03:14pm
Quote (ferf @ Jan 9 2021 04:40pm)
Thanks! makes sense... BUT, i'm confused about this?

System.out.println("What is " + num1 + " + " + num2 + "?");



^ where is num1/num2? I don't see them.......


replace that with numRange1 and numRange2
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll