JAVA编程题全集(100题及答案)w

上传人:xian****hua 文档编号:132593031 上传时间:2022-08-08 格式:DOC 页数:13 大小:60.01KB
返回 下载 相关 举报
JAVA编程题全集(100题及答案)w_第1页
第1页 / 共13页
JAVA编程题全集(100题及答案)w_第2页
第2页 / 共13页
JAVA编程题全集(100题及答案)w_第3页
第3页 / 共13页
点击查看更多>>
资源描述
Java程序设计总复习题1、 编写一个Java程序,用if-else语句判断某年份是否为闰年。(分支)/ Programme Name LeapYear.javapublic class LeapYearpublic static void main(String args)int year=2010; if(args.length!=0) year=Integer.parseInt(args0);if(year%4=0 & year%100!=0)|(year%400=0) System.out.println(year+ 年是闰年。);else System.out.println(year+ 年不是闰年。); /if-else语句2、 编写一个Java程序在屏幕上输出1!+2!+3!+10!的和。(循环)/ programme name ForTest.javapublic 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);3、 依次输入10个学生成绩,判断学生(优秀、良好、中等、及格、不及格)并计算人数(switch)4、 使用冒泡排序(数组)public class BubbleSort public static void main(String args) int array=63,4,24,1,3,5;BubbleSort sorter=new BubbleSort();sorter.sort(array);/冒泡排序public void sort(int array)for(int i=1;iarray.length;i+)for(int j=0;jarrayj+1)int temp=arrayj;arrayj=arrayj+1;arrayj+1=temp; showArray(array);/遍历数组,并输出数组的元素。public void showArray(int array)for(int i=0;iarray.length;i+) System.out.print(arrayi+t);System.out.println();5、 实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同 (字符串)import java.util.Scanner;public class Register String name;String password;String newPassword;/public void nameExe()Scanner input=new Scanner(System.in);System.out.println(请输入用户名,密码和验证密码); System.out.print(用户名:); name=input.next(); System.out.print(密码:); password=input.next(); System.out.print(验证密码:); newPassword=input.next();while(name.length()3|(password.equals(newPassword)=false)|(password.length()6)if(name.length()3)System.out.println(用户名不能小于3);if(password.equals(newPassword)=false)|password.length()0&age12) ticketPrice=20; else if(age20) ticketPrice=40; else if(age50) ticketPrice=80; else ticketPrice=35; System.out.println(门票价格:+ticketPrice); /import java.util.Scanner;public class Test 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(); 7、 (1)编写一个圆类Circle,该类拥有:一个成员变量Radius(私有,浮点型); / 存放圆的半径; 两个构造方法 Circle( ) / 将半径设为0 Circle(double r ) /创建Circle对象时将半径初始化为r 三个成员方法 double getArea( ) /获取圆的面积 double getPerimeter( ) /获取圆的周长 void show( ) /将圆的半径、周长、面积输出到屏幕(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:一个成员变量double hight(私有,浮点型); / 圆柱体的高; 构造方法 Cylinder (double r, double h ) /创建Circle对象时将半径初始化为r 成员方法 double getVolume( ) /获取圆柱体的体积 void showVolume( ) /将圆柱体的体积输出到屏幕编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。/Programme Name TestCylinder.java class Circle /定义父类-园类private double radius; /成员变量-园半径Circle() /构造方法radius=0.0;Circle(double r) /构造方法 radius=r; double getPerimeter() /成员方法-求园周长return 2*Math.PI*radius;double getArea() /成员方法-求园面积return Math.PI*radius*radius;void disp() /成员方法-显示园半径、周长、面积System.out.println(圆半径=+radius);System.out.println(圆周长=+getPerimeter();System.out.println(圆面积=+getArea();class Cylinder extends Circle /定义子类-圆柱类private double hight; /成员变量-园柱高Cylinder(double r,double h) /构造方法 super(r);hight=h;public double getVol() /成员方法-求园柱体积return getArea()*hight;public void dispVol() /成员方法-显示园柱体积System.out.println(圆柱体积=+getVol();public class TestCylinder /定义主类public static void main(String args) /主程入口 Circle Ci=new Circle(10.0); / 生成园类实例 Ci.disp(); / 调用园类的方法 Cylinder Cyl=new Cylinder(5.0,10.0); /生成圆柱类实例 Cyl.disp(); /调用父类方法 Cyl.dispVol(); /调用子类方法 8、 编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。(异常处理)/programme name Strinput.javaimport java.io.*;public class Strinput public static void main(String args) String s1=null,s2=null,ss,si,sf; int i1,i2; float f1,f2; BufferedReader strin=new BufferedReader(new InputStreamReader(System.in); trySystem.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 ); 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); 9、 应用FileInputStream类,编写应用程序,从磁盘上读取一个Java程序,并将源程序代码显示在屏幕上。(被读取的文件路径为:E:/myjava/Hello.java)/ Programme Name FISDemo.javaimport java.io.*; public class FISDemo public static void main(String args) byte buf=new byte2056; 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( );10、 编写一个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=new int200; try / File file=new File(dest.txt); DataOutputStream out = new DataOutputStream (new FileOutputStream(dest.txt); for(int i=0;idata.length;i+) out.writeInt(datai); out.close(); DataInputStream in = new DataInputStream (new FileInputStream(dest.txt); /先读出来再倒序输出 for(int i=0;i= 0;i-) System.out.print( +ti); /* for(int i= data.length-1;i= 0;i-) t=in.readInt(datai); System.out.print( +t); */ System.out.println( ); in.close(); catch(IOException e) System.out.println(e.getMessage(); 11、 编写一个Java程序实现多线程,在线程中输出线程的名字,隔300毫秒输出一次,共输出20次。/ programme name TestThread; / 声明一个子线程类Threaddemo; class ThreadDemo extends Thread public ThreadDemo(String str) super(str); public void run() for(int i=0;i20;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(); 10. 编写程序,在屏幕上显示带标题的窗口,并添加一个按钮。当用户单击按钮时,结束程序。(窗体编程)/ Programme Name ButtonEventDemo.javaimport javax.swing.*;import java.awt.event.*;public class ButtonEventDemo extends JPanel implements ActionListenerprotected 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(); ); 12、定义一个表示学生信息的类Student,要求如下:(有关类的编程)(1)类Student的成员变量:sNO表示学号;sName表示姓名;sSex表示性别;sAge表示年龄;sJava:表示Java课程成绩。(2)类Student带参数的构造方法: 在构造方法中通过形参完成对成员变量的赋值操作。(3)类Student的方法成员:getNo():获得学号; getName():获得姓名; getSex():获得性别;getAge()获得年龄;getJava():获得Java 课程成绩(4) 根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。/Pragramme name Student;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 Student5;st0=new Student(09zc01,张三,男,19,94);st1=new Student(09zc02,李四,男,20,85);st2=new Student(09zc03,王五,女,18,96);st3=new Student(09zc04,赵六,男,17,90);st4=new Student(09zc05,杨七,女,21,88); int max=0,min=100,sum=0; System.out.println( 学生信息:); for (int i=0;ist.length;i+) if (sti.sJava max) max=sti.sJava; sum=sum+sti.sJava; System.out.println(学生编号:+sti.getNO()+, 姓名:+sti.getName()+, 性别:+sti.getSex()+, 年龄:+sti.getAge()+, Java课学分:+sti.getJava(); System.out.println(); System.out.println( 共有学生:+st.length+, 平均成绩:+sum/st.length); System.out.println( 最小学分:+min+, 最大学分:+max);
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!