-
Switch Statement
Hello,
Is it possible to make a variable in, say, Case 1, recognised from, say, Case 2 in a Switch statement?
Example:
Code:
import java.io.*;
class Switch
{
public static void main (String [] args) throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String x = in.readLine ();
int y = Integer.parseInt(x);
switch (y)
{
case 1 : {int a = 5;int b = 6;}
case 2 : {int c = a*b;}
}
}
}
In this case, the compiler complains that it cannot find variables a and b. Is there a method to solve this matter so that something declared in a case can be accessed from another one? Thanks.
-
Just put your declarations outside of the switch statement and initialize them inside. For example:
Code:
int a = 0, b = 0, c = 0;
switch (y)
{
case 1 : a = 5;b = 6;
case 2 : c = a*b;
}
You just have to make sure that you have good default values.
Hope this helps.
~evlich
-
Last edited by mr1yh1; 11-13-2005 at 05:32 PM.
Reason: same as above evlich wrote
-
 Originally Posted by evlich
Just put your declarations outside of the switch statement and initialize them inside. For example:
Code:
int a = 0, b = 0, c = 0;
switch (y)
{
case 1 : a = 5;b = 6;
case 2 : c = a*b;
}
You just have to make sure that you have good default values.
Hope this helps.
But if you run this code, case 2 doesn't return 30 (i.e. 5x6), it multiplies the values of a and b outside the switch statement (i.e. 0x0).
What I would like is that case 2 multiplies the values of a and b in case 1. I don't know if this is possible.
-
That is basically because of the nature of a switch statement, it is only going to execute the code that corresponds to the value that you are switching on. If you always want c to be 30 if y is 2, you just say something like c = 30. It sounds like a switch statement is not what you should be using for this, exactly what functionality are you trying to achieve?
~evlich
-
 Originally Posted by evlich
That is basically because of the nature of a switch statement....
I thought so but wasn't sure....... thanks anyway..... I'll use some other statement.
-
What exactly is it that you are trying to do?
~evlich
-
 Originally Posted by Dreamer
But if you run this code, case 2 doesn't return 30 (i.e. 5x6), it multiplies the values of a and b outside the switch statement (i.e. 0x0).
What I would like is that case 2 multiplies the values of a and b in case 1. I don't know if this is possible.
it happens there what you want to do , if y = 1..
becos you dont use "break" statements.
be careful about it, if you dont use "break"s , it will be a error-prone code.
-
 Originally Posted by evlich
What exactly is it that you are trying to do?
I'm having this assignment where you have to create sort of a forum where users can choose to register or to login. So I created Case 1 for Registration and Case 2 for Login. I put no 'break' in Case 1 so that the user, after registration, goes to Case 2 to Login. Since the username was input from the user in Case 1, the function to match the username in Case 2 couldn't find the username variable since it's in Case 1.
Anyway, I managed to do it by creating blocks. Thanks.
Similar Threads
-
Replies: 3
Last Post: 11-01-2005, 09:48 AM
-
By zicq in forum Database
Replies: 2
Last Post: 05-18-2003, 10:16 PM
-
By Daryl Shockey in forum Database
Replies: 2
Last Post: 05-28-2002, 03:50 PM
-
By Cali LaFollett in forum .NET
Replies: 24
Last Post: 09-16-2001, 03:43 PM
-
By James Lin in forum .NET
Replies: 9
Last Post: 08-15-2000, 10:54 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks