?
Solved

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

Posted on 2014-12-14
7
Medium Priority
?
222 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 28

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

In this post we will learn different types of Android Layout and some basics of an Android App.
If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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