Is the code for a case statement like this?
code:
switch (x)
{
case 1 :
y = 10;
do;case 2 :
y = 5;
do;
}
I honestly can't remember the syntax, and I left my notes at work :cry:
If you mean switches in general, you don't need the "do;". You would need a "break;" if you don't want case 1 to run through case 1, case 2, etc.
code:switch(x)
{
case 1: y = 10;
break;
case 2: y = 5;
break;
default: System.out.println("Pikachu!");
break; //not really necessary but advocated by teachers anyway
}
[ 11-23-2003: Message edited by: Oscar 3/Xray 1 ]
quote:
Verily, Khyron doth proclaim:
I dunno, Waisz, I honestly dunno Like I said, suddenly my brain seized up and I couldn't remember the syntax =/
I meant, were you using a normal switch or a switch with some kind of loop?
quote:
We were all impressed when Oscar 3/Xray 1 wrote:
I meant, were you using a normal switch or a switch with some kind of loop?
This is the exact code I was doing...
code:switch (month)
{
case 1 :
mth = "Jan";
break;
case 2 :
mth = "Feb";
break;
case 3 :
mth = "Mar"
break;
case 4 :
mth = "Apr";
break;
case 5 :
mth = "May";
break;
case 6 :
mth = "Jun";
break;
case 7 :
mth = "Jul";
break;
case 8 :
mth = "Aug";
break;
case 9 :
mth = "Sep";
break;
case 10 :
mth = "Oct";
break;
case 11 :
mth = "Nov";
break;
case 12 :
mth = "Dec";
break;
}
I just had a brainfart and I started mixing Pascal syntax and java syntax I had case (month) at first and wondered why it wouldn't compile... then I realised I couldn't remember what next. [ 11-23-2003: Message edited by: Khyron ]
String monthArray[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; [ 11-24-2003: Message edited by: Nina ]
mth = monthArray[month-1];
quote:
Nina had this to say about (_|_):
... use an array?String monthArray[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
mth = monthArray[month-1];
That works, too, unless this is going in a class with a lot of instances where having the string array would cause too much memory usage (on computers that are over a decade old or something ).
Oh, nevermind that, monthArray[] would be a static final. [ 11-24-2003: Message edited by: Oscar 3/Xray 1 ]
Well, I CAN do an array, but since the teacher hasn't instructed us on them yet, then I would probably lose points on the assignment if I did so.
[ 11-24-2003: Message edited by: Rabidbunnylover'nix ]
so
code:
case MONTH.Oct :
mth = "Oct";
break;
quote:
Rabidbunnylover'nix had this to say about Cuba:
mth = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM).format((new java.util.GregorianCalendar(2003, month - 1, 1)).getTime( )).substring(0, 3);
lol
you code like i did when I was in college. an entire assignment in as little code as possible using only one line. i enjoyed doing that.. handing in stuff seconds after it was assigned. good man for that but... i believe the point of the assignment was to learn how to use select case correctly. and the professors hate people doing what we do. I remember during a final getting a problem marked wrong (i wrote the code on the paper with some short recursive statement) and didn't get it fixed until I insisted the prof actually enter the code into a computer and try it out.. it worked, he was a little surprised, but I got the problem marked right. how I programmed in college and how I program the "proper" way in the real world are... worlds apart. [ 11-24-2003: Message edited by: diadem ]
"... ... ... My brain hurts from the stupidness of our teacher."
Yes, that is my input. Also I agree with what everyone else here says. Of course you could just have it go through a gheto if loop or a gheto for loop till it finds the number its looking for then output said month. Th efor loop would suck btw. alot.
quote:
diadem stopped staring at Deedlit long enough to write:
lolyou code like i did when I was in college. an entire assignment in as little code as possible using only one line. i enjoyed doing that.. handing in stuff seconds after it was assigned. good man for that but... i believe the point of the assignment was to learn how to use select case correctly. and the professors hate people doing what we do. I remember during a final getting a problem marked wrong (i wrote the code on the paper with some short recursive statement) and didn't get it fixed until I insisted the prof actually enter the code into a computer and try it out.. it worked, he was a little surprised, but I got the problem marked right. how I programmed in college and how I program the "proper" way in the real world are... worlds apart.
Yeah...my comp. sci. teacher at the moment used to work at IBM, and he's big on programming style. So I've learned how to whitespace nicely and follow style guides, but it's still fun to bastardize the language from time to time.