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

对上一篇文章: java求助的一点改进,但是还是存在一个很严重的错误,继续求助????

2013-04-06 16:05 281 查看
这个问题就是: 当随机产生数: source产生的是一个有两位数字相同的时候,若此时输入的数字只有一个和它相同,但是位置是末尾:

例如: source = 434;test = 564;结果应该是四等奖, 但是却是三等奖???该怎么处理???

程序源代码:

package Testforchapter3;

import javax.swing.JOptionPane;

public class Exercise11 {

public static void main(String[] args) {

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

System.out.println("随机数字的值是: " + source);

int[] s = new int[3];

s[0] = source % 10;

s[1] = source / 10 % 10;

s[2] = source / 100;

int test = Integer.parseInt(JOptionPane.showInputDialog("输入一个三位数字"));

int[] t = new int[3];

t[0] = test % 10;

t[1] = test / 10 % 10;

t[2] = test / 100;

if (source == test) {

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

} else {

int num = 0;

for (int i = 0; i < 3; ++i) {

for (int j = 0; j < 3; j++) {

if (s[i] == t[j]) {

num += 1;

break;

} else {

continue;

}

}

}

switch (num) {

case 3:

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

break;

case 2:

JOptionPane.showMessageDialog(null, "三等奖, 奖金1000美金!!!");

break;

case 1:

JOptionPane.showMessageDialog(null, "四等奖, 奖金100美金!!!");

break;

case 0:

JOptionPane.showMessageDialog(null, "你的好运快到了,再买一张吧!");

break;

}

}

}

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