Hello java. Welcome to the forums
I have wrote this example for you to look at.
There is no error handling to check what the user inputs but the functionality is there.
import java.util.Scanner;
public class JavaQuestions {
/**
* JavaProgrammingForums.com
*/
// Global variables
static String[] rangeArray = null;
static int startRange, endRange;
public void userInput(){
// Scanner class to take user input
Scanner sc = new Scanner(System.in);
System.out.println("Enter a range between 1 and 20. eg 5-10: ");
String firstRange = sc.nextLine();
// Split the input String
rangeArray = firstRange.split("-");
// Assign range values
startRange = Integer.parseInt(rangeArray[0]);
endRange = Integer.parseInt(rangeArray[1]);
// doMaths method
doMaths();
}
public void doMaths(){
System.out.println("Questions:");
// Loop 5 times and print
for(int i = 0; i < 5; i++){
// Generate random number between startRange and endRange
int randomPt1 = (int)(Math.random() * (endRange - startRange + 1) ) + startRange;
// Generate random number between 1 and 10
int randomPt2 = (int)(Math.random() * (10 - 1 + 1) ) + 1;
// Print math questions
System.out.println(randomPt1 + " * " + randomPt2 + " = ?");
}
}
public static void main(String[] args) {
JavaQuestions j = new JavaQuestions();
j.userInput();
}
}
Example output:
Enter a range between 1 and 20. eg 5-10:
1-20
Questions:
11 * 5 = ?
13 * 5 = ?
8 * 5 = ?
11 * 8 = ?
20 * 1 = ?