Java programming written test questions 1-5

In a string, consecutive repeated elements are removed

import java.util.Scanner;

public class Test01 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner=new Scanner(System.in);
        String str=scanner.next();
        String myArray[]=str.split(",");
        String newArray="";
        for (int i = 0; i < myArray.length-1; i++) {
    
    
            if(myArray[i]!=myArray[i+1]){
    
    
                newArray=newArray+myArray[i];
            }
        }
            System.out.print(newArray);


    }
}

In the second string, output the array in reverse order

public class Test02 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner=new Scanner(System.in);
        String str=scanner.next();
        for (int i = 0; i < str.length(); i++) {
    
    
            System.out.print(str.charAt(str.length()-i-1)+"");
            
        }
    }

}

Three to sort the array

public class Test04 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner=new Scanner(System.in);
        int []arr=new int[25];
        for (int i = 0; i < ; i++) {
    
    
            arr[i]= scanner.nextInt();
        }
        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.println(arr[i]);
        }
    }
}

Four output 99 multiplication table

public class Test04 {
    
    
    public static void main(String[] args) {
    
    
        for (int i = 1; i <=9 ; i++) {
    
    
            for(int j=1;j<=i;j++){
    
    
            System.out.print(i+"*"+j+"="+i*j);
          }  
          System.out.print();
            
        }
    }
}

5. Enter some characters on the keyboard and write them to a disk file one by one until a # is entered

public class Test04 {
    
    
    public static void main(String[] args) {
    
    
    Scanner sc=new Scanner(System.in);
    File file=new file("test");
    FileWriter filewriter=new FileWriter("file");
    BufferdWriter bw=new BufferdWriter("filewriter");
    String str;
    while( true){
    
    
    System.out.println("请输入信息");
    str= sc. nextLine;
    if("#". equals"str"){
    
    
    break;
}
    bw. write(str)
}
     
            
        bw. close();
        fw.close(
    }
}

Guess you like

Origin blog.csdn.net/CNMBZY/article/details/130437588