Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Can anyone identify the error in this Java code?
This is the code and the error that I am getting is below. Any help would be appreciated!
public class Tentativa_100000 {
public static void main(String[] args) {
int n=(int)Math.floor(Math.random)*100000+1;
random= new random
int number= (int)Math.floor(Math.random)*100000+1;
int sum= 0;
int digit;
digit= n%10;
sum += digit;
n= n/10;
digit= n%10;
sum += digit;
n= n/10;
digit= n%10;
sum += digit;
n= n/10;
digit= n%10;
sum += digit;
System.out.println("Given number= "+ number+" ;sum of digits= "+sum);
ERROR:
int number= (int)Math.floor(Math.random)*100000+1;
^
1 error
Process completed.
3 Answers
- 1 decade ago
1) You need to change this line: int n=(int)Math.floor(Math.random)*100000+1; as
place a "()" after Math.random so that it becomes Math.random()
int n=(int)Math.floor(Math.random())*100000+1;
-----------
2) Likewise change line: int number= (int)Math.floor(Math.random)*100000+1; as
int number= (int)Math.floor(Math.random())*100000+1;
------------
3) line: random= new random
It seems that you were actually trying to create a java.util.Random object. If so the line should be
Random random = new Random();
However you do NOT need to create a Random object, since you are using Math.random() method so just remove the line ( random= new random )
Source(s): [ Math - Java Platform SE 6 ] http://java.sun.com/javase/6/docs/api/java/lang/Ma... [ Random - Java Platform SE 6 ] http://java.sun.com/javase/6/docs/api/java/util/Ra... - 1 decade ago
What is random = new random ? If you're trying to use the Object Random, you need
Random {var name} = new Random() as I don't see random declared anywhere in your code.
You dont' use it anywhere, so not sure why you have it in there.
However, if you'd like your code to compile Math.random is a static method, so you should read Math.random()
Source(s): 10 years of Java programming - Anonymous1 decade ago
random= new random;
You forgot the ;