重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class Demo {
成都创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、雅安机房托管、等保安全、私有云建设等企业级互联网基础服务,来电联系:028-86922220
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int inputNum;
for (; ; ) {
System.out.println("请输入一个4位整数: ");
inputNum = scanner.nextInt();
if (inputNum 0) {
break;
}
}
int sum = 0;
String inputNumStr = String.valueOf(inputNum);
for (int i = 0; i inputNumStr.length(); i++) {
sum += Integer.parseInt(inputNumStr.substring(i, i + 1));
}
System.out.println("该整数的各位数字之和: " + sum);
}
}
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class test { public static void main(String[] args) { JFrame frame = new JFrame("Find Max Value"); frame.setSize(300, 300); frame.setLocation(100, 100); final JTextField f = new JTextField(); f.setColumns(20); FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 100, 0); frame.setLayout(fl); frame.add(f); JButton b = new JButton("找出最大值"); final JLabel l = new JLabel("null"); frame.add(l); frame.add(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String s = f.getText(); if (s.length() == 0 || s.matches("[^\\d\\.]+") ) { l.setText("输入有误!"); return; } String[] line = s.split("[\\s]+"); double max = Double.parseDouble(line[0]); for(int i = 1; i line.length; ++i) { double a = Double.parseDouble(line[i]); if (a max) max = a; } l.setText("最大值:" + max); } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //-----------add by 小月------ import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Test extends JFrame implements ActionListener{ private Container c; private TextField[] text; private JButton btn; private JLabel result; public Test(){ setBounds(100, 200, 800, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); c = getContentPane(); text = new TextField[10]; JPanel NORTH = new JPanel(new FlowLayout()); c.add(NORTH,BorderLayout.NORTH); JPanel center = new JPanel(new FlowLayout()); result = new JLabel(""); center.add(result); c.add(center); for(int i=0;itext.length;i++){ text[i] = new TextField(4); NORTH.add(text[i]); } btn = new JButton("计算"); c.add(btn,BorderLayout.SOUTH); btn.addActionListener(this); } public void actionPerformed(ActionEvent e) { List list = new ArrayList(); for(int i=0;itext.length;i++){ String str = text[i].getText(); if(str==null||"".equals(str)){ result.setText("输入10个整数"); return; } list.add(Integer.parseInt(str)); } Collections.sort(list); result.setText("最大的数值是:"+list.get(9)); } public static void main(String[] args) { new Test().setVisible(true); } }
我来试试,不是很难啦 ,搞定编译过了,文件名为DS.java 缺陷是输入数不能太大···
//编写一个字符界面的Java Application 程序,接受用户从键盘输入的一个正整数,然后统计并输出从1到这个正整数的累加和。
import java.awt.*;
import java.awt.event.*;
public class DS implements ActionListener
{
Frame f=new Frame("输入正整数");
Label l=new Label("请输入整数");
Button b=new Button("确定");
TextField t=new TextField(20);
TextField t2=new TextField(20);
Label l1=new Label("结果为");
DS(){
f.setLayout(new GridLayout(2,3));
f.add(l);
f.add(t);
f.add(b);
f.add(l1);
f.add(t2);
f.setSize(200,300);
f.setVisible(true);
t2.setEditable(false);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
int k = Integer.valueOf(t.getText()).intValue();
int sum=0;
String s="";
for (int i=1;i=k;i++)
{
sum=sum+i;
}
s=s+sum;
t2.setText(s);
}
public static void main(String args[]){
new DS();
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("请输入十个整数:");
for (int i = 0; i arr.length; i++) {
arr[i] = scn.nextInt();
}
Arrays.sort(arr);
System.out.println("最小值为: " + arr[0] + " 最大值为:" + arr[arr.length - 1]);
}
}