import java.util;
public class Compact
{
public static int[] randomArray (int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
return a;
}
public static double[] printArray (double[] a)
{
for (int i = 0; i<a.length; i++)
{
return a[i];
}
}
public static void main(String args[])
{
System.out.prin tln("Test for printing " + printArray(a));
}
}
In the above code, what I am trying to do is create an array with random integers. The size of the array is 20. I don't know how to call the array...
47 3996 JosAH 11,448
Recognized Expert MVP
Have you tried to compile it and read what the compiler had to say about it? btw,
you can't call an array, you might yell 'yoohoo' at it but I'm sure it won't react nor
respond. Arrays are kind of stupid.
kind regards,
Jos
Have you tried to compile it and read what the compiler had to say about it? btw,
you can't call an array, you might yell 'yoohoo' at it but I'm sure it won't react nor
respond. Arrays are kind of stupid.
kind regards,
Jos
import java.util;
public class Compact
{
public static void main(String[] args)
{
public static int[] randomArray (int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
System.out.prin tln (a);
}
}
}
this doesn't work for some reason, it says illegal start of expression at the randomArray method. and a semicolon is expected at the second to last bracket
BigDaddyLH 1,216
Recognized Expert Top Contributor
When your syntax is way off the mark, the resulting compiler messages may not be helpful, because the compiler is so confused. That is the case, here.
You nested the defintion of method randomArray inside method main. In other words, you wrote: -
void f() {
-
void g() {
-
-
}
-
}
-
instead of -
void f() {
-
-
}
-
-
void g() {
-
-
}
-
When your syntax is way off the mark, the resulting compiler messages may not be helpful, because the compiler is so confused. That is the case, here.
You nested the defintion of method randomArray inside method main. In other words, you wrote: -
void f() {
-
void g() {
-
-
}
-
}
-
instead of -
void f() {
-
-
}
-
-
void g() {
-
-
}
-
okay, one problem was fixed. But it still says "illegal start of expression" for the random array method.
BigDaddyLH 1,216
Recognized Expert Top Contributor
okay, one problem was fixed. But it still says "illegal start of expression" for the random array method.
I can only guess what your code looks like now. I suggest that when you are asking a question about a syntax error, you should post the relevant code.
I can only guess what your code looks like now. I suggest that when you are asking a question about a syntax error, you should post the relevant code.
srry
import java.util;
public class Compact
{
public static void main(String[] args)
{
public static int[] randomArray(int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
}
System.out.prin tln (a);
}
}
JosAH 11,448
Recognized Expert MVP
Your first line is wrong: you can't import a package, you import classes instead;
as in:
Compilers can be real nitpickers (which is AGT (A Good Thing (tm)))
kind regards,
Jos
BigDaddyLH 1,216
Recognized Expert Top Contributor
You are still nesting method randomArray inside method main. See reply #4.
ps. If you use code tags, your code will be more readable in this forum.
-
import java.util.Arrays;
-
-
public class Compact
-
{
-
public static void main(String[] args)
-
{
-
public static int[] randomArray(int n)
-
{
-
int[] a = new int[n];
-
for (int i = 0; i < a.length; i++)
-
{
-
a[i] = Math.random();
-
}
-
}
-
System.out.println (a);
-
}
-
}
-
Still doesn,t work, same error
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Virus |
last post by:
Ok well what I am trying to do is have
1.) the background color to change randomly with 5 different colors.(change
on page load)
2,) 10 different quotes randomly fadeing in and out in random spots on the
webpage. with a delay timer on them, so they keep changing as the page is
open. Not random each time the page is loaded.
If anyone can help it would be greatly appreaciated, I have tried many of
|
by: Jason |
last post by:
Hi,
I need a way to use random numbers in c++. In my c++ project, when using
the mingw compiler I used a mersenne twister that is publicly available and
this did its job well. Now I have shelled out on VC++ 6.0 compiling that
same code is proving difficult. I am not too worried how I generate random
numbers in c++, as long as it is sufficiently random.
Can anybody help me out in getting what I want from VC++?
|
by: Johnny Snead |
last post by:
Hey guys,
Need help with this random sort algorithm
private void cmdQuestion_Click(object sender, System.EventArgs e)
{
Random rnd = new Random(); //initialize rnd to new random object
System.Random iRnd = new System.Random();
string theNum = iRnd.Next(0,8).ToString();
lblAnswer.Text = iRnd.Next(0,8).ToString();
|
by: Arif |
last post by:
I C# code prints very slow as compared to a third party barcode printing
software. That software prints approximately 10 labels in 2 seconds while my
C# code prints 10 labels in 5 to 6 seconds. And this differences increases
with the increase number of labels.
The code is as follwods:
Here rdr = OleDbDataReader
Font is Times New Roman, 12pt
|
by: jar13861 |
last post by:
I'm confused on how to write a random array that will only generate 9
different numbers from 1-9. Here is what I have, but its only writing
one number....
holder = new Array ( 9 );
var flag = true;
var rannum = Math.floor( 1 + Math.random() * 9 );
for (var j = 0; j < 9; j++)
{
| |
by: Siv |
last post by:
Hi,
I am getting into printing with VB.NET 2005 and want to implement the usual
capability that a user can select a selection of pages. I have a report
that is generated by my application that if the user wants all pages will
produce 3 pages. I want to offer the user the ability to select via the
print dialog that only pages 1 and 2 of it are printed or possibly pages 1
and 3 but not 2.
At the moment I can produce all three pages...
|
by: Pascal |
last post by:
hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to split a number in
several pieces in a random way without success.
for example if the number is 123 456 : i would like to have some random
strings like theese :
(12 * 10 000) + (345 * 10) + (6*1)
or
(123*1 000)+(4*100)+(5*10)+(6*1)
etc...
|
by: Pao |
last post by:
My code works in this way:
I declared a static array in a class (public static int GVetRandom =
new int;)
that in a for cycle I fill with random numbers.
The array gets cleared (clear method) and refilled at each turn of
cycle.
On my developing pc, the same sequence of random numbers was repeated
from on turn of cycle to the other; I put some Application.DoEvents()
and all gone well.
|
by: TTCEric |
last post by:
This will be original. I promise.
I cannot get the random number generator to work.
I tried seeding with Date.Now.Milliseconds, it still results in the same values.
What I have are arrays of values.
I get a random index value for each array so I can pull the data from them.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |