Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-03-2002, 08:37 PM   #1
Member (8 bit)
 
Join Date: Feb 2001
Posts: 156
Java Help

Ok... I'm not trying to get you to do my homework. I just need some help b/c this is a sample question for my test tomorrow. I know the answer is b = 6 but how did they get it? I don't understand the b += a--; part. I know that a-- is a postfix and means a = a - 1 but what about the b += part of that? Thanks for the help.... Josh

Java Sample1 5

public class Sample1{

public static void main(String [] args){
int a, b = 0;
a = Integer.parseInt( args[0] );

do {
switch ( a ) {
case 3:
b += a --;
break;
case 2:
b += --a;
break;
case 1:
b += 2 * a --;
break;
default:
--a;
}
}while ( a >= 0 );
System.out.print ( "b is " );
System.out.println ( b );
}
}
minutrice is offline   Reply With Quote
Old 03-03-2002, 08:38 PM   #2
Member (8 bit)
 
Join Date: Feb 2001
Posts: 156
hmmm there isn't any formatting... sorry. It was formatted when I posted it.
minutrice is offline   Reply With Quote
Old 03-04-2002, 05:46 AM   #3
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
x += y; (x is of type type1) means:
x = (type1)(x + y);

so since a is of the type int:

b += a--; means b = (int)(b + a--);
aym is offline   Reply With Quote
Old 03-04-2002, 05:50 AM   #4
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
To format your text, use   instead of the space char.
aym is offline   Reply With Quote
Old 03-04-2002, 10:09 AM   #5
Member (8 bit)
 
Join Date: Feb 2001
Posts: 156
thanks for the reply. I messed around with it until I finally figured it out.

Thanks again,

Josh
minutrice is offline   Reply With Quote
Old 03-05-2002, 11:30 PM   #6
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
I think you can also use UBB tags:

[code]

put your code here

[/code]

It would look like this:

Code:
int main(){
   doNothing();
   return 0;
}
__________________
Paul M. Victorey
------------------
I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk.
Paul Victorey is offline   Reply With Quote
Old 03-06-2002, 12:38 AM   #7
Member (8 bit)
 
Join Date: Feb 2001
Posts: 156
Just trying to see if it works



Java Sample1 5



public class Sample1{

public static void main(String [] args){
int a, b = 0;
a = Integer.parseInt( args[0] );

do {
switch ( a ) {
case 3:
b += a --;
break;
case 2:
b += --a;
break;
case 1:
b += 2 * a --;
break;
default:
--a;
}
}while ( a >= 0 );
System.out.print ( "b is " );
System.out.println ( b );
}
}



hmm did it work?
minutrice is offline   Reply With Quote
Old 03-06-2002, 12:42 AM   #8
Member (8 bit)
 
Join Date: Feb 2001
Posts: 156
Java Sample1 5

Code:
public class Sample1{ 

 public static void main(String [] args){ 
   int a, b = 0; 
   a = Integer.parseInt( args[0] ); 

      do { 
         switch ( a ) { 
         case 3: 
               b += a --; 
               break; 
         case 2: 
               b += --a; 
               break; 
         case 1: 
               b += 2 * a --; 
               break; 
         default: 
               --a; 
         } 
      }while ( a >= 0 ); 
  System.out.print ( "b is " ); 
  System.out.println ( b ); 
 } 
}
trying again
minutrice is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:52 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2