d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Go About Doing This?
Add Reply New Topic New Poll
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
May 17 2020 09:13pm
Code
package com.company;

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

public class MathTryCatch {


public static void main(String[] args) {


mainMenu();


}


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");
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 0:
System.out.println("Quitting ferfys math game...");
game = false;

}
}

}


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

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!!");
} else {
System.out.println("Your solution was wrong");
}
}
}


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

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!!");
} else {
System.out.println("Your solution was wrong");
}
}
}


public static void divisionQuestions() {

try {
Scanner scan = new Scanner(System.in);

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

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();
scan.nextLine();

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

}
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!!!");
}
}


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

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!!");
} else {
System.out.println("Your solution was wrong");
}
}
}


}












So after catch the exception it redoes the entire loop, i'm wondering how i would go about continuing the questions....
for example, say you select divide, then input 20 questions... when dividebyzero it exits those questions and redoes the loop... how do i finish the questions in catch statement? thanks.


and i know there are a lot of issues/things i could do better, i don't care, i'm learning very specific things atm.
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
May 18 2020 01:03pm
In the divide method, forgot to mention that
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
May 18 2020 04:30pm
Code
int num2 = random.nextInt(9) + 1;
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
May 18 2020 04:32pm
Or put try-catch in for loop
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
May 18 2020 04:33pm
Quote (waraholic @ May 18 2020 05:30pm)
Code
int num2 = random.nextInt(9) + 1;


alraedy did this in a different version of the code...

Quote (waraholic @ May 18 2020 05:32pm)
Or put try-catch in for loop


Hmm will experiment with this. thanks

Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
May 21 2020 05:26pm
Code
public static void divisionQuestions() {

Scanner scan = new Scanner(System.in);

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

for (int i = 1; i <= numberOfQuestions; i++) {

try {

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();
scan.nextLine();

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

} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!!!");
}

}
}





This works as i intended it to be.... :) thanks Warah0olic
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
Jun 21 2020 06:33am
What is 3 / 5?

0.6

Code
int answer = scan.nextInt();



Exception in thread "main" java.util.InputMismatchException


What is 8 / 0?
Can't answer :(


Suggestions:
get answers as strings, then use a function to determine what to do with the answer ?
make a case to allow answering divide by zero questions ?


This post was edited by moutonguerrier on Jun 21 2020 06:37am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll