编辑代码

import java.util.*;
public class Main {
	public static void main(String[] args) {
        int year;
        Scanner input=new Scanner(System.in);
        System.out.println("please input a year:");
        year=input.nextInt();
        if((year%4==0 && year%100!=0)||(year%400==0))
            System.out.println(year+" 年是闰年。");
        else
            System.out.println(year+" 年不是闰年。");
	}
}


public class TestWanshu {
    public static void main(String[] args) {
        System.out.println("1到1000的完数有: "); 
        for(int i=1; i<1000; i++) {
            int t = 0;
            for(int j=1; j<= i/2; j++) { 
                if(i % j == 0) {
                    t = t + j;
                }
            }
            if(t == i) { 
                System.out.print(i + "	");
            }
        }
    }
}


import java.io.*; 
import java.util.*;
public class TestStudentScore {
    public static void main(String[] args){ 
        Scanner ss = new Scanner(System.in);
        String[][] a = new String[3][6];
        for (int i = 1; i < 4; i++) {
            System.out.print("请输入第" + i + "个学生的学号:");
            a[i - 1][0] = ss.nextLine();
            System.out.print("请输入第" + i + "个学生的姓名:");
            a[i - 1][1] = ss.nextLine();
            for (int j = 1; j < 4; j++) {
                System.out.print("请输入该学生的第" + j + "个成绩:");
                a[i - 1][j + 1] = ss.nextLine();
            }
            System.out.println("\n");
        }
        float avg;
        int sum;
        for (int i = 0; i < 3; i++) {
            sum = 0;
            for (int j = 2; j < 5; j++) {
                sum = sum + Integer.parseInt(a[i][j]);
            }
            avg = (float) sum / 3;
            a[i][4] = String.valueOf(avg);
        }
        System.out.println("学生的平均成绩为:");
        for (int i = 0; i < 3; i++) {
            System.out.println(a[i][4]);
        }
    }
}

import java.util.*;
public class TestThreeData {
    public static void main(String[] args) { 
        int x=0, y=0, z=0;
        Scanner input=new Scanner(System.in); 
        System.out.println("please input three data:"); 
        x=input.nextInt();
        y=input.nextInt(); z=input.nextInt();
        if(x > y) { 
            int t=x; 
            x = y;
            y = t;
        }
        if(x > z) {
            int t = x; x = z;
            z = t;
        }
        if(y > z) {
            int t = y;
            y = z;
            z = t;
        }
        System.out.println( "三个数字由小到大排列为: "+x + " " + y + " " + z);
    }
}



import java.util.*;
public class TestCountChar {
    public static void main(String[] args) { 
        int digital = 0;
        int character = 0; 
        int other = 0;
        int blank = 0; 
        char[] ch = null;
        Scanner sc = new Scanner(System.in); 
        System.out.println("please input a line String:"); 
        String s = sc.nextLine();
        ch = s.toCharArray();
        for(int i=0; i<ch.length; i++) { 
            if(ch[i] >= '0'&& ch[i] <= '9') { 
                digital ++;
            } else if((ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A' && ch[i] <= 'Z')) { 
                character ++;
            } else if(ch[i]== ' ') { 
                blank ++;
            } else { 
                other ++;
            }
        }
        System.out.println("数字个数: " + digital); 
        System.out.println("英文字母个数: " + character); 
        System.out.println("空格个数: " + blank); 
        System.out.println("其他字符个数:" + other );
    }
    
}



import java.util.Scanner;
class Tourist {
    int age;
    int ticketPrice;
    public void setAge(int age) {
        this.age = age;
    }
    public void ticket() {
        if (age > 0 && age < 12)
            ticketPrice = 20;
        else if (age < 20)
            ticketPrice = 40;
        else if (age < 50)
            ticketPrice = 80;
        else
            ticketPrice = 35;
        System.out.println("门票价格:" + ticketPrice);
    }
}
public class TestTourist {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Tourist t1 = new Tourist();
        System.out.print("请输入年龄:");
        t1.setAge(input.nextInt());
        t1.ticket();
    }
}




import java.util.Scanner; 
public class CountEachLetter {
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in); 
        System.out.print("Enter a string: "); 
        String s = input.nextLine();
        int[] counts = countLetters(s.toLowerCase()); 
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] != 0) {
                System.out.println((char)('a' + i) + " appears " + counts[i] + ((counts[i] == 1) ? " time" : " times"));
            }
        }
    }
    public static int[] countLetters(String s) { 
        int[] counts = new int[26]; 
        for (int i = 0; i < s.length(); i++) { 
            if (Character.isLetter(s.charAt(i))) {
                counts[s.charAt(i) - 'a']++; 
            }
        }
        return counts; 
    }
}



import java.util.*;
public class TestMax {
    public static void main(String[] args) {
        int a, b, m;
        Scanner s = new Scanner(System.in);
        System.out.print("键入一个整数: ");
        a = s.nextInt();
        System.out.print("再键入一个整数: ");
        b = s.nextInt();
        deff cd = new deff();
        m = cd.deff(a,b);
        int n = a * b / m;

        System.out.println("最大公约数: " + m);
        System.out.println("最小公倍数: " + n);
    }
}
class deff {
    public int gcd(int x, int y) {
        int t;
        if (x < y) {
            t = x;
            x = y;
            y = t;
        }
        while (y != 0) {
            if (x == y) return x; else {
                int k = x % y;
                x = y;
                y = k;
            }
        }
        return x;
    }
}


public class TestCourse {
    public static void main(String[] args) {
        Course course1 = new Course("Data Structures"); 
        Course course2 = new Course("Database Systems"); 
        course1.addStudent("Peter Jones"); 
        course1.addStudent("Brian Smith"); 
        course1.addStudent("Anne Kennedy"); 
        course2.addStudent("Peter Jones"); 
        course2.addStudent("Steve Smith"); 
        System.out.println("Number of students in course1: "+ course1.getNumberOfStudents());
        String[] students = course1.getStudents();
        for (int i = 0; i < course1.getNumberOfStudents(); i++){
            System.out.print(students[i] + ", "); 
            System.out.println();
            System.out.print("Number of students in course2: "+ course2.getNumberOfStudents());
        }    
    }
}
public class Course {
    private String courseName;
    private String[] students = new String[100]; 
    private int numberOfStudents;
    public Course(String courseName) { 
        this.courseName = courseName;
    }
    public void addStudent(String student) { 
        students[numberOfStudents] = student; 
        numberOfStudents++;
    }
    public String[] getStudents() { 
        return students;
    }	
    public int getNumberOfStudents() { 
        return numberOfStudents;
    }
    public String getCourseName() { 
        return courseName;
    }
}




public class ForTest {
    public static void main( String args[] ) {
        int i,j,mul,sum=0;
        for(i=1;i<=10;i++) {
            mul=1;
            for(j=1;j<=i;j++) {
                mul=mul*j;
            }
            sum=sum+mul;
        }
        System.out.println("1!+2!+3!+……+10!= "+sum);
    }
}



import javax.swing.*;
import java.awt.event.*;
public class ButtonEventDemo extends JPanel implements ActionListener {
    protected JButton b1;
    public ButtonEventDemo() {
        ImageIcon ButtonIcon = new ImageIcon("images/green.png");
        b1 = new JButton("退出按钮", ButtonIcon);
        b1.setMnemonic(KeyEvent.VK_E);
        b1.setToolTipText("这是退出按钮。");
        this.add(b1);
        b1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
    private static void createGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("按钮测试");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ButtonEventDemo CPane = new ButtonEventDemo();
        CPane.setOpaque(true);
        frame.setContentPane(CPane);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createGUI();
            }
        });
    }
}



import java.io.*;
public class Strinput {
    public static void main(String args[]) {
        String s1, s2, ss, si, sf;
        int i1, i2;
        float f1, f2;
        s1 = "";
        s2 = "";
        BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.print("输入第一个字符串:");
            s1 = strin.readLine();
            System.out.print("输入第二个字符串:");
            s2 = strin.readLine();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        i1 = Integer.parseInt(s1);
        i2 = Integer.parseInt(s2);
        f1 = Float.parseFloat(s1);
        f2 = Float.parseFloat(s2);
        ss = strAdd(s1, s2);
        si = strAdd(i1, i2);
        sf = strAdd(f1, f2);
        System.out.println("输入的二个字符串相加结果为:" + ss);
        System.out.println("输入字符串转换为整数相加结果为:" + si);
        System.out.println("输入字符串转换为浮点数相加结果为:" + sf);
    }
    static String strAdd(String str1, String str2) {
        return str1 + str2;
    }
    static String strAdd(int int1, int int2) {
        return String.valueOf(int1 + int2);
    }
    static String strAdd(float flt1, float flt2) {
        return String.valueOf(flt1 + flt2);
    }
}