public class Demo {
成都創(chuàng)新互聯(lián)-云計算及IDC服務(wù)提供商,涵蓋公有云、IDC機(jī)房租用、雅安機(jī)房托管、等保安全、私有云建設(shè)等企業(yè)級互聯(lián)網(wǎng)基礎(chǔ)服務(wù),來電聯(lián)系:028-86922220
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int inputNum;
for (; ; ) {
System.out.println("請輸入一個4位整數(shù): ");
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("該整數(shù)的各位數(shù)字之和: " + 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個整數(shù)"); return; } list.add(Integer.parseInt(str)); } Collections.sort(list); result.setText("最大的數(shù)值是:"+list.get(9)); } public static void main(String[] args) { new Test().setVisible(true); } }
我來試試,不是很難啦 ,搞定編譯過了,文件名為DS.java 缺陷是輸入數(shù)不能太大···
//編寫一個字符界面的Java Application 程序,接受用戶從鍵盤輸入的一個正整數(shù),然后統(tǒng)計并輸出從1到這個正整數(shù)的累加和。
import java.awt.*;
import java.awt.event.*;
public class DS implements ActionListener
{
Frame f=new Frame("輸入正整數(shù)");
Label l=new Label("請輸入整數(shù)");
Button b=new Button("確定");
TextField t=new TextField(20);
TextField t2=new TextField(20);
Label l1=new Label("結(jié)果為");
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("請輸入十個整數(shù):");
for?(int?i?=?0;?i??arr.length;?i++)?{
arr[i]?=?scn.nextInt();
}
Arrays.sort(arr);
System.out.println("最小值為:?"?+?arr[0]?+?"?最大值為:"?+?arr[arr.length?-?1]);
}
}