> How to write a simple Java program to multiply any two numbers (lower than ten) that are randomly generated?

How to write a simple Java program to multiply any two numbers (lower than ten) that are randomly generated?

Posted at: 2014-12-18 
I can get the random numbers generated and I can multiply numbers, I just cannot multiply numbers that are randomly generated!

Here is a program which simply reads two numbers, and gives the result of adding them together and multiplying them together:

1 import java.io.*;

2

3 class TwoNums0

4 {

5 // A program that prompts for two numbers then prints their sum and product

6

7 public static void main (String[] args) throws IOException

8 {

9 int m,n;

10 BufferedReader in = Text.open(System.in);

11 System.out.print("Enter first number: ");

12 m=Text.readInt(in);

13 System.out.print("Enter second number: ");

14 n=Text.readInt(in);

15 System.out.println("Adding the two gives: "+(m+n));

16 System.out.println("Multiplying the two gives: "+m*n);

17 }

18 }

Put each randomly generated number into a variable. Now multiply the two variables.

try this

import java.util.Random;

public class Program {

public static void main(String[] args) {

int first = getRandomNumber(1, 10);

int second = getRandomNumber(1, 10);

System.out.printf("%d * %d = %d%n", first, second, multiply(first, second));

}

private static int getRandomNumber(int min, int max) {

return new Random().nextInt(max - min + 1) + min;

}



private static int multiply(int first, int second) {

return first * second;

}

}

import java.util.Random;



public class Example {

public static void main(String[] args) {



Random r = new Random();



int i1 = r.nextInt(10);

int i2 = r.nextInt(10);

int total = i1*i2;

System.out.println( i1+"*"+i2+"="+total );

}

}

I can get the random numbers generated and I can multiply numbers, I just cannot multiply numbers that are randomly generated!