zoukankan      html  css  js  c++  java
  • java数组实现红包的方法

     1 package Hongbao;
     2 
     3 import java.text.DecimalFormat;
     4 import java.util.Scanner;
     5 
     6 public class Hongbao {
     7     public static void main(String[] args) {
     8         Scanner sc=new Scanner(System.in);
     9         
    10         System.out.println("请输入红包金额:");
    11         double money=sc.nextDouble();
    12         
    13         System.out.println("请输入红包个数:");
    14         int count=sc.nextInt();
    15         
    16         //有序数组1到99
    17         int[] num=new int[100];
    18         for (int i = 1; i < 100; i++) {
    19             num[i-1]=i;
    20         }
    21         
    22         //有序数组打乱
    23         for (int i = 0; i < num.length; i++) {
    24             int ran=(int)(Math.random()*100);
    25             int temp=num[i];
    26             num[i]=num[ran];
    27             num[ran]=temp;
    28         }
    29         
    30         //将分配的红包个数赋给新数组
    31         int[] acount=new int[count];
    32         for (int i = 0; i < count; i++) {
    33             acount[i]=num[i]; 
    34         }
    35         //冒泡排序
    36         for (int i = 0; i < acount.length; i++) {
    37             for (int j = 0; j < acount.length-1-i; j++) {
    38                 if(acount[j]>acount[j+1]){
    39                   int temp=acount[j];
    40                   acount[j]=acount[j+1];
    41                   acount[j+1]=temp;
    42                 }
    43             } 
    44         }
    45             
    46         //红包分钱算法
    47         int[] mon=new int[count];
    48         for (int i = 0; i < count; i++) {
    49             if(i==0){
    50                 mon[0]=acount[0];
    51             }else if(i>0&&i<count-1){
    52                 mon[i]=acount[i]-acount[i-1];
    53             }else{
    54                 mon[i]=100-acount[i-1];
    55             }
    56         }
    57 
    58         //带钱
    59         DecimalFormat df=new DecimalFormat("####0.00");//保留两位小数
    60         for (int i = 0; i < mon.length; i++) {
    61             System.out.println("第"+(i+1)+"名,领取"+df.format(mon[i]*0.01*money)+"元红包");
    62         }
    63         
    64     }
    65 }
  • 相关阅读:
    about java
    nginx+tomcat
    sed
    百度推送
    线程及更新ui线程的简单理解
    测试异常检测的Bugly使用
    轮播图带加点,带时间自动轮播加手动轮播
    自定义listView与scrollView使用
    tabLayout加viewPager的实现
    网络获取图片列表
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/5869055.html
Copyright © 2011-2022 走看看