A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 潘耿威 中级黑马   /  2016-3-25 20:31  /  375 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.Random;
import java.util.Scanner;

public class Demo_01 {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Random r = new Random();
                Scanner sc = new Scanner(System.in);
                int temp = r.nextInt(900) + 100;
                int a = temp / 10 / 10 % 10;// 百位
                int b = temp / 10 % 10;// 十位
                int c = temp % 10;
                boolean flag = true;
                int index;
                System.out.println(temp);
                while (flag) {
                        System.out.println("请猜数字(100~1000):");
                        index = sc.nextInt();
                        if (temp == index) {
                                System.out.println("猜对了--I 服了--YOU--");
                                flag = false;
                        } else {
                                int x = index / 10 / 10 % 10;// 百位
                                int y = index / 10 % 10;// 十位
                                int z = index % 10;
                                int id = 0;
                                int idd = 0;
                                if (y == b) {
                                        id++;
                                }
                                if (x == a) {
                                        id++;
                                }
                                if (z == c) {
                                        id++;
                                }
                                if (a == x) {
                                        idd++;
                                }
                                if (a == y) {
                                        idd++;
                                }
                                if (a == z) {
                                        idd++;
                                }
                                if (b == x) {
                                        idd++;
                                }
                                if (b == y) {
                                        idd++;
                                }
                                if (b == z) {
                                        idd++;
                                }
                                if (c == x) {
                                        idd++;
                                }
                                if (c == y) {
                                        idd++;
                                }
                                if (c == z) {
                                        idd++;
                                }
                                System.out.println("猜对" + id + "个位置\n猜对:" + idd + "个数");
                        }
                }

        }

}


5 个回复

倒序浏览
学习了学习了
回复 使用道具 举报
来点注释啊,大胸弟
回复 使用道具 举报
有意思~
回复 使用道具 举报
满足条件id++部分可以合并的
回复 使用道具 举报
unexpectedman 发表于 2016-3-25 21:53
满足条件id++部分可以合并的

合并了就不是一个意思了,他只会j一次 idd 也只会加一次
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马