import java.util.Calendar;
創(chuàng)新互聯(lián)是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、網(wǎng)站制作、成都網(wǎng)站建設(shè)、網(wǎng)站備案、服務(wù)器租用、域名申請、軟件開發(fā)、微信平臺小程序開發(fā)等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運營推廣經(jīng)驗的科技公司,有著多年的網(wǎng)站建站經(jīng)驗,致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開一個面向全國乃至全球的業(yè)務(wù)窗口:建站電話聯(lián)系:028-86922220
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class H {
public static void main(String args[]) {
new Time("年齡計算器");
}
}
class Time extends Frame implements ActionListener {
Calendar calendar;
Button button;
TextField t1, t2, t3;
Label l, l1, l2, l3;
Time(String s) {
super(s);
setLayout(new FlowLayout());
button = new Button("確定");
button.addActionListener(this);
t1 = new TextField(2);
t2 = new TextField(2);
t3 = new TextField(2);
l = new Label(" 請輸入您的生日 ");
l.setBackground(Color.cyan);
l1 = new Label("年");
l2 = new Label("月");
l3 = new Label("日");
add(l);
add(t1);
add(l1);
add(t2);
add(l2);
add(t3);
add(l3);
add(button);
setBounds(100, 100, 280, 100);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
calendar = Calendar.getInstance();
calendar.setTime(new Date());
NumberFormat f = NumberFormat.getInstance();
long time = calendar.getTimeInMillis();
if (e.getSource() == button) {
try {
int n = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int r = Integer.parseInt(t3.getText());
calendar.set(n, y - 1, r);
double time1 = calendar.getTimeInMillis();
double c = (time - time1) / (1000 * 60 * 60 * 24);
double d = c/365;
f.setMaximumFractionDigits(2);
String s = f.format(d);
l.setText("您的年齡約為" + s + " 歲");
} catch (NumberFormatException ee) {
l.setText("請正確輸入");
}
}
}
}
功底淺薄,如果有問題,還望指教。
可以用循環(huán)啊,定義類person
Class person{
@Getter
@Setter
private int age;
@Getter
@Setter
private String name;
public person(int age,String name){
this.age=age;
this.name=name.
}
}
判斷年齡就用數(shù)組做了,假設(shè)你放一個ArrayList里
ArrayListPerson personList=new ArrayList();
personList.add(父親)
personList.add(爺爺)
personList.add(兒子)
for(Person person:personList){
if (person.getage90){
game over
}else if(person.getage0){
洗洗睡吧
}else{
打印名字和年齡 getname()+getage()
}
}
計算全班同學(xué)年齡之和。
思路:從鍵盤上依次輸入每個同學(xué)的年齡,然后求和輸出:
import java.util.Scanner;
public class TestMain {
public static void main(String[] args) {
int sum=0;
Scanner in =new Scanner(System.in);
int n = in.nextInt();
sum+=n;
System.out.println(sum);
}
}