当前位置:编程学习 > JAVA >>

本人新手,学习corejava 目前进度到了控制语句,但是遇到这个问题。。。


public class Menu {
public static void main(String args[]){
throws java.io.IOException{
char choice;

do{
System.out.println("Help on:");
System.out.println("1.if");
System.out.println("2.switch");
System.out.println("3.while");
System.out.println("4.do-while");
System.out.println("5.for\n");
System.out.println("choose one:");
choice =(char) System.in.read();
}while(choice<'1'||choice>'5');

System.out.println("\n");

switch(choice){}
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement");
System.out.println("else statement");
break;

case '2':
System.out.println("The switch");
System.out.println("switch(expression) {");
System.out.println("case constant:");
System.out.println("statement sequence");
System.out.println("break");
System.out.println("//...");
System.out.println("}");
break;

case '3':
System.out.println("The while:");
System.out.println("while (condition) statement");
break;

case '4':
System.out.println("The do-while:");
System.out.println("do (");
System.out.println("statement");
System.out.println("} while(condition)");
break;

case '5':
System.out.println("The for:");
System.out.println("for(int;condition;iteration)");
System.out.println("statement");
break;
}
}
}
不能编译,提示throws java.io.IOException 语法错误,但是例子就是这样写的,请各位高手帮忙看看。。 --------------------编程问答--------------------

public class Menu {
public static void main(String args[]) throws java.io.IOException {
char choice;

do {
System.out.println("Help on:");
System.out.println("1.if");
System.out.println("2.switch");
System.out.println("3.while");
System.out.println("4.do-while");
System.out.println("5.for\n");
System.out.println("choose one:");
choice = (char) System.in.read();
} while (choice < '1' || choice > '5');

System.out.println("\n");

switch (choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement");
System.out.println("else statement");
break;

case '2':
System.out.println("The switch");
System.out.println("switch(expression) {");
System.out.println("case constant:");
System.out.println("statement sequence");
System.out.println("break");
System.out.println("//...");
System.out.println("}");
break;

case '3':
System.out.println("The while:");
System.out.println("while (condition) statement");
break;

case '4':
System.out.println("The do-while:");
System.out.println("do (");
System.out.println("statement");
System.out.println("} while(condition)");
break;

case '5':
System.out.println("The for:");
System.out.println("for(int;condition;iteration)");
System.out.println("statement");
break;

}
}

}




我帮你改了不报错了,你自己比对下。。。 --------------------编程问答--------------------
引用 1 楼 plumebobo 的回复:

public class Menu {
public static void main(String args[]) throws java.io.IOException {
char choice;

do {
System.out.println("Help on:");
System.out.println("1.if");
System.out.println("2.switch");
System.out.println("3.while");
System.out.println("4.do-while");
System.out.println("5.for\n");
System.out.println("choose one:");
choice = (char) System.in.read();
} while (choice < '1' || choice > '5');

System.out.println("\n");

switch (choice) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement");
System.out.println("else statement");
break;

case '2':
System.out.println("The switch");
System.out.println("switch(expression) {");
System.out.println("case constant:");
System.out.println("statement sequence");
System.out.println("break");
System.out.println("//...");
System.out.println("}");
break;

case '3':
System.out.println("The while:");
System.out.println("while (condition) statement");
break;

case '4':
System.out.println("The do-while:");
System.out.println("do (");
System.out.println("statement");
System.out.println("} while(condition)");
break;

case '5':
System.out.println("The for:");
System.out.println("for(int;condition;iteration)");
System.out.println("statement");
break;

}
}

}




我帮你改了不报错了,你自己比对下。。。


非常感谢。。怪自己粗心。。 --------------------编程问答-------------------- 还真是挺隐蔽的...
补充:Java ,  Eclipse
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,