当前位置: 技术问答>java相关
java小程序调试,马上给分
来源: 互联网 发布时间:2017-05-01
本文导语: class SwitchDemo { public static void main(String args[]) { char c; System.out.println("请输入数字:"); c = (char)System.in.read(); switch( c ) { case 0: Sys...
class SwitchDemo
{
public static void main(String args[])
{
char c;
System.out.println("请输入数字:");
c = (char)System.in.read();
switch( c )
{
case 0:
System.out.println("测试输入0");
break;
case 1:
System.out.println("测试输入1");
break;
case 2:
System.out.println("测试输入2");
break;
case 3:
System.out.println("测试输入3");
break;
default:
System.out.println("输入的字符不在0-3之间");
}
}
}
提示错误:
"Swith.java": Error #: 360 : unreported exception: java.io.IOException; must be caught or declared to be thrown at line 7, column 28
{
public static void main(String args[])
{
char c;
System.out.println("请输入数字:");
c = (char)System.in.read();
switch( c )
{
case 0:
System.out.println("测试输入0");
break;
case 1:
System.out.println("测试输入1");
break;
case 2:
System.out.println("测试输入2");
break;
case 3:
System.out.println("测试输入3");
break;
default:
System.out.println("输入的字符不在0-3之间");
}
}
}
提示错误:
"Swith.java": Error #: 360 : unreported exception: java.io.IOException; must be caught or declared to be thrown at line 7, column 28
|
成功的程序:
import java.io.*;
class SwitchDemo
{
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader( new InputStreamReader (System.in));
System.out.println("请输入数字:");
int c=Integer.parseInt(br.readLine());
switch( c ){
case 0:
System.out.println("测试输入0");
break;
case 1:
System.out.println("测试输入1");
break;
case 2:
System.out.println("测试输入2");
break;
case 3:
System.out.println("测试输入3");
break;
default:
System.out.println("输入的字符不在0-3之间");
}
}
}
import java.io.*;
class SwitchDemo
{
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader( new InputStreamReader (System.in));
System.out.println("请输入数字:");
int c=Integer.parseInt(br.readLine());
switch( c ){
case 0:
System.out.println("测试输入0");
break;
case 1:
System.out.println("测试输入1");
break;
case 2:
System.out.println("测试输入2");
break;
case 3:
System.out.println("测试输入3");
break;
default:
System.out.println("输入的字符不在0-3之间");
}
}
}