⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptionapplet.java

📁 针对越来越多的用户喜欢JAVA
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ExceptionApplet extends Applet implements ActionListener
{
Label L1, L2;
TextField tf1,tf2;
String answerStr;
double d1,d2;

public void init()
{
L1=new Label ("请输入0到100之间的整数");
add (L1);
tf1=new TextField (6);
add (tf1);
tf2=new TextField (6);
add (tf2);
L2=new Label ("两数相除的结果:                          ");
add (L2);
tf1.addActionListener(this);
tf2.addActionListener(this);
}
public void actionPerformed (ActionEvent evt)
{
try
{
d1=Double.valueOf (tf1.getText()).doubleValue();
d2=Double.valueOf (tf2.getText()).doubleValue();
L2.setText ("两数相除的结果:  "+Result());
}
catch (NumberFormatException e )
{
answerStr="输入的必须是数字";
L2.setText (answerStr);
}
catch (NumberRangeException ee)
{
answerStr=ee.getMessage ();
L2.setText (answerStr);
}
repaint ();
}
public double Result () throws NumberRangeException
{
double answer=0;
try
{
if ((d1<0)|| (d2<0) || (d1>100)|| (d2>100))
{
NumberRangeException ee=new NumberRangeException ("输入的数据超出范围!请重新输入。");
throw ee;
}
answer=d1/d2;
}
catch (ArithmeticException eee )
{
answerStr=eee.toString ();
}
return answer;
}
}
class NumberRangeException extends Exception
{
NumberRangeException (String msg)
{  super (msg);   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -