Solved

What is the best way to generate a random addition problem?

Posted on 2014-12-14
7
213 Views
Last Modified: 2014-12-15
Hi,

What is the best way to generate a random addition problem in android on a textview using only numbers 1-9?

8 + 7 = ?
0
Comment
Question by:Computer Guy
7 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 40499153
Math.rand. No? Tried it?
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40499168
Example?
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40499180
You want something like this:

Random r = new Random() ;
int first       = r.nextInt(9) + 1 ; // 1-9
int second = r.nextInt(9) + 1 ; // 1-9

String problem = Integer.toString(first) + " + " + Integer.toString(second) + " = ? " ;

Now show 'problem' in the text view.

Doug
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 40499200
Hi!

To generate random number between 1 - 9 you can do this

Random rn = new Random();
int firstnum = rn.nextInt(9) + 1;
int secondnum = rn.nextInt(9) + 1;

Then simply present it using SetText method in TextView
http://www.java2s.com/Code/Android/UI/TextView.htm

I suggest you have a class that generates the math equation to solve and does the solving.

Regards,
    Tomas Helgi
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40499223
Also how can I generate the answer?

I have 3 buttons below the question I need to generate 2 random numbers and the other button needs to be the answer.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40499266
BTW I meant Math.random() - got my languages wrong. ;)
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40499388
I need to generate 2 random numbers

All you need is

int r_1 = (int)(Math.random()*10);
int r_2 = (int)(Math.random()*10);
int answer = r_1+r_2;

Open in new window


Java.util.Random is for streams of numbers. Doesn't look like you need that.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb message driven bean mdb creation steps 2 35
junit as external jar or library 7 38
program arguments vs VM arguments 4 28
How to log java errors in tomcat 8 24
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question