📄 division2.java
字号:
//源文件名:Division2.java
//在下载源程序中的文件夹:0803完善除法计算器
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Division2 extends Applet implements TextListener
{
Label prompt1,prompt2,prompt3;
Label ExceptionMess,ExceptionMess1,ExceptionMess2;
TextField input1,input2,output1,output2;
int a,b,c,d;
public void init()
{
input1=new TextField(5);
prompt1=new Label("÷");
prompt1.setAlignment(Label.CENTER);
input2=new TextField(5);
prompt2=new Label("=");
prompt2.setAlignment(Label.CENTER);
output1=new TextField(5);
prompt3=new Label("余");
prompt3.setAlignment(Label.CENTER);
output2=new TextField(5);
ExceptionMess=new Label(" ");
ExceptionMess.setAlignment(Label.CENTER);
ExceptionMess.setForeground(Color.red);
ExceptionMess1=new Label(" ");
ExceptionMess1.setAlignment(Label.CENTER);
ExceptionMess1.setForeground(Color.blue);
ExceptionMess2=new Label(" ");
ExceptionMess2.setAlignment(Label.CENTER);
ExceptionMess2.setForeground(Color.blue);
add (input1);
add (prompt1);
add (input2);
add (prompt2);
add (output1);
add (prompt3);
add (output2);
add (ExceptionMess);
add (ExceptionMess1);
add (ExceptionMess2);
input1.addTextListener(this);
input2.addTextListener(this);
output1.setEditable(false);
output2.setEditable(false);
}
public void textValueChanged(TextEvent e)
{
ExceptionMess.setText("");
ExceptionMess1.setText("");
ExceptionMess2.setText("");
try
{
if (input1.getText().equals(""))
{
throw new Exception("被除数不能为空");
}
if (input1.getText().indexOf(".")>0)
{
throw new Exception("被除数不是整数");
}
if (input2.getText().indexOf(".")>0)
{
throw new Exception("除数不是整数");
}
a=Integer.parseInt(input1.getText());
b=Integer.parseInt(input2.getText());
c=a/b;
d=a%b;
output1.setText(Integer.toString(c));
output2.setText(Integer.toString(d));
}
catch (ArithmeticException e1)
{
ExceptionMess.setText("除数不能为0,请重新输入");
ExceptionMess1.setText(e1.getMessage());
ExceptionMess2.setText(e1.toString());
output1.setText("");
output2.setText("");
}
catch(NumberFormatException e3)
{
ExceptionMess1.setText(e3.getMessage().equals("")?"除数不能为空":e3.getMessage()+"不是数字");
output1.setText("");
output2.setText("");
}
catch (Exception e2)
{
ExceptionMess1.setText(e2.getMessage());
output1.setText("");
output2.setText("");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -