#Building a small elementary math program

35 messages · Page 1 of 1 (latest)

craggy ravine
#

I can type the code myself as well but that won't help you learn it so here is the basic algorithm for this assignment

#

Firstly you need to use Math.random() method which will give you a number between 0<=x<1

#

Luckily this method can do logical operations and you teacher wanted you to use 1 digit integers

#

so you need to assign a int variable and make it equal to (int) (10*Math.random())

#

then you need to use an if statement and if the input is equal to this variable then print very good else you will say no please try again and use a loop and the loop condition is while the input is not equal to int variable repeat saying no please try again

#

when we get out of the loop which requires user to match the number you will print Very good once again

#

and you can expand your code with other operations such as for example you can say (int) (3*Math.random()+4) and for instance you can one of the results equal to "+" one of them to "-" one of them to "x" and one of them to "/" in another method

#

@eternal comet

craggy ravine
#

import java.util.Scanner;

public class Test {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int number_1 = (int) (Math.random()*10);
int number_2 = (int) (Math.random()*10);
System.out.println("What is the product of "+number_2 +" * "+number_1);
int variable = input.nextInt();
if (variable == number_1 * number_2)
System.out.println("Very Good");
else{
while (number_1 * number_2 != variable){
System.out.println("Please try again");
variable = input.nextInt();
}
System.out.println("Very Good");
}

#

PLEASE PLEASE DONT JUST COPY THIS AND UNDERSTAND THE ALGORITHM YOU WONT LEARN IF YOU JUST COPY WHAT I DO

#

This is just the beginning tho you will continue like this

eternal comet
craggy ravine
#

Yea man we do need to do that as i said

#

Here

craggy ravine
#

I just did the half of the assignment your teacher asks you to expand it and here is where to start

#

Like the basis is this and from here you will add on

#

as you said you can create new methods for operations and you can assign a variable named clock and you can make a while loop at the beginning of all the codes and you can say if the user puts the correct asnwer clock++ and generate next question and when we hit 10 true questions the second level begins which makes 3 random numbers and 3 random operations etc etc

#

Like this is not the assignment your teacher is asking this is the basis of your assignment so that you can understand the logic and algorithm and carry on

#

@eternal comet

eternal comet
craggy ravine
#

sure

eternal comet
craggy ravine
#

Im in lesson rn I'll tel u

#

why

eternal comet
craggy ravine
#

The reason is

#

when we get the answer correctly at 1 try we want the program to exit

#

if we get it wrong we will get into loop

#

that is why 2 very good

eternal comet
#

Do u mind if I ask u few more Qs later on if I have any questions while working on my assignment?

craggy ravine
#

sure