JAVA啊啊啊啊啊啊啊啊啊

编写一个游戏程序,由程序随机生成一个4位数,然后用户输入一个4位数。如果输入的数
和随机生成的数由两个数字相同,则输出 获得三等奖;如果有三个数字相同,则 输出
获得二等奖;如果4个数都相同,则输出 获得一等奖

可以把数字转为字符串进行解析。也可以进行计算的答案。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test2 {

public static void main(String[] args) {
    int random = (int) (Math.random() * 9000 + 1000);
    System.out.print("Random:");
    System.out.println(random);
    System.out.print("Input:");
    Scanner sc = new Scanner(System.in);
    int input = sc.nextInt();

    List<Integer> list1 = new ArrayList<Integer>();
    List<Integer> list2 = new ArrayList<Integer>();
    list1=getDigital(random);
    list2=getDigital(input);
    int prizeKind=prize(list1,list2);
    System.out.print("奖项:");
    if(prizeKind!=0)
        System.out.println(prizeKind+"等奖");
    else
        System.out.println("无");

}

/**
 * 得到四位数字的每一位,并且放在List<String>中
 * 
 * @param num
 * @return
 */
public static List<Integer> getDigital(int num) {
    List<Integer> ls = new ArrayList<Integer>();
    int n;
    while (num != 0) {
        n = num % 10;
        ls.add(n);
        num /= 10;
    }
    return ls;
}

public static int prize(List<Integer> list1, List<Integer> list2) {
    int sum = 0;
    for (int i = 0; i < list1.size(); i++)
        for (int j = 0; j < list2.size(); j++) {
            if (list1.get(i)==list2.get(j)){
                list2.remove(j);
                sum++;  
                break;
            }
        }
    if (sum == 2)
        return 3;
    else if (sum == 3)
        return 2;
    else if (sum == 4)
        return 1;
    else
        return 0;
}

}

/**
*功能:编写一个游戏程序,由程序随机生成一个4位数,然后用户输入一个4位数。如果输入的数
和随机生成的数由两个数字相同,则输出 获得三等奖;如果有三个数字相同,则 输出
获得二等奖;如果4个数都相同,则输出 获得一等奖
*作者:johnny2016
*日期:2017-03-25 10:34
*备注:看到前面两位高手的回答,汗颜,思路都比我好。学习了
*/

import java.util.*;

public class RandomTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //用于接受键盘输入,以回车键结束
sc.useDelimiter("\n");
String strIn = sc.next();

    if(strIn.length() !=5)       //判断输入的是否是四位数,不是的话提示,退出(因为还有一个回车符)
    {
        System.out.println("数据的长度必须是四位!");
        System.exit(0); 
    }

    int minNum = 1000;           //设定随机数范围
    int maxNum = 9999;

    Random  random = new    Random();   //得到随机数,并将随机数转为字符串
    int intRan = random.nextInt(maxNum)%(maxNum-minNum+1)+minNum;   
    String  strRan = Integer.toString(intRan);

    System.out.println("随机数:"+strRan);

    char[]  cIn = strIn.toCharArray();    //将输入的四位数和随机产生的四位数转成字符数组,并进行比较
    char[]  cRan = strRan.toCharArray();

    int sum = 0;
    for(int i=0; i<4; i++)
    {
        if(cIn[i] == cRan[i])
        {
            sum++;  
        }   
    }   
    switch(sum)   //根据得到的比较结果,判断
    {
        case 2:
        {
            System.out.println("三等奖");
            break;  
        }   
        case 3:
        {
            System.out.println("二等奖");
            break;  
        }
        case 4:
        {
            System.out.println("一等奖");
            break;  
        }
        default:
        {
            System.out.println("您没有中奖!"); 
        }
    }
}

}