unknown error

Printable View

• May 28th, 2013, 06:10 AM
njabulo ngcobo
unknown error
//7. Write a class called RandomNumbers to do the following:

//a. Write a method called generate to randomly generate a 1000 numbers in the range [50..100]. Find and return the sum of these numbers.
//b. Write a method called output to receive the sum. Output the sum

import java.util.*;
public class RandomNumbers
{

// declarations
int sum=0;

public static void main (String[]a)
{
// method to generate a 1000 numbers in the range [50..100]and sum up those numbers

public double random() <<<< that's where I get an error but I "illegal start of expression"
{
for(int i=1;i<=1000;i++)
{
int number=(int)(math.random()*51)+100;
}
sum=sum+number;
return sum;
}
}

public void output(int sum)
{
System.out.print("The sum is :"+sum);

}
}

can you pick up an error above?? please help
• May 28th, 2013, 06:12 AM
PhHein
Re: unknown error
Jeebus! Use code tags to format your code!
You cannot implement a method inside another method.
• May 28th, 2013, 06:33 AM
angstrem
Re: unknown error
If you really want to make methods inside methods, consider functional programming :)
• May 28th, 2013, 07:28 AM
njabulo ngcobo
Re: unknown error
now I am confussed

--- Update ---

can you tell me what axactlly do I need to change ,just a clue
• May 28th, 2013, 08:01 AM
angstrem
Re: unknown error
No, we can't, you must figure it out by ourself. If methods-within-methods are impossible, but you still need that method, then?.. What are your suggestions?
• May 29th, 2013, 03:23 AM
njabulo ngcobo
Re: unknown error
is it possible to find the highest number using for loop ( looking AT number 9a)

9. Write a class called Marks to do the following:
a. Write a method called findHi to input 10 marks. Find and return the highest mark.
b. Write a method called output to receive and output the highest mark.
• May 29th, 2013, 03:41 AM
jps
Re: unknown error
Quote:

Originally Posted by njabulo ngcobo
is it possible to find the highest number using for loop ( looking AT number 9a)

What happened when you tried it?
Post the code with your questions if you have any
• May 29th, 2013, 04:23 AM
njabulo ngcobo
Re: unknown error
this is what i tryed

import java.util.*;
public class Marks
{
public double findHi()
{
int Marks=0;

for(int i=1;i<=10;i++)
{
Scanner console=new Scanner(System.in);
System.out.println("Enter a mark");
Marks =console.nextInt();

//totalCost=price*5;
}
return Marks ;

}

public void output(int Marks)
{
System.out.println("The highest mark is : " +Marks);

}

}
• May 29th, 2013, 07:51 AM
angstrem
Re: unknown error
Consider [code=Java] /* your code here */ [//code] (the latter with ONE slash) to automatically highlight your code here.
What was your reasoning for developing this for loop's logic? As I see, your algorithm of "for" is following:
1. Before for loop create a variable Marks (btw, why starts with capital latter and why the name is identical to class's one?)
2. In for loop
2.1. Read a mark
2.2. Save it to Marks (overwrite it)
3. Return Marks
Why shouldn't it work? What algorithm should work?