Results 1 to 2 of 2
Thread: error when i use switch-case
- 12-08-2011, 11:56 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
error when i use switch-case
Hi, i have written the following code and i got tons of errors, dont know why...please any guidance
and here are the error messages:Java Code:import java.lang.*; public class TestSwitch { public static Double[] test(int i)// i should be from 1 to 4 { Double []prob = new Double[4]; double x,y,z,x2,y2,z2; Random rand = new Random(); switch(i) { case 1: prob[i-1] = 25.0 + (rand.nextDouble(75) + 1); x = 100 - prob[i-1]; y = x/3; z = y * 2; prob[1]= y + rand.nextDouble(z); x2 = x-prob[1]; y2 = x2/2; prob[2] = y2 + rand.nextDouble(y2); prob[3] = 100 - ( prob[i-1] + prob[1] + prob[2] ); break; case 2: prob[0] = 25 + rand.nextDouble(75) + 1; x = 100 - prob[0]; y = x/3; z = y * 2; prob[i-1] = y + rand.nextDouble(z); x2 = x-prob[i-1]; y2 = x2/2; prob[2] = y2 + rand.nextDouble(y2); prob[3] = 100 - ( prob[0] + prob[i-1] + prob[2] ); break; case 3: prob[0] = 25 + rand.nextDouble(75) + 1; x = 100 - prob[0]; y = x/3; z = y * 2; prob[1]= y + rand.nextDouble(z); x2 = x-prob[1]; y2 = x2/2; prob[i-1] = y2 + rand.nextDouble(y2); prob[3] = 100 - ( prob[0] + prob[1] + prob[i-1] ); break; case 4: prob[0] = 25 + rand.nextDouble(75) + 1; x = 100 - prob[0]; y = x/3; z = y * 2; prob[1]= y + rand.nextDouble(z); x2 = x-prob[1]; y2 = x2/2; prob[2] = y2 + rand.nextDouble(y2); prob[i-1] = 100 - ( prob[0] + prob[1] + prob[2] ); break; } return prob; } public static void main (String[] args) { Double []t = TestSwitch.test(4); System.out.println (t[1]); } }
Java Code:--------------------Configuration: <Default>-------------------- D:\3rd semster\Java\ProjectTest\TestSwitch.java:10: cannot find symbol symbol : class Random location: class TestSwitch Random rand = new Random(); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:10: cannot find symbol symbol : class Random location: class TestSwitch Random rand = new Random(); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:15: operator + cannot be applied to Random.nextDouble,int prob[i-1] = 25.0 + (rand.nextDouble(75) + 1); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:15: operator + cannot be applied to double,<nulltype> prob[i-1] = 25.0 + (rand.nextDouble(75) + 1); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:20: operator + cannot be applied to double,Random.nextDouble prob[1]= y + rand.nextDouble(z); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:24: operator + cannot be applied to double,Random.nextDouble prob[2] = y2 + rand.nextDouble(y2); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:31: operator + cannot be applied to int,Random.nextDouble prob[0] = 25 + rand.nextDouble(75) + 1; ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:31: operator + cannot be applied to <nulltype>,int prob[0] = 25 + rand.nextDouble(75) + 1; ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:36: operator + cannot be applied to double,Random.nextDouble prob[i-1] = y + rand.nextDouble(z); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:40: operator + cannot be applied to double,Random.nextDouble prob[2] = y2 + rand.nextDouble(y2); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:47: operator + cannot be applied to int,Random.nextDouble prob[0] = 25 + rand.nextDouble(75) + 1; ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:47: operator + cannot be applied to <nulltype>,int prob[0] = 25 + rand.nextDouble(75) + 1; ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:52: operator + cannot be applied to double,Random.nextDouble prob[1]= y + rand.nextDouble(z); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:56: operator + cannot be applied to double,Random.nextDouble prob[i-1] = y2 + rand.nextDouble(y2); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:63: operator + cannot be applied to int,Random.nextDouble prob[0] = 25 + rand.nextDouble(75) + 1; ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:63: operator + cannot be applied to <nulltype>,int prob[0] = 25 + rand.nextDouble(75) + 1; ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:68: operator + cannot be applied to double,Random.nextDouble prob[1]= y + rand.nextDouble(z); ^ D:\3rd semster\Java\ProjectTest\TestSwitch.java:72: operator + cannot be applied to double,Random.nextDouble prob[2] = y2 + rand.nextDouble(y2); ^ 18 errors Process completed.
- 12-09-2011, 12:29 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: error when i use switch-case
The compiler cannot find the Random class, because you have not imported it. (Incidentally there is no need to import java.lang.*)Java Code:D:\3rd semster\Java\ProjectTest\TestSwitch.java:10: cannot find symbol symbol : class Random location: class TestSwitch Random rand = new Random();
Frankly the other errors are reported in a rather mysterious way, but the bottom line is that you cannot meaningfully say "rand.nextDouble(75)". Read the nextDouble() API docs to see how this method should be used.
Similar Threads
-
about conditonal if else and switch case
By niksipandit in forum New To JavaReplies: 6Last Post: 09-22-2011, 11:53 AM -
java switch case
By aconti in forum New To JavaReplies: 16Last Post: 08-09-2011, 07:05 AM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 12:34 AM -
Switch Case and Key Events
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 01-26-2009, 11:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks