?? 例4-11.java
字號:
//例4.11
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class tt 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.parseDouble(tf1.getText());//可能異常
d2=Double.parseDouble(tf2.getText());//可能異常
L2.setText("兩數相除的結果:"+Result());//可能異常
}
catch(NumberFormatException e)
{
answerStr="輸入的必須是數字";
L2.setText(answerStr);
}
catch(NumberRangeException e)
{
answerStr=e.getMessage(); L2.setText(answerStr);
}
repaint();
}
public double Result() throws NumberRangeException
{
double answer=0;
try
{
if((d1<0)||(d1>100)||(d2<0)||(d2>100))
{
String ss="輸入的數不在指定的范圍,請重新輸入。";
NumberRangeException ee=new NumberRangeException(ss);
throw ee;
}
answer=d1/d2;
}
catch(ArithmeticException e)
{ answerStr=e.toString(); }
return answer;
}
}
class NumberRangeException extends Exception
{
NumberRangeException(String msg) {super(msg); }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -