Solved

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

Posted on 2014-12-14
7
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Samsung Tablette 22 98
spring maven example issues 3 97
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 32
IBM TS2900 (3572) Tape Autoloader Java? 12 104
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Suggested Courses

737 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