JAVA基础实战练习

5 篇文章 0 订阅

 1.卖飞机票

 

import java.util.Scanner;

public class airplane {
    public static void main(String[] args) {
//        键盘录入三个信息
        Scanner sc = new Scanner(System.in);
        System.out.println("输入机票原价");
        int ticket = sc.nextInt();
        System.out.println("输入月份");
        int month = sc.nextInt();
        System.out.println("输入0 则为头等舱 1 则为经济舱");
        int type = sc.nextInt();
//        判断旺季还是淡季
        if (month>4&&month<11){
            System.out.println("旺季");
//            判断是头等舱还是经济舱
            if (type==0){
                double i  =ticket*0.9;
                System.out.println("头等舱票价为"+i);
            }else if (type==1){
                double j = ticket*0.85;
                System.out.println("经济舱票价为"+j);
            }else{
                System.out.println("没有这个座位");
            }
        }else if (month>10&&month<5){
            if (type == 0) {
                double i = ticket * 0.7;
                System.out.println("头等舱票价为" + i);
            } else if (type==1){
                double j = ticket * 0.65;
                System.out.println("经济舱票价为" + j);
            }else{
                System.out.println("没有这个座位");
            }
        }else{
            System.out.println("月份出错");
        }

    }

}

2 判断101-200中间的质数 并计算出质数数量

import java.util.Scanner;

public class text2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入最终数字");
        int i = sc.nextInt();
        int count = 0;

        for (int i1 = 101; i1 < i; i1++) {
//            单纯判断一个数字是否为质数
            boolean flag = true;
            for (int j = 2; j < i1; j++) {
                if (i1 % j == 0){
                    flag=false;
//                    跳出单层循环
                    break;
                }
            }
            if (flag){
                System.out.println(i1+"为质数");
                count++;
            }else{
                System.out.println(i1+"不是质数");
            }
        }
        System.out.println("质数数量"+count);
    }

}

3 长度为5的验证码 

import java.util.Random;

public class 验证码 {
    public static void main(String[] args) {
//        把大小写字母放入数组中
        char[] chs = new char[52];
        for (int i = 0; i < chs.length; i++) {
            if (i <= 25) {
                chs[i] = (char) (97 + i);
            } else {
//                i从26开始,减26确保是从大写A开始
                chs[i] = (char) (65 + i - 26);
            }
        }
//        定义一个字符串变量 把内容拼接上去
        String result = "";
        Random r = new Random();
//先随机索引一次 然后在随机索引四次
        for (int i = 0; i < 4; i++) {
            int j = r.nextInt(chs.length);
            result = result + chs[j];
        }
//        随机生成数字
        int n = r.nextInt(10);
//        拼接字符和数字
        result = result + n;
        System.out.println(result);
    }
}

4 复制数组

public class 复制数组 {
    public static void main(String[] args) {
//        创建新老数组 把新数组存入老数组并且打印
        int [] arr1={1,2,3};
        int [] arr2= new int[arr1.length];
        for (int i = 0; i < arr1.length; i++) {
        arr2[i]=arr1[i];
        }
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
    }
}

        5 数字加密

import com.sun.imageio.plugins.common.I18N;

import java.util.Scanner;

public class mathscreat {
    public static void main(String[] args) {
        int [] arr=new int[4];
        Scanner sc = new Scanner(System.in);
//自由输入数字,进行密码加密
        for (int i1 = 0; i1 < 4; i1++) {
            System.out.println("输入密码");
            int i= sc.nextInt();
            arr[i1]=i;
        }
        //加五
        for (int i = 0; i < arr.length; i++) {
            arr[i]=arr[i]+5;
        }
        //取余
        for (int i = 0; i < arr.length; i++) {
            arr[i]=arr[i]%10;
        }
//        反转
        int temp=0;
        int j=arr.length-1;
        for (int i = 0; i < arr.length; i++) {
            if (i<j){
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
//        打印
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

    }
}

6 抢红包

import java.util.Random;

public class 抽奖 {
    public static void main(String[] args) {
        int []arr1 = {5,588,2888,1000,10000};
        int []arr2 = new int[arr1.length];
        Random r = new Random();
        for (int i = 0; i < 5; ) {
//            随机获得数字
            int idex= r.nextInt(arr1.length);
//            获得数组中对应值
            int price= arr1[idex];
//            对比奖金和arr2(已经被抽走的奖)中有无重复
            boolean flag = campare(arr2,price);
//            true则已经被抽走了 所以要false没有被抽中
//            为什么flag是true呢??要是第一个抽arr2里面一个数字也没有啊.flag为false啊
            if(!flag){
                arr2[i]=price;
                i++;
            }
        }
        for (int i = 0; i < arr2.length; i++) {
            System.out.println("获得奖金为"+ arr2[i]);
        }
    }
//    判断奖项是否存在 存在则重新抽取 不存在则中奖了
    public static boolean campare(int [] arr2, int price){
        for (int i = 0; i < arr2.length; i++) {
         if (arr2[i]==price){
             return true;
         }
        }
        return false;
    }
}
import java.util.Random;
//        随机生成数字 和数组本身进行交换 达到打乱的目的 
public class 抽奖必中版 {
    public static void main(String[] args) {
        int [] arr ={2,2000,21000,100000,588};
        Random r= new Random();
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            int idex = r.nextInt(arr.length);
            temp = arr[i];
            arr[i]=arr[idex];
            arr[idex]=temp;

        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println("抽中的奖项是"+arr[i]);
        }
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值