后续有时间把没有的答案的补全!
1, 使用冒泡排序
2, 实现会员注册, 要求用户名长度不小于 3, 密码长度不小于 6, 注册时两次输入密码必须相同
3, 一个景区根据游人的年龄收取不同价格的门票. 请编写游人类, 根据年龄段决定能够购买的门票价格并输出, 然后写出测试类测试该类
2. 编写一个 Java 程序, 用 if-else 语句判断某年份是否为闰年
- // Programme Name LeapYear.java
- public class LeapYear{
- public static void main(String args[]){
- int year=2010;
- if(args.length!=0)
- year=Integer.parseInt(args[0]);
- if((year%4==0 && year%100!=0)||(year%400==0))
- System.out.println(year+"年是闰年.");
- else
- System.out.println(year+"年不是闰年.");
- }
- }//if-else 语句
- View Code
3, 编写一个 Java 程序在屏幕上输出 1!+2!+3!+......+10! 的和
- 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);
- }
- }
- View Code
5, 编写一个 Java 应用程序, 从键盘读取用户输入两个字符串, 并重载 3 个函数分别实现这两个字符串的拼接, 整数相加和浮点数相加. 要进行异常处理, 对输入的不符合要求的字符串提示给用户, 不能使程序崩溃
- import java.io.*;
- public class Strinput
- {
- public static void main(String args[]) {
- String s1,s2,ss,si,sf;
- int i1,i2;
- float f1,f2;
- BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
- try{System.out.print ("输入第一个字符串:" );
- s1= strin.readLine();
- System.out.print ("输入第二个字符串:" );
- s2= strin.readLine();}
- catch(Exception 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 );
- }
- String strAdd(String str1,String str2) {
- return str1+str2;
- }
- String strAdd(int int1,int int2) {
- return String.valueOf(int1+int2);
- }
- String strAdd(float flt1,float flt2) {
- return String.valueOf (flt1+flt2);
- }
- }
- View Code
6. 应用 FileInputStream 类, 编写应用程序, 从磁盘上读取一个 Java 程序, 并将源程序代码显示在屏幕上.(被读取的文件路径为: E:/myjava/Hello.java)
- import java.io.*;
- public class FISDemo {
- public static void main(String args[]) {
- byte[] buf=new byte[2056];
- try{
- FileInputStream fileIn=new FileInputStream("e:/myjava/Hello.java");
- int bytes=fileIn.read(buf,0,2056);
- String str=new String(buf,0,bytes);
- System.out.println(str);
- }catch(Exception e){
- e.printStackTrace( );
- }
- }
- View Code
7, 编写一个 Java 程序将当 100,101,102,103,104,105 个数以数组的形式写入到 Dest.txt 文件中, 并以相反的顺序读出显示在屏幕上
- import java.io.*;
- public class IODemo {
- public static void main( String args[] ) {
- int data[] = {100,101,102,103,104,105};
- int t;
- try
- { DataOutputStream out = new DataOutputStream (new FileOutputStream("dest.txt"));
- for(int i=0;i<data.length;i++)
- out.WriteInt(data[i]);
- out.close();
- DataInputStream in = new DataInputStream (new FileInputStream("dest.txt"));
- for(int i= data.length-1;i>= 0;i--) {
- t=in.readInt(data[i]);
- System.out.print(" "+t);
- }
- System.out.println( );
- in.close();
- }catch(IOException e)
- { System.out.println(e.getMessage());}
- }
- }
- View Code
9, 编写一个 Java 程序实现多线程, 在线程中输出线程的名字, 隔 300 毫秒输出一次, 共输出 20 次.
- // 声明一个子线程类 Threaddemo;
- class ThreadDemo extends Thread {
- public ThreadDemo(String str) {
- super(str);
- }
- public void run() {
- for(int i=0;i<20;i++){
- System.out.print(" "+this.getName());
- Try {
- Sleep(300);
- }catch(InterruptedException e){
- System.out.println(e.getMessage());
- Return;
- }
- }
- System.out.println("/end");
- }
- }
- public class TestThread {
- public static void main( String args[] ) {
- ThreadDemo thread1=new ThreadDemo("T1");
- ThreadDemo thread2=new ThreadDemo("T2");
- ThreadDemo thread3=new ThreadDemo("T3");
- thread1.start();
- thread2.start();
- thread3.start();
- }
- }
- View Code
10. 编写程序, 在屏幕上显示带标题的窗口, 并添加一个按钮. 当用户单击按钮时, 结束程序.
- 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 的助记符是 Alt+E
- b1.setToolTipText("这是退出按钮."); // 设置按钮提示条
- this.add(b1); // 往面板对象中加载按钮
- b1.addActionListener(this); // 本类对象注册为按钮的事件监听器
- }
- public void actionPerformed(ActionEvent e){ // 按钮事件响应方法
- System.exit(0); // 按 b1 则退出主程序
- }
- private static void createGUI() { // 创建窗体
- JFrame.setDefaultLookAndFeelDecorated(true); // 设置 java 隐含观感
- 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) { // 将 createGUI() 列入线程
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createGUI();
- }
- });
- }
- }
- View Code
11, 定义一个表示学生信息的类 Student, 要求如下:
(1) 类 Student 的成员变量:
sNO 表示学号; sName 表示姓名; sSex 表示性别; sAge 表示年龄; sJava: 表示 Java 课程成绩.
(2) 类 Student 带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作.
(3) 类 Student 的方法成员:
getNo(): 获得学号;
getName(): 获得姓名;
getSex(): 获得性别;
getAge() 获得年龄;
getJava(): 获得 Java 课程成绩
(4) 根据类 Student 的定义, 创建五个该类的对象, 输出每个学生的信息, 计算并输出这五个学生 Java 语言成绩的平均值, 以及计算并输出他们 Java 语言成绩的最大值和最小值.
- public class Student {
- String sNO,sName,sSex;
- int sAge,sJava;
- public Student(String XH,String XM,String XB,int NL,int XF) {
- super();
- sNO=XH;
- sName=XM;
- sSex=XB;
- sAge=NL;
- sJava=XF;
- }
- public String getNO() {
- return sNO;
- }
- public String getName() {
- return sName;
- }
- public String getSex() {
- return sSex;
- }
- public int getAge() {
- return sAge;
- }
- public int getJava() {
- return sJava;
- }
- public static void main(String[] args) {
- Student[] st=new Student[5];
- st[0]=new Student("09zc01","张三","男",19,94);
- st[1]=new Student("09zc02","李四","男",20,85);
- st[2]=new Student("09zc03","王五","女",18,96);
- st[3]=new Student("09zc04","赵六","男",17,90);
- st[4]=new Student("09zc05","杨七","女",21,88);
- int max=0,min=100,sum=0;
- System.out.println("学生信息:");
- for (int i=0;i<st.length;i++) {
- if (st[i].sJava <min)
- min=st[i].sJava;
- if (st[i].sJava> max)
- max=st[i].sJava;
- sum=sum+st[i].sJava;
- System.out.println("学生编号:"+st[i].getNO()+", 姓名:"+st[i].getName()+", 性别:"+st[i].getSex()+", 年龄:"+st[i].getAge()+", Java 课学分:"+st[i].getJava());
- }
- System.out.println();
- System.out.println("共有学生:"+st.length+", 平均成绩:"+sum/st.length);
- System.out.println("最小学分:"+min+", 最大学分:"+max);
- }
- }
- View Code
[更多学习参考]
链接: https://pan.baidu.com/s/1Xm4Qt6eWD5cE2t_ZpKk-ow 密码: z6qh
来源: http://www.bubuko.com/infodetail-2693607.html