欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java练习题五【数组】

程序员文章站 2022-07-12 08:13:05
...

目录

 

练习题53.计算数字的出现次数

练习题54.打印不同的数

练习题55.是否排好序了

练习题56.游戏:豆机

练习题57.完全相同的数组

练习题58.模式识别:四个连续相等的数

练习题59.合并两个有序列表

练习题60.游戏:猜字游戏


练习题53.计算数字的出现次数

Java练习题五【数组】

import java.util.Scanner;
class Demo53{
    public static void main(String[] args){
        int[] arr=new int[101];
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter numbers:");
        while(true){
            int num=scanner.nextInt();
            if(num==0){
                break;
            }
            arr[num]++;
        }
        for(int i=0;i<arr.length;i++){
            if(arr[i]!=0){
                System.out.println(i+" occurs "+arr[i]+(arr[i]>1?" times":" time"));
            }
        }
    }
}

练习题54.打印不同的数

Java练习题五【数组】

import java.util.*;
class Demo54{
    public static void main(String[] args){
        int[] arr=new int[0];
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter numbers:");
        for(int i=0;i<10;i++){
            int num=scanner.nextInt();
            if(!contains(arr,num)){
                arr=copyOf(arr,arr.length+1);
                arr[arr.length-1]=num;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
    public static boolean contains(int[] arr,int key){
        for(int i=0;i<arr.length;i++){
            if(arr[i]==key){
                return true;
            }
        }
        return false;
    }
    public static int[] copyOf(int[] arr,int newLen){
        int[] newArr=new int[newLen];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        return newArr;
    }
}

练习题55.是否排好序了

Java练习题五【数组】

import java.util.*;
class Demo55{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter a list:");
        int len=scanner.nextInt();
        int[] arr=new int[len];
        for(int i=0;i<arr.length;i++){
            arr[i]=scanner.nextInt();
        }
        if(isSorted(arr)){
            System.out.println("The list is already sorted.");
        }else{
            System.out.println("The list is not sorted.");
        }
    }
    public static boolean isSorted(int[] list){
        for(int i=1;i<list.length;i++){
            if(list[i-1]>list[i]){
                return false;
            }
        }
        return true;
    }
}

练习题56.游戏:豆机

Java练习题五【数组】

Java练习题五【数组】

import java.util.*;
class Demo56{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter the number of balls to drop:");
        int balls=scanner.nextInt();
        System.out.print("Enter the number of slots in the bean machine:");
        int slots=scanner.nextInt();
        int[] arr=new int[slots];
        for(int i=0;i<balls;i++){
            String path=getPath(slots);
            System.out.println(path);
            arr[getR(path)]++;
        }
        System.out.println(Arrays.toString(arr));
        show(arr);
    }
    public static void show(int[] arr){
        int w=arr.length;
        int h=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]>h){
                h=arr[i];
            }
        }
        for(int i=h-1;i>=0;i--){
            for(int j=0;j<w;j++){
                if(i<arr[j]){
                    System.out.print("O");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }
    public static int getR(String path){
        int count=0;
        for(int i=0;i<path.length();i++){
            if(path.charAt(i)=='R'){
                count++;
            }
        }
        return count;
    }
    public static String getPath(int slots){
        Random random=new Random();
        String path="";
        for(int j=0;j<slots-1;j++){
            if(random.nextInt(2)==0){
                path+="L";
            }else{
                path+="R";
            }
        }
        return path;
    }
}

练习题57.完全相同的数组

Java练习题五【数组】

class Demo57{
    public static void main(String[] args){
        int[] list1={1,2,3,4,5,6,7};
        int[] list2={1,2,3,4,5,7,6};
        System.out.println(equals(list1,list2));
    }
    public static boolean equals(int[] list1,int[] list2){
        if(list1.length!=list2.length){
            return false;
        }
        for(int i=0;i<list1.length;i++){
            if(list1[i]!=list2[i]){
                return false;
            }
        }
        return true;
    }
}

练习题58.模式识别:四个连续相等的数

Java练习题五【数组】

class Demo58{
    public static void main(String[] args){
        int[] arr={1,1,1,1,2,2,2,2,2,3,3,3,3,3,4};
        for(int i=0;i<arr.length;){
            int count=1;
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]==arr[j]){
                    count++;
                }else{
                    break;
                }
            }
            if(count>=4){
                System.out.println(arr[i]);
                return;
            }
            i+=count;
        }
        System.out.println("没有!");
    }
}

练习题59.合并两个有序列表

Java练习题五【数组】

import java.util.*;
class Demo59{
    public static void main(String[] args){
        int[] list1={1,3,5,7,9};
        int[] list2={2,4,6,8,10};
        System.out.println(Arrays.toString(merge(list1,list2)));
    }
    public static int[] merge(int[] list1,int[] list2){
        if(list1==null&&list2==null){
            return null;
        }
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        int[] list3=new int[list1.length+list2.length];
        int p1=0;
        int p2=0;
        int p3=0;
        while(true){
            if(p1==list1.length&&p2==list2.length){
                break;
            }
            if(p1<list1.length&&p2==list2.length){
                list3[p3++]=list1[p1++];
            }else if(p1==list1.length&&p2<list2.length){
                list3[p3++]=list2[p2++];
            }else{
                if(list1[p1]<=list2[p2]){
                    list3[p3++]=list1[p1++];
                }else{
                    list3[p3++]=list2[p2++];
                }
            }
        }
        return list3;
    }
}

练习题60.游戏:猜字游戏

Java练习题五【数组】

import java.util.*;
class Demo60{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        String[] words={"naruto","kakashi","sasuke","banana","java","program"};
        while(true){
            String word=words[random.nextInt(words.length)];
            boolean[] status=new boolean[word.length()];
            int miss=0;
            while(true){
                String ciphertext=getCipherText(word,status);
                System.out.print("Enter a letter in word "+ciphertext+" >");
                char letter=scanner.nextLine().charAt(0);
                if(isContainsLetter(word,letter)){
                    if(!changeWordStatus(word,status,letter)){
                        System.out.println("\t "+letter+" is already in the word");
                    }
                }else{
                    System.out.println("\t "+letter+" is not in the word");
                    miss++;
                }
                if(isFinish(status)){
                    System.out.println("The word is "+word+". You miss "+miss+" time");
                    break;
                }
            }
            System.out.print("Do you want to guess another word?Enter y or n:");
            String choice=scanner.nextLine();
            if(choice.equals("n")){
                System.out.println("Welcome!Thank you! FUCK PROGRAM!");
                break;
            }
        }
    }
    public static boolean isFinish(boolean[] status){
        for(int i=0;i<status.length;i++){
            if(!status[i]){
                return false;
            }
        }
        return true;
    }
    public static boolean changeWordStatus(String word,boolean[] status,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                if(status[i]){
                    return false;
                }else{
                    status[i]=true;
                }
            }
        }
        return true;
    }
    public static boolean isContainsLetter(String word,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                return true;
            }
        }
        return false;
    }
    public static String getCipherText(String word,boolean[] status){
        String ciphertext="";
        for(int i=0;i<status.length;i++){
            if(status[i]){
                ciphertext+=word.charAt(i);
            }else{
                ciphertext+="*";
            }
        }
        return ciphertext;
    }
}

 

相关标签: Java练习题