有无帮我编写个用java实现gui程序,输入1-1000整数程序判断出可以被3整除也可以被7整除 ,谢谢。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SumFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = -3059616600875760053L;
JLabel lable;
JTextField textLable;
static JButton butSummation;
public SumFrame() {
lable = new JLabel("输入整数n:");
textLable = new JTextField("", 10);
butSummation = new JButton("判断");
lable.setBounds(30, 30, 120, 30);
textLable.setBounds(180, 30, 120, 30);
butSummation.setBounds(160, 180, 120, 30);
add(lable);
add(textLable);
add(butSummation);
setLayout(null);
setBounds(200, 100, 400, 300);
// 窗口在屏幕中间显示
setLocationRelativeTo(null);
setTitle("判断");
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
SumFrame summationForm = new SumFrame();
butSummation.addActionListener(summationForm);
summationForm.add(butSummation);
}
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (jb == butSummation) {
String Mathematics = textLable.getText();
int a = Integer.parseInt(Mathematics);
if (a%3==0 && a%7==0)
JOptionPane.showMessageDialog(this, a+"能够被3和7整除!", "提示对话框", JOptionPane.DEFAULT_OPTION);
else
JOptionPane.showMessageDialog(this, a+"不能够被3和7整除!", "提示对话框", JOptionPane.DEFAULT_OPTION);
}
}
}
无参数滴 谢谢大哥们
package csdn008;
import javax.swing.*;
/**
* 实现gui程序,输入1-1000整数程序判断出可以被3整除也可以被7整除 ,谢谢。
* @author wangfei
* @version 1.0
* @date 2022/6/4
*/
public class ZhengChuTest extends JFrame {
public static void main(String[] args) {
new ZhengChuTest();
}
public ZhengChuTest() {
super("被3整除也可以被7整除");
JPanel jPanel = new JPanel();
JTextField jTextField = new JTextField();
jTextField.setColumns(20);
JButton jButton = new JButton("计算");
jButton.addActionListener(e -> {
String value = jTextField.getText();
if (!"".equals(value)) {
if (value.matches("[0-9]+")) {
int integerValue = Integer.parseInt(value);
if (integerValue >= 1 && integerValue <= 1000) {
if (integerValue % 3 ==0 && integerValue % 7 ==0) {
JOptionPane.showMessageDialog(null,value + "可以被3整除且被7整除");
}else {
JOptionPane.showMessageDialog(null,value + "不能被3整除且被7整除");
}
}else {
JOptionPane.showMessageDialog(null,value + ",不在1-1000之间");
}
}else {
JOptionPane.showMessageDialog(null,value + ",不是纯数字!");
}
}else {
JOptionPane.showMessageDialog(null,"输入信息不能为空");
}
jTextField.setText("");
});
jPanel.add(jTextField);
jPanel.add(jButton);
add(jPanel);
setSize(500,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
完整代码如下:
public static void main(String[] args) {
JFrame jFrame = new JFrame("同时被3和7整除");
jFrame.setLocationRelativeTo(null);
jFrame.setSize(80, 100);
jFrame.setLayout(new FlowLayout());
JTextField jTextField = new JTextField(50);
jTextField.setEditable(true);
jTextField.setEnabled(true);
JButton jButton = new JButton("计算");
jButton.addActionListener(e -> {
String[] str = jTextField.getText().split(",");
List<Integer> nums = Arrays.stream(str).map(s->Integer.parseInt(s)).filter(n->n%3==0&&n%7==0).collect(Collectors.toList());
JOptionPane.showMessageDialog(null, "既能被3整除又能被7整除的数据有:"+nums.toString());
});
jFrame.add(jTextField);
jFrame.add(jButton);
jFrame.setVisible(true);
jFrame.pack();
}
运行结果如下:
如有帮助,请采纳,十分感谢!
套个壳
public class Asak {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num=0;
System.out.println("1~1000之间能被3整除又可以被7整除的数: ");
for (int i=1;i<1000;i++){
if (i%3==0&&i%7==0){
num++;
System.out.print(i+"\t");
if (num%10==0&&num>=10)
System.out.println();
}
}
}
}