java 1 2 3..100_用java编写 1+2+3+ +100 的程序

这是一个Java程序,创建了一个窗口应用,用户可以通过按钮操作进行1到100之间的加减运算,展示1+2+3+...+100的计算过程。程序包含了加1、减1、加10、减10、加100、减100及设置数为0的功能。
摘要由CSDN通过智能技术生成

展开全部

//有窗口的62616964757a686964616fe59b9ee7ad9431333431346339 1+2+3+...+到某一个数

import java.awt.Color;

import java.awt.Font;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.WindowConstants;

public class AbsolutePosition extends JFrame

{

Font F =new Font("宋体",1,20);

int A=1;

int B=0;

int C=0;

int Undo=0;

public void ChangeNum(int X)

{

A=A+X;

C=0;

B=0;

while(C

{

C=C+1;

B=B+C;

}

if(A<0)

{

B=-B;

}

}

public AbsolutePosition()

{

setTitle("算...+一个整数");

setLayout(null);

setResizable(false);

Container container = getContentPane();

JButton B1=new JButton("+1");

JLabel L1=new JLabel("请计算");

JButton B2=new JButton("-1");

JButton B3=new JButton("+10");

JButton B4=new JButton("-10");

JButton B5=new JButton("+100");

JButton B6=new JButton("-100");

JButton B7=new JButton("设数为0");

JButton B8=new JButton("撤销");

L1.setBounds(120, 100, 300, 70);

B1.setBounds(120, 200, 200, 40);

B2.setBounds(320, 200, 200, 40);

B3.setBounds(120, 240, 200, 40);

B4.setBounds(320, 240, 200, 40);

B5.setBounds(120, 280, 200, 40);

B6.setBounds(320, 280, 200, 40);

B7.setBounds(120, 160, 200, 40);

B8.setBounds(320, 160, 200, 40);

B1.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

Undo=A;

ChangeNum(1);

L1.setText(""+B+"(...+"+A+")");

}

});

B2.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

Undo=A;

ChangeNum(-1);

L1.setText(""+B+"(...+"+A+")");

}

});

B3.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

Undo=A;

ChangeNum(+10);

L1.setText(""+B+"(...+"+A+")");

}

});

B4.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

Undo=A;

ChangeNum(-10);

L1.setText(""+B+"(...+"+A+")");

}

});

B5.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

Undo=A;

ChangeNum(+100);

L1.setText(""+B+"(...+"+A+")");

}

});

B6.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

Undo=A;

ChangeNum(-100);

L1.setText(""+B+"(...+"+A+")");

}

});

B7.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

Undo=A;

A=0;

ChangeNum(0);

L1.setText(""+B+"(...+"+A+")");

}

});

B8.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent E)

{

A=Undo;

ChangeNum(0);

L1.setText(""+B+"(...+"+A+")");

}

});

L1.setFont(F);

L1.setForeground(Color.BLUE);

L1.setBackground(Color.WHITE);

container.add(B1);

container.add(L1);

container.add(B2);

container.add(B3);

container.add(B4);

container.add(B5);

container.add(B6);

container.add(B7);

container.add(B8);

setVisible(true);

setSize(660,480);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String [] args)

{

new AbsolutePosition();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值