当前位置: 技术问答>java相关
为什么总会有异常类抛出?请各位大哥再帮小第一次!
来源: 互联网 发布时间:2015-03-22
本文导语: 我想在终端下输入两个短整形数,可是还没等我输入,就有异常类抛出,请问这是怎么回事?小弟我该怎么办?(程序可能有点长,可其实很简单的)。 import java.io.*; public class t { private BufferedReader breader= new Bu...
我想在终端下输入两个短整形数,可是还没等我输入,就有异常类抛出,请问这是怎么回事?小弟我该怎么办?(程序可能有点长,可其实很简单的)。
import java.io.*;
public class t {
private BufferedReader breader= new BufferedReader( new InputStreamReader( System.in ) );
private void showMainMenu() {
try {
System.out.println( "1 - t2 - t3 -" );
switch ( System.in.read() ) {
case '1': a(); break;
case '2': b(); break;
case '3': c(); break;
default: ;
}
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void a() {
System.out.println( "function a:" );
short s, j;
try {
System.out.print( "s? " );
s = Short.parseShort( breader.readLine() );
System.out.print( "j? " );
j = Short.parseShort( breader.readLine() );
System.out.println( "s = " + s + "j = " + j );
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void b() {
System.out.println( "function b:" );
short s, j;
try {
System.out.print( "s? " );
s = Short.parseShort( breader.readLine() );
System.out.print( "j? " );
j = Short.parseShort( breader.readLine() );
System.out.println( "s = " + s + "j = " + j );
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void c() {
System.out.println( "function c:" );
short s, j;
try {
System.out.print( "s? " );
s = Short.parseShort( breader.readLine() );
System.out.print( "j? " );
j = Short.parseShort( breader.readLine() );
System.out.println( "s = " + s + "j = " + j );
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
public static void main( String[] args ) {
t test = new t();
test.showMainMenu();
}
}
import java.io.*;
public class t {
private BufferedReader breader= new BufferedReader( new InputStreamReader( System.in ) );
private void showMainMenu() {
try {
System.out.println( "1 - t2 - t3 -" );
switch ( System.in.read() ) {
case '1': a(); break;
case '2': b(); break;
case '3': c(); break;
default: ;
}
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void a() {
System.out.println( "function a:" );
short s, j;
try {
System.out.print( "s? " );
s = Short.parseShort( breader.readLine() );
System.out.print( "j? " );
j = Short.parseShort( breader.readLine() );
System.out.println( "s = " + s + "j = " + j );
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void b() {
System.out.println( "function b:" );
short s, j;
try {
System.out.print( "s? " );
s = Short.parseShort( breader.readLine() );
System.out.print( "j? " );
j = Short.parseShort( breader.readLine() );
System.out.println( "s = " + s + "j = " + j );
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void c() {
System.out.println( "function c:" );
short s, j;
try {
System.out.print( "s? " );
s = Short.parseShort( breader.readLine() );
System.out.print( "j? " );
j = Short.parseShort( breader.readLine() );
System.out.println( "s = " + s + "j = " + j );
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
public static void main( String[] args ) {
t test = new t();
test.showMainMenu();
}
}
|
我帮你改了改,问题应该解决了,问题原因是输入缓冲问题
import java.io.*;
public class t {
private BufferedReader breader= new BufferedReader( new InputStreamReader( System.in ) );
private void showMainMenu() {
try {
System.out.println( "1 - t2 - t3 -" );
switch ( System.in.read() ) {
case '1': a(); break;
case '2': b(); break;
case '3': c(); break;
default: ;
}
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void a() {
System.out.println( "function a:" );
short s, j;
try {
System.out.print( "s? " );
while (breader.ready()) //
import java.io.*;
public class t {
private BufferedReader breader= new BufferedReader( new InputStreamReader( System.in ) );
private void showMainMenu() {
try {
System.out.println( "1 - t2 - t3 -" );
switch ( System.in.read() ) {
case '1': a(); break;
case '2': b(); break;
case '3': c(); break;
default: ;
}
} catch ( Exception e ) {
System.out.println( "an exception has occured." );
}
}
private void a() {
System.out.println( "function a:" );
short s, j;
try {
System.out.print( "s? " );
while (breader.ready()) //
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!