creating a java segment code [closed]
segment of code:
int x, y = 34, z = 19, w=11;
swtich (x) {
case 1: y += 4;
z -= w;
break;
case 2: w = (x++)*(--y);
break;
case 3: w = (--x)*(++y);
break;
case 4: y -=7;
case 5: z *= 2;
break;
default: w = (++x)*(++y);
}
System.out.println(“x = “ + x + “ y = “ + y + “ z = “ + z + “ w = “ + w);
can someone help me fix up this code please?
For one thing, you forgot to put break;
on CASE 4
. Then if x is local variable, you forgot to initialize it. And how do you plan to input the x?
EDIT: It's working on my PC.
public class Main {
public static void main (String[] args) {
int x=0, y = 34, z = 19, w = 11;
x = Integer.parseInt(JOptionPane.showInputDialog(x)); //this is how i input x
switch (x) {
case 1:
y += 4;
z -= w;
break;
case 2:
w = (x++) * (--y);
break;
case 3:
w = (--x) * (++y);
break;
case 4:
y -= 7;
break;
case 5:
z *= 2;
break;
default:
w = (++x) * (++y);
}
System.out.println("x = " + x + "y = " + y + " z = " + z + " w = " + w);
}
}
illegal character: \8220 or \8221: You used Unicode 8220 (aka \u291c, 0x291c, “, left quote) or ... something of the form usually when you Copy Paste a code... Type again the System.out.println line especially the quote ("), maybe you copy paste it and it is in different form... For more details about errors check out this site
case 4
needs a break
(unless you intended case 4 to "fall through" to case 5
.
Your code has smart quote characters (“
), which aren't normal quotes and are not recognized by the Java language.
Change the “
characters to "
.
精彩评论