请各位大侠看看这段源码,帮忙修正,不胜感谢!---->终于搞定! (50分)

  • 主题发起人 主题发起人 lql0459
  • 开始时间 开始时间
L

lql0459

Unregistered / Unconfirmed
GUEST, unregistred user!
源码如下:谢谢解答!
import java.io.*;
public class exam2{
String color(int i){
int j;
for (j=0;
j<=i;
j++){
switch (j%2){
case 0: return "red";
break;
case 1: return "green";
break;
case 2: return "white";
break;
default: return "none color";
break;
}
}
}
public static void main(String args[]) {
exam2 abc=new exam2();
System.out.println(abc.color(9));
}
}
错误提示:
--------------------Configuration: java.exe <Default>--------------------
D:/Program Files/JCreator/MyProjects/exam2.java:8: unreachable statement
case 0: return "red";
break;
^
D:/Program Files/JCreator/MyProjects/exam2.java:9: unreachable statement
case 1: return "green";
break;
^
D:/Program Files/JCreator/MyProjects/exam2.java:10: unreachable statement
case 2: return "white";
break;
^
D:/Program Files/JCreator/MyProjects/exam2.java:11: unreachable statement
default: return "none color";
break;
^
D:/Program Files/JCreator/MyProjects/exam2.java:4: missing return statement
String color(int i){
^
5 errors
Process completed.
 
case 0: return "red";
break;
~~~~~~~~~~~已经从函数返回了,所以break语句
不可能被执行到,下面同理.删除Break.
 
卷兄:
去掉了所有的break可就不能退出循环了!
不过总算有人回答了!
看来还是DFW forum的FW最热心了!
 
兄弟啊,你Return,就整个函数返回,退出函数了!
 
那请卷兄写出修正后的源码!
偶太笨了!
 
直接把紧跟着return的break去掉嘛
 
已经试过了!
请再来的FW贴出修正后的源码!
 
你去掉以后有什么错误提示?还是不对么?
 
终于搞定了!谢谢卷兄及各位FW的提示!再次表示感谢!
import java.io.*;
public class exam2{
String color(int i){
int j;
for (j=0;
j<=i;
j++){
switch (i%2){
case 0: System.out.println("red");
break;
case 1: System.out.println("green");
break;
case 2: System.out.println("white");
break;
}
}
return "The End";
}
public static void main(String args[]) {
exam2 abc=new exam2();
abc.color(9);
}
}
or
import java.io.*;
public class exam2{
String color(int i){
int j;
for (j=0;
j<=i;
j++){
switch (i%2){
case 0: System.out.println("red");

case 1: System.out.println("green");
case 2: System.out.println("white"); }
}
return "The End";
}
public static void main(String args[]) {
exam2 abc=new exam2();
abc.color(9);
}
}
 
多人接受答案了。
 
后退
顶部