|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#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 ); } } |
|
|
|
|
|
#2 |
|
Member (8 bit)
Join Date: Feb 2001
Posts: 156
|
hmmm there isn't any formatting... sorry. It was formatted when I posted it.
|
|
|
|
|
|
#3 |
|
Registered User
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--); |
|
|
|
|
|
#4 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
To format your text, use instead of the space char.
|
|
|
|
|
|
#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 |
|
|
|
|
|
#6 |
|
Member (12 bit)
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. |
|
|
|
|
|
#7 |
|
Member (8 bit)
Join Date: Feb 2001
Posts: 156
|
Just trying to see if it works
Java Sample1 5 hmm did it work? |
|
|
|
|
|
#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 );
}
}
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|