您的位置:首页 > 编程语言 > Java开发

求助: Java基础问题求助: 如何求三个数字中只有一个相同??????

2013-04-06 14:57 489 查看
这里是源程序: 请帮帮忙,给该支出该从哪里思考????

/**游戏:彩票

* 描述:产生三位整数的彩票。 程序提示用户输入一个三位整数,

* 然后依照下面的规则判定用户是否赢得奖金:

* 1. 如果用户输入的所有数匹配彩票的确切顺序,奖金是10 000美金;

* 2. 如果用户输入的所有数匹配彩票的所有数字,奖金是3000美金;

* 3. 如果用户输入的其中一个数匹配彩票号码中的一个数,奖金是1000美金。

*

* 程序分析:

* 1. 使用取余和除法的方法,把三位整数进行分解;[彩票和用户的猜测]

* 2. 输入的所有数匹配彩票的所有数字有多少可能???怎么编程实现这些可能

* 3. 只有一个数匹配有多少可能?? 怎么进行编程实现

*/

package Testforchapter3;

import javax.swing.JOptionPane;

public class Exercise1 {

public static void main(String[] args) {

// Create a three bits number call lottery

int lottery = (int) (100 + Math.random() * 900);

System.out.println("The value of the lottery is: " +lottery);

// the range of the value is 100 to 999

int[] lotteryArray = resolveData(lottery);

// Prompt the user to enter his guess

int guess = Integer

.parseInt(JOptionPane

.showInputDialog("Enter your guess for the lottery[100 - 999]"));

int[] guessArray = resolveData(guess);

// Call the method for result

rewardRank(lotteryArray, guessArray);

}

// resolve the digit to three number

public static int[] resolveData(int data) {

int[] singleNumber = new int[3];

singleNumber[0] = data % 10;

singleNumber[1] = data / 10 % 10;

singleNumber[2] = data / 100;

return singleNumber;

}

// judge the reward from two arrays

public static void rewardRank(int[] lotteryCode, int[] guessCode) {

// 判断一等奖

if (lotteryCode[0] == guessCode[0] && lotteryCode[1] == guessCode[1]

&& lotteryCode[2] == guessCode[2]) {

JOptionPane.showMessageDialog(null, "恭喜你!一等奖, 奖金10 000美金!!!");

} else {

// 判断二等奖

boolean b1 = lotteryCode[0] == guessCode[0]

&& lotteryCode[1] == guessCode[2]

&& lotteryCode[2] == guessCode[1];

boolean b2 = lotteryCode[0] == guessCode[1]

&& lotteryCode[1] == guessCode[0]

&& lotteryCode[2] == guessCode[2];

boolean b3 = lotteryCode[0] == guessCode[1]

&& lotteryCode[1] == guessCode[2]

&& lotteryCode[2] == guessCode[0];

boolean b4 = lotteryCode[0] == guessCode[2]

&& lotteryCode[1] == guessCode[1]

&& lotteryCode[2] == guessCode[0];

boolean b5 = lotteryCode[0] == guessCode[2]

&& lotteryCode[1] == guessCode[0]

&& lotteryCode[2] == guessCode[1];

if (b1 || b2 || b3 || b4 || b5) {

JOptionPane.showMessageDialog(null, "恭喜你,二等奖,奖金3000美金!!!");

} else {

boolean a1 = lotteryCode[0] != guessCode[0] && lotteryCode[0] != guessCode[1] && lotteryCode[0] != guessCode[2];

boolean a2 = lotteryCode[1] != guessCode[0] && lotteryCode[1] != guessCode[1] && lotteryCode[1] != guessCode[2];

boolean a3 = lotteryCode[2] != guessCode[0] && lotteryCode[2] != guessCode[1] && lotteryCode[2] != guessCode[2];

if (a1 && a2 && a3) {

JOptionPane.showMessageDialog(null, "好运气不远了,继续努力!!!");

}

}

}

// 判断三等奖 这个地方不知道该这么写??? 求助?????

boolean c1 = lotteryCode[0] == guessCode[0];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐