[Java] Basic exercises (programming questions)

Write a Java program to calculate the sum of even numbers from 1 to 1000, and display the output on the console.

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

Assume that the data in the file C:\a.txt is: aaa, bbb, ccc three strings, each string occupies a separate line in the file, now you need to write a program to read the data in this file, and convert the The data is output to the console from bottom to top (that is, output ccc, bbb, aaa), how to achieve it.

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

Assuming there is an array int[] arr={1,2,3,4,5,6,7,10,13,15}, write code to realize: initialize the array, and use a loop to carry out items that are multiples of 3 in the array Summed and output to the console.

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

Realize the file copy function, the existing file D:\source.txt, use Java to realize the copy of this file, the new file is stored in the D:\dest.txt file.

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

Suppose you need to develop a program named "XXX" (your own name), please briefly explain the development process of this java program.

  1. First start eclipse, select "File-New-Java Project" in the top menu bar

  1. Enter the project name in the Project name position in the pop-up window: WuDa, and click Finish

  1. In the "Package Explorer" on the left column, right-click the src under the WuDa directory and select New-Package

  1. Enter the package name in the name position of the pop-up interface: hello, and click Finish

  1. Right-click the empty package named hello just created, select New-Class

  1. Enter the class name in the name position in the pop-up interface: Hello, and click Finish

  1. You can write code in the hello.java source file generated in the hello directory

Write a Java program to output on the screen: "My student number: XXX; name: XXX."

package hello;

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

Write the following statements: int i = 200; int j='a'; char c=(char)(i+j) the result of the variable c after the operation, and write out the calculation process.


Result: Garbled symbols

Process: The value in ASCII corresponding to "a" is 97, (char)(i+j) is equivalent to (char)(200+97), so char c = char(297); standard ASCII only defines 0- 127 characters. There is no encoding symbol at position 297, so it will output garbled symbols.


Write a Java program that outputs 1 on the screen! +2! +3! +...+10! of and.

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

A number is called a "perfect number" if it is exactly equal to the sum of its factors. For example, 6=1+2+3. Program to find all perfect numbers up to 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);
            }
        }
    }

Find and output the maximum and minimum values ​​in the array {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);
    }

Read 10 integers from the standard input (that is, the keyboard) and store them in the integer array a, and then output the 10 integers in reverse order.

 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]+" ");
        }
    }

Write the program according to the following requirements: (1) Create a Rectangle class, add two integer member variables of width and height; (2) Add two methods getLength() and getArea() to Rectangle to calculate the circumference of the rectangle respectively Length and area; (3) Programming uses Rectangle to output the perimeter and area of ​​a 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);
    }
}

Write the program according to the following requirements: (1) write the Animal interface, and declare the run() method in the interface; (2) define the Bird class and the Fish class to implement the Animal interface; (3) write the test program of the Bird class and the Fish class, and call them The run() method.

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

Enter a string from the keyboard, and count the number of uppercase letters, lowercase letters, numbers and other characters in the string. For example: Hello12345@World! Uppercase: 2; lowercase: 8; numbers: 5; other characters: 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+"个");
    }

Realize the file copy function, the existing file D:\source.pm4, use Java to realize the copy of this file, the new file is stored in the D:\dest.pm4 file. A byte stream is required for copying, and the size of the input buffer cannot exceed 32 bytes.

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

Guess you like

Origin blog.csdn.net/zhou_ge1/article/details/129234638