[编程题] Java基础编程练习题(一)

点击上方“Coder编程”,选择“置顶公众号”

技术文章第一时间送达!

Java基础编程练习题(一)

1.编写一个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语句

2.编写一个Java程序在屏幕上输出1!+2!+3!+……+10!的和。(循环)

// programme name ForTest.java
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);
}
}

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;i<array.length;i++)
            for(int j=0;j<array.length-1;j++){
                if(array[j]>array[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
           showArray(array);  
    }
    //遍历数组,并输出数组的元素。
    public void showArray(int[] array){
        for(int i=0;i<array.length;i++){      
            System.out.print(array[i]+"\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()<6){
            System.out.println("两次输入密码不一样或密码不能小于6位");   
            }
            System.out.println("\n"+"请重新输入");
             System.out.print("用户名:");
             name=input.next();
             System.out.print("密码:");
             password=input.next();
             System.out.print("验证密码:");
             newPassword=input.next(); 
        }
         System.out.println("注册成功!");
        }   
        }

 public class Verify {
    public static void main(String[] args) {
        Register m1=new Register();
        m1.nameExe();
    }
}

6.一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购买的门票价格并输出,然后写出测试类测试该类(类的基本实现)

public 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); 
     }
}/

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.java
import 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));
       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 );
    }
   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.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( );
}
}

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 int[200];
          try{ 
             // File file=new File("dest.txt");
              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=0;i<data.length;i++) {
              t[i]=in.readInt();
              }
          for(int i= data.length-1;i>= 0;i--) {
              System.out.print("  "+t[i]);
              }

         /* 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());}
          }
      }

推荐

400道——大厂Java选择题

HR面试都会问什么问题?(上)

HR面试都会问什么问题(下)

文末

欢迎关注个人微信公众号:Coder编程
欢迎关注Coder编程公众号,主要分享数据结构与算法、Java相关知识体系、框架知识及原理、Spring全家桶、微服务项目实战、DevOps实践之路、每日一篇互联网大厂面试或笔试题以及PMP项目管理知识等。更多精彩内容正在路上~
新建了一个qq群:315211365,欢迎大家进群交流一起学习。谢谢了!也可以介绍给身边有需要的朋友。

文章收录至
Github: https://github.com/CoderMerlin/coder-programming
Gitee: https://gitee.com/573059382/coder-programming
欢迎关注并star~

微信公众号

                   我知道你 “在看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值