『壹』 (1) 编写一个java程序实现两数求和功能:在两个文本框里分别输入两个数字,单击“求和”按钮,则在第三
效果如图
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
{
JTextFieldjtf1,jtf2,jtf3;
publicAddFrame(){
//控件的创建
jtf1=newJTextField(10);
JLabeljl=newJLabel("+");
jtf2=newJTextField(10);
JButtonjbAdd=newJButton("求和");
jbAdd.addActionListener(this);//给按钮添加事件响应
jtf3=newJTextField(10);
jtf3.setEditable(false);//结果文本框不能被编辑
add(jtf1);
add(jl);
add(jtf2);
add(jbAdd);
add(jtf3);
setLayout(newFlowLayout());//窗口流式布局
setSize(458,95);
setTitle("求和");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
//当按钮被点击时执行actionPerformed方法
publicvoidactionPerformed(ActionEvente){
try{
doubled1=Double.parseDouble(jtf1.getText());
doubled2=Double.parseDouble(jtf2.getText());
jtf3.setText(""+(d1+d2));
}catch(Exceptionex){
JOptionPane.showMessageDialog(this,"提示:只能输入两个数字,不能为空或者其他字符");
}
}
publicstaticvoidmain(String[]args){
newAddFrame();//启动窗口
}
}
『贰』 程序运行后在两个文本框中分别输入正整数a和b(a<=b)单击确定按钮,在文本框中显示ab间所有素数
Public Class Form1
Function isprime(ByVal x As Integer) As Integer
Dim i As Integer
For i = 2 To x - 1
If x Mod i = 0 Then
Exit For
End If
Next i
If i >= x Then
isprime = 1
Else
isprime = 0
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
Dim x As Integer
For x = a To b
If isprime(x) = 1 Then
Label1.Text = Label1.Text + x.ToString() + ","
End If
Next x
End Sub
End Class