发布网友 发布时间:2022-04-22 06:52
共5个回答
热心网友 时间:2022-06-16 23:51
//有窗口的 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<Math.abs(A))
{
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();
}
}
热心网友 时间:2022-06-16 23:51
public class Test1 {
public static void main(String[] args) {
int sum=0;//存放累加的数
for(int i=1;i<=100;i++){//从1循环到100
sum+=i;//累加到sum变量中
}
System.out.println(sum);//输出sum
}
}
具体代码如上:输出为5050
热心网友 时间:2022-06-16 23:52
提供递归和循环两种思路。
public class Test {
public static void main(String[] args) {
int count = 100;
int result = loopCaculate1(count);
System.out.println(count + "的加和为:" + result);
}
//递归
public static int loopCaculate(int count){
if(count == 1){
return 1;
}
return count + loopCaculate(-- count);
}
//魂环
public static int loopCaculate1(int count){
int result = 0;
for(int index = 1;index <= count; index ++){
result += index;
}
return result;
}
}
热心网友 时间:2022-06-16 23:53
代码如下:
public class Test{
public static void main(String[]args){
int sum=0;//用于累加结果
for(int i=1;i<=100;i++)
sum=sum+i;//累加
System.out.println("1!+2!+...+19!+20!="+sum);
}
}
热心网友 时间:2022-06-16 23:53
public class Add{
public static void main(String[]args){
int count=0;//用于存放累加结果
for(int i=1;i<100=;i++)//采用for循环来累加。
{
count=count+i;//累加
}
System.out.println("1+2+...+98+99+100="+sum);
}
}