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

编写 Java 程序,算出 1 到 1000 的偶数和,并输出显示在控制台。

public class A1{
    public static void main(String[] args){    
        int sum=0;
        for(int i=1;i<=1000;i++){
            if(i%2==0){
                sum = sum+i;
            }
        }
        System.out.print("1-1000的偶数和为:"+sum);
    }
}

假设有文件 C:\a.txt 中的数据为:aaa,bbb,ccc 三个字符串,每个字符串在文件中单独占用一行,现在需要编写程序读取这个文件中的数据,并将其中的数据按从下往上输出到控制台(即输出 ccc,bbb,aaa),如何实现。

public class A2{
    public static void main(String[] args){
            String strArr[] = new String[3];//先定义一个长度为3的字符串数组
            File file = new File("C:\\a.txt");//创建一个代表 C:\a.txt的一个File对象
            FileReader fr;
            try{
                fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                String str;
                int i=0;
                while((str=br.readLine())!=null){
                    strArr[i] = str;
                    i++;
                }
                for(int j=2;i>0;j--){
                    if (j<0){
                        break;
                    }
                    System.out.println(strArr[j]);
                }
                br.close();
                fr.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
}

假设有数组 int[] arr={1,2,3,4,5,6,7,10,13,15},编写代码实现:初始化该数组,利用循环,将数组中 3 的倍数的项进行求和,并输出在控制台。

public class B2{
    public static void main(String[] args){
        int[] arr = {1,2,3,4,5,6,7,10,13,15};
        int sum = 0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]%3==0){
                sum = sum + arr[i];
            }
        }
        System.out.println("该数组中3的倍数的和为"+sum);
    }
}

实现文件的复制功能,现有文件 D:\source.txt,用 Java实现此文件的复制,新文件存放在 D:\dest.txt 文件中。

import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCopy {
    public static void main(String[] args) throws Exception{

        FileInputStream fileIn = new FileInputStream("D:\\source.txt");
        FileOutputStream fileOu = new FileOutputStream("D:\\dest.txt");

        byte[] barr = new byte[32];
        while (fileIn.read(barr) != -1){
            fileOu.write(barr);
        }
        fileIn.close();
        fileOu.flush();
        fileOu.close();
    }
}

假设需要开发一个名为“XXX”(自己的姓名)的程序,请简要说明该java程序的开发过程。

  1. 首先启动eclipse,在顶部菜单栏选择“File-New-Java Project”

  1. 在弹出的窗口中的Project name位置输入项目名:WuDa后,点击Finish

  1. 左栏的“Package Explorer”中右键WuDa目录下的src,选择New-Package

  1. 在弹出的界面中的name位置输入包名:hello后,点击Finish

  1. 右键刚建立的名为hello的空包,选择New-Class

  1. 在弹出的界面中的name位置输入类名:Hello后,点击Finish

  1. 即可在hello目录下生成的hello.java源文件中,编写代码

编写一个Java程序在屏幕上输出:“我的学号:XXX;姓名:XXX。”

package hello;

public class Hello {
    public static void main(String[] args) {
        System.out.println("我的学号:20200712;姓名:武大。");
    }
}

写出以下几条语句:int i = 200;int j=‘a’;char c=(char)(i+j)运算后变量c的结果,并写出计算过程。


结果:乱码符号

过程:”a“对应的ASCII中的值为97,(char)(i+j)相当于(char)(200+97),所以char c = char(297);标准的ASCII只定义了0-127的字符。没有297这个位置的编码符号,所以会输出乱码符号。


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

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

一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3。编程找出1000以内的所有完数。

    public static void main(String[] args) {
        for(int i = 1;i <= 1000;i++) {
            //判断i是不是完数
            int num = 0;
            for(int j = 1;j <= i/2;j++) {
                //找因子
                if(i % j == 0) {
                    num += j;
                }
            }
            if(i == num) {
                System.out.println("完数有:"+i);
            }
        }
    }

在数组{ 12,67,8,98,23,56,124,55,99,100 }中查找并输出最大值和最小值。

 public static void main(String[] args) {
        int[] a = new int[] {12,67,8,98,23,56,124,55,99,100 };//定义数组
        int max = a[0];//默认第一个数
        int min = a[0];
        for (int i = 1; i < a.length; i++) {//遍历数组
            if (a[i] > max) {//判断
                max = a[i];//判断后交换最大值
            }
            if (a[i] < min) {
                min = a[i];
            }
        }
        System.out.println("该数组中最大值为:" + max);
        System.out.println("该数组中最小值为:" + min);
    }

从标准输入(即键盘)读入10个整数存入整型数组a中,然后逆序输出这10个整数。

 public static void main(String[] args){

        Scanner sc=new Scanner(System.in);
        int arr[]=new int[10];
        System.out.print("请输入10个整数:");

        for(int i=0;i<10;i++){
            arr[i]=sc.nextInt();
        }
        System.out.print("逆序输出:");

        for(int i=9;i>=0;i--){
            System.out.print(arr[i]+" ");
        }
    }

按以下要求编写程序:(1) 创建一个Rectangle类,添加width和height两个整型成员变量;(2) 在Rectangle中添加两种方法getLength()和getArea()两种方法分别计算矩形的周长和面积;(3) 编程利用Rectangle输出一个矩形的周长和面积。

public class Rectangle {

    private int width;
    private int height;

    public Rectangle(int width,int height){
        this.width = width;
        this.height = height;
    }

    public int getLength() {
        int girth = (width+height)*2;
        return girth;
    }

    public int getArea(){
        int area = width*height;
        return area;
    }

    public static void main(String[] args) {
        int girth, area;
        Rectangle rectangle = new Rectangle(10,20);
        girth = rectangle.getLength();
        area = rectangle.getArea();
        System.out.println("该矩形的周长是:" + girth);
        System.out.println("该矩形的面积是:" + area);
    }
}

按以下要求编写程序:(1) 编写Animal接口,接口中声明run() 方法;(2) 定义Bird类和Fish类实现Animal接口;(3) 编写Bird类和Fish类的测试程序,并调用其中的run()方法。

// Animal 接口
public interface Animal {
     void run();
}
//Bird类
class Bird implements Animal{
    @Override
    public void run() {
        System.out.println("小鸟");
    }
}
//Fish类
class Fish implements Animal{
    @Override
    public void run() {
        System.out.println("小鱼");
    }
}
//测试类
class AnimalTest {
    public static void main(String[] args) {
        Animal b = new Bird();
        Animal f = new Fish();
        b.run();
        f.run();
    }
}

从键盘录入一个字符串,统计该串中大写字母、小写字母、数字和其他字符各有多少个。比如:Hello12345@World!大写:2个;小写:8个;数字:5个;其他字符:2个。

    public static void main(String[] args) {

        System.out.print("请输入字符串:");
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();

        int count = 0;
        int big_count = 0;
        int small_count = 0;
        int other_count = 0;

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                big_count++;
            } else if (c >= 'a' && c <= 'z') {
                small_count++;
            } else if (c >= '0' && c <= '9') {
                count++;
            }else{
                other_count++;
            }
        }

        System.out.println("大写字母:" + big_count+"个");
        System.out.println("小写字母:" + small_count+"个");
        System.out.println("数字:" + count+"个");
        System.out.println("其他字符:" + other_count+"个");
    }

实现文件的复制功能,现有文件D:\source.pm4,用Java实现此文件的复制,新文件存放在D:\dest.pm4文件中。要求使用字节流进行复制,且输入缓存的大小不能超过32个字节。

import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCopy {
    public static void main(String[] args) throws Exception{

        FileInputStream fileIn = new FileInputStream("D:\\source.pm4");
        FileOutputStream fileOu = new FileOutputStream("D:\\dest.pm4");

        byte[] barr = new byte[32];
        while (fileIn.read(barr) != -1){
            fileOu.write(barr);
        }
        fileIn.close();
        fileOu.close();
    }
}

猜你喜欢

转载自blog.csdn.net/zhou_ge1/article/details/129234638