Skip to main content
gamedev.net
🔒 Locked

Java Help

Started by Narusuke Oct 9, 2009 at 12:21 PM 6 replies 1.4k views
Original Post
Narusuke
Narusuke
Im trying to help a friend who just started a java class, and we are a bit stuck. We thought we had the first problem but it wont add up to what its supposed to. And for the second project we are just completely lost. Anyway here is the first program, its supposed to randomly generate 5 numbers and add them together. class problem

{
public static void main(String[] args)

{
int number, x = (int)Math.floor(Math.random()*100000+1);
int sum = 0;
int digit;

number = x;
x = x/10;
digit = x % 10;
sum += digit;
x = x/10;
digit = x % 10;
sum += digit;
x = x/10;
digit = x % 10;
sum = x/10;
x = x/10;
digit = x % 10;
sum += digit;
x = x/10;
digit = x % 10;
sum += digit;
System.out.printf("Gien number =%6d and the sum of digits = %6d", number, sum);
 }
}

Problem 2, this one we were completely lost on. Write a program to randomly generate a 3-digit number N (it is ok if the number has fewer than 3 digits). Create a number NN which is N placed next to it. Example if the number N is 263 then NN is 263263. If N is 7, NN is 007007. If N is 032, NN is 32032. Next Divide NN by 7, and then by 11 and then 13 Write the original number and the final result after the divisions. Formula to randomly generate a 3-digit number is (int)Math.floor(Math.random()*1000+1) [Edited by - Narusuke on October 9, 2009 12:38:56 PM]
alvaro
alvaro
Quote:
Original post by Narusuke
Im trying to help a friend who just started a java class, and we are a bit stuck. We thought we had the first problem but it wont add up to what its supposed to. And for the second project we are just completely lost.
Anyway here is the first program, its supposed to randomly generate 5 numbers and add them together.
[...]

Please, surround pieces of source code by [source] and [/source]. I can see two obvious problems with your code:
1) You shouldn't add 1 to Math.random()*100000 before you convert it to an int: 0 is a perfectly good 5-digit number, but 100000 is not.
2) You should first extract the last digit and then remove it from x. You are doing it the other way around.

Also, why don't you simply call Math.random() five times, get the corresponding 5 digits and add the results? Isn't that what the problem tells you to do?

Quote:
Problem 2, this one we were completely lost on.

Write a program to randomly generate a 3-digit number N (it is ok if the number has fewer than 3 digits). Create a number NN which is N placed next to it. Example if the number N is 263 then NN is 263263. If N is 7, NN is 007007. If N is 032, NN is 32032.
Next Divide NN by 7, and then by 11 and then 13
Write the original number and the final result after the divisions.
Formula to randomly generate a 3-digit number is
(int)Math.floor(Math.random()*1000+1)

That formula is incorrect, again, for the same reason as before: Remove the `+1'.

I'll give you a hint: 263263 = 263 * 1001
Narusuke
Narusuke
Thanks for the response, were trying it the other way now. But we need to convert an interger into a string, how exactly would I go about doing that?
alvaro
alvaro
Quote:
Original post by Narusuke
Thanks for the response, were trying it the other way now. But we need to convert an interger into a string, how exactly would I go about doing that?

No, you don't. Why do you think you need to convert the integer into a string?
Narusuke
Narusuke
Just trying to figure out how to do it. I have the statement of String.valueOf but not sure how to use it.
alvaro
alvaro
Quote:
Original post by Narusuke
Just trying to figure out how to do it. I have the statement of String.valueOf but not sure how to use it.


I wouldn't complicate my life with strings if you have trouble implementing these simple programs. The second program asks you to compute a random 3-digit number and print it twice. Really, if you read it carefully, it comes down to just that.

EDIT: Be careful with leading zeros in Java (or C, or C++, or Perl...) because they are not no-ops. The literal 007007 is actually 3591 (it's interpreted in octal).
Narusuke
Narusuke
thanks
Fenrisulvur
Fenrisulvur
Quote:
Original post by Narusuke
sum = x/10;

You noticed this snippet in your code, right? You were flooring your sum variable to zero halfway through.

It may be a better idea to go with a loop next time, so you don't have to review fifty-bajillion redundant lines of code. Like:

number = x;count = 0;while (count < 5){    digit = x % 10;    sum += digit;    x = x/10;    count++;}


EDIT: can't source tags appear within quote tags? Gah.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.