Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJorge Ponce
2,252 PointsNow limit the range of the randomNumber variable from 0 to 9. Use the version of nextInt() that takes a parameter specif
Now limit the range of the randomNumber variable from 0 to 9. Use the version of nextInt() that takes a parameter specifying how many numbers to choose from.
Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(3); fact = randomNumber + "" ;
why this test don't accept my option because he said use a number 0 to 9 i put 3 example a said are a error.
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(3);
4 Answers
Seth Kroger
56,414 PointsnextInt(3)
will only give you a number from 0 to 2, not 0 to 9. You want to use a number that will let nextInt give you a result of 0 to 9.
Jorge Ponce
2,252 PointsRandom randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(0);
Jorge Ponce
2,252 Pointsthanks i put 10 a ready
but explaim me this Create a String variable named intAsString. Convert the randomNumber integer to a String value and store it in the intAsString variable. Hint: Add them together like in the video!
thanh van giang
376 PointsI have a question: Why this one work: 1. Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(10); but this one doesn't work: 2. Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(n:10); I thought I saw the lecturer did the method 2 in the video. But when I put n:10, it did not work so I changed to method 1: (10) and it worked. many thanks for the explanation!
Ben Jakuben
Treehouse TeacherI don't want to give you the answer outright, but let's try this: I'll give you all the pieces you need for that final line, and you'll just need to figure out how to configure them in the right way to meet that last requirement.
String
intAsString
=
randomNumber
+
""
;
Remember that you'll start by declaring a variable named intAsString
. You need the type to be String
. Then you set it equal to something. In this case you want to set it to the random number stored in a variable. But since that number is an int
type, you need to convert it to a String
, which you can do using the same shortcut like we did in the previous video.
Jorge Ponce
2,252 PointsJorge Ponce
2,252 Pointsyeah man i put the number (9) and said a error epxlaim much better please because a i'm losing the time here.
Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(9); here put the limite 0 to 9
Seth Kroger
56,414 PointsSeth Kroger
56,414 PointsForgot the bound on nextInt is exclusive, i.e., the value from nextInt will be from 0 to bound-1 but won't include bound. Edited my answer to reflect that.