There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
audio avg avg 8 blue screen brand new codec control panel conversion crash delete personal data desktop display dos driver duplicate dvd error error message excel explorer file firefox game graphics hardware hijackthis log install installation internet itunes javascript laptop macro malware monitor msconfig msn music network outlook outlook 2003 outlook express php problem program random rundll32 security seo sound sp3 spyware switch tag cloud trojan usb video virtumonde virus vista visual basic vundo wallpaper windows windows vista windows xp wireless word xp sp3 youtube
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Java Help!


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
bootsiebella's Avatar
Junior Member with 12 posts.
 
Join Date: Mar 2008
09-May-2008, 03:39 PM #1
Java Help!
I am trying to find the value of x, y, and z after execution but I can't get it to compile

public class Numbers
{
int x=3, y=4, z=5;
switch(y) {
case 2:
x *= y;
case 4:
y = (x + z)/y;
case 6:
z += y++;
break;
case 8:
z++;
default:
x -= y;
}

Also there is something wrong with this code can anyone help me out please!

for(int i=0;i<22;i++) {
if(i<10) {
int j= 2 + i;
}
System.out.println("i: " + i + " j: " + j);
}
Chicon's Avatar
Computer Specs
Distinguished Member with 6,213 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.58" E
Experience: Intermediate
09-May-2008, 05:42 PM #2
At the root of a Java class, you may only have declarations of variables, methods and constructors.
You can't simply have statements outside a method.

In the second part, the integer j has no existence outside the if statement.
Indeed, a variable exists only inside the area, delimited by the symbols { }, where it has been declared.
Therefore, the System.out.println statement won't never work as j is only known between the { } area of the if statement.
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !
bootsiebella's Avatar
Junior Member with 12 posts.
 
Join Date: Mar 2008
09-May-2008, 07:06 PM #3
Smile Java Help
public class TestA {
public static void main( String args[] )
{


int x=3, y=4, z=5;
switch(y) {
case 2:
x *= y;
case 4:
y = (x + z)/y;
case 6:
z += y++;
break;
case 8:
z++;
default:
x -= y;
}
}
}
This compiles but how do I get the results for x, y, and z. Can you show me please!
Thanks
Chicon's Avatar
Computer Specs
Distinguished Member with 6,213 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.58" E
Experience: Intermediate
09-May-2008, 07:28 PM #4
Here's a way to display the results :

Code:

public class TestA {

   public static void main( String args[] )
   {
      int x=3, y=4, z=5;
      switch(y) {
         case 2:
            x *= y;
         case 4:
            y = (x + z)/y;
         case 6:
            z += y++;
            break;
         case 8:
            z++;
         default:
            x -= y;
      }
      System.out.println("x = " + x + "  y = " + y + "  z = " + z);
    }
}
In this case, the System.out.println statement will work as the variables x, y, z are declared in the same area delimited by the { } of the main method.
In my prior post, I should have specified that variables may exist in inner areas. Therefore, x, y and z may work in the area delimited in the switch statement.
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:06 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.