栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础练习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java基础练习题

目录

一、数组复制

二、评委打分

三、将数组加密

四、找到素数

五、计算机票价格

六、随机生成n位数的验证码


一、数组复制
package com.test.hello.method;

public class copyArr {

    

    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8,9};
        int[] arr2 = new int[arr1.length];
        coprArrary(arr1,arr2);
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + "t");
        }
    }
    public static void coprArrary(int[] arr1,int[] arr2){
        for (int i = 0; i < arr1.length; i++) {
            arr2[i] = arr1[i];
        }
    }
}

二、评委打分
package com.test.hello.method;

import java.util.Scanner;

public class dafen{

    

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("有几个要打分:请输入:");
        int n = sc.nextInt();
        System.out.println("最终分数是:" + compute(creat(n)));
    }

    public static int[] creat(int n){
        System.out.println("请依次输入分数");
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i+1) + "个评委的分数");
            arr[i] = sc.nextInt();
        }
        return arr;
    }
    public static double compute(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        sum -= (find_max(arr) + find_min(arr));
        return sum * 1.0 / (arr.length - 2);
    }
    public static int find_max(int[] arr){
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]){
                max = arr[i];
            }
        }
        return max;
    }
    public static int find_min(int[] arr){
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (min > arr[i]){
                min = arr[i];
            }
        }
        return min;
    }
}

三、将数组加密
package com.test.hello.method;

public class jiami {

    
    public static void main(String[] args) {

        int[] arr = {1,9,8,3};
        for (int i = 0; i < arr.length; i++) {
           arr[i] = (arr[i] + 5) % 10;
        }
        for (int i = 0,j = arr.length-1; i < j; i++,j--) {
            int temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }
        for (int k = 0; k < arr.length; k++) {
            System.out.println(arr[k]);
        }
    }
}

四、找到素数
package com.test.hello.method;

public class sushu {

    
    public static void main(String[] args) {
        find_sushu();
    }

    private static void find_sushu() {
        for (int i = 100; i <= 200; i++) {
            boolean flag = true;
            for (int j = 2; j < (i / 2); j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + "t");
            }
        }
    }
}

五、计算机票价格
package com.test.hello.method;

import java.util.Scanner;

public class ticket_feiji {

    

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入购买机票的原价:");
        double money = scanner.nextDouble();
        System.out.println("请输入买机票的月份:");
        int month = scanner.nextInt();
        System.out.println("请输入舱位类型:");
        String type = scanner.next();
        System.out.println("您的最终机票钱是:" + calc(money,month,type));
    }

    public static double calc(double money, int month, String type){
        if (month >= 5 && month <= 12){
            if ("经济舱".equals(type)) {
                money *= 0.9;
            } else if ("头等舱".equals(type)) {
                money *= 0.85;
            } else {
                System.out.println("输入无效舱类型");
                return -1;
            }
        }
        else if (month >=1 && month <= 4 || money == 11 || money == 12){
            if ("经济舱".equals(type)) {
                money *= 0.65;
            } else if ("头等舱".equals(type)) {
                money *= 0.7;
            } else {
                System.out.println("输入无效舱类型无效");
            }
        }
        else {
            System.out.println("您输入的月份无效");
            money = -1;
        }
        return money;
    }
}

六、随机生成n位数的验证码
package com.test.hello.method;

import java.util.Random;

public class yanzhengma {
    

    public static void main(String[] args) {
        String str = createCode(3);
        System.out.println(str);

    }
    public static String createCode(int n){
        Random r = new Random();
        String str = "";
        for (int i = 0; i < n; i++) {
            int type = r.nextInt(3);
            switch (type){
                case 0 :
                    char ch = (char)(r.nextInt(25) + 65);
                    str += ch;
                    break;
                case 1 :
                    char ch1 = (char)(r.nextInt(25) + 97);
                    str += ch1;
                    break;
                case 2 :
                    int num = r.nextInt(10);
                    str += num;
                    break;
            }
        }
        return str;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786140.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号