Java课堂作业第三周

#Java课堂作业第三周
猜数字游戏,猜数字是一种古老的密码破译类益智类小游戏,起源于20世纪中期,一般由两个人或多个人玩,也可以由一个人和电脑玩。
计算机随机产生一个数字,默认为没有重复数字的4位数字,你来猜,每猜一个数字,计算机反馈nAmB.其中A前面的数字n表示数字正确且位置也正确的数字个数,B前面的数字M表示数字正确但位置不正确的数字个数,持续猜下去,直至猜中或超过指定次数为止。
在这里插入图片描述
复杂版本

package ch03;

import java.util.Scanner;
import java.util.Random;
public class a {
    public static void main(String[] args) {
        int a = 7391;
        boolean Flag = true;
        int j = 1;
        int input, a1, a2, a3, a4;
        int b1, b2, b3, b4;
        Scanner scanner = new Scanner(System.in);
        Random rand = new Random();
        a = rand.nextInt(1000) + 9000;
        b4 = a % 10;
        b3 = a / 10 % 10;
        b2 = a / 100 % 10;
        b1 = a / 1000 % 10;
        boolean flag = true;
        while (flag) {
            a = rand.nextInt(1000) + 1000;
            b4 = a % 10;
            b3 = a / 10 % 10;
            b2 = a / 100 % 10;
            b1 = a / 1000 % 10;
            if ((b1 != b2) & (b1 != b3) & (b1 != b4) & (b2 != b3) & (b2 != b4) & (b3 != b4))
                flag = false;
            //System.out.print(flag);
        }
        System.out.print(a);
        while (Flag) {
            System.out.print("请输入你猜的数字:");
            input = scanner.nextInt();

            int m = 0, n = 0;
            String output;

            a4 = input % 10;
            a3 = input / 10 % 10;
            a2 = input / 100 % 10;
            a1 = input / 1000 % 10;
            if(a1==0)
            {   System.out.println("输入字符不合格式");
                System.out.print("请输入你猜的数字:");
                input = scanner.nextInt();
                a4 = input % 10;
                a3 = input / 10 % 10;
                a2 = input / 100 % 10;
                a1 = input / 1000 % 10;
            }
            if (b1 == a1)
                m++;
            if (b2 == a2)
                m++;
            if (b3 == a3)
                m++;
            if (b4 == a4)
                m++;
            //System.out.println(m);
            if (((b1 == a2) || (b1 == a3) || (b1 == a4) & (b1 != a1)))
                n++;
            if (((b2 == a1) || (b2 == a3) || (b2 == a4) & (b2 != a2)))
                n++;
            if (((b3 == a1) || (b3 == a2) || (b3 == a4) & (b3 != a3)))
                n++;
            if (((b4 == a1) || (b4 == a2) || (b4 == a3) & (b4 != a4)))
                n++;
            output = m + "A" + n + "B";
            //System.out.println(output);
            if ((m == 4) & (n == 0)) {

                System.out.println(output);
                System.out.println("你很厉害呀!");
                Flag = false;
            } else {
                System.out.println(j + ": " + output);
                j++;
            }
        }
    }
}

简化版本

package ch03;

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
public class a {
    public static void main(String[] args) {
        String s=suijishu();
        System.out.println("产生的随机数是"+s);
        //System.out.print("请输入你猜的数字:");
        Scanner scanner = new Scanner(System.in);
        String input ="1234";
        int j=0;
        boolean flag=true;
        if(hefa(input)){
            while(flag){
            System.out.print("请输入你猜的数字:");
            input = scanner.nextLine();
            j+=1;
            flag=panduan(s,input,j);
            }
        }
    }
    public static String suijishu(){
        Random rand = new Random();
        String s=Integer.toString(rand.nextInt(9)+1);
        //System.out.println(s);
        int i=0;
        while(i<3){
            int a=rand.nextInt(10);
            if(!(s.contains(Integer.toString(a)))){
                s=s+Integer.toString(a);
                i++;
            }
        }
        return s;
    }
    public static boolean hefa(String s){
        if(s.length()!=4)
            return false;
        if((Integer.valueOf(s)<0) || (Integer.valueOf(s)>9999))
            return false;
        if(s.charAt(0)=='0')
            return false;
        if(Integer.valueOf(s)%1111==0)
            return false;
        else
            return true;
    }
    public static boolean panduan(String s1,String s2,int j){
        int m=0,n=0;
        if (s1.charAt(0) == s2.charAt(0))
            m++;
        if (s1.charAt(1) == s2.charAt(1))
            m++;
        if (s1.charAt(2) == s2.charAt(2))
            m++;
        if (s1.charAt(3) == s2.charAt(3))
            m++;
        //System.out.println(m);
        if (((s2.charAt(0) == s1.charAt(1)) || (s2.charAt(0) == s1.charAt(2)) || (s2.charAt(0) == s1.charAt(3)) & (s2.charAt(0) != s1.charAt(0))))
            n++;
        if (((s2.charAt(1) == s1.charAt(0)) || (s2.charAt(1) == s1.charAt(2) || (s2.charAt(1) == s1.charAt(3)) & (s2.charAt(1) != s1.charAt(1)))))
            n++;
        if (((s2.charAt(2) == s1.charAt(0)) || (s2.charAt(2) == s1.charAt(1)) || (s2.charAt(2) == s1.charAt(3)) & (s2.charAt(2) != s1.charAt(2))))
            n++;
        if (((s2.charAt(3) == s1.charAt(0)) || (s2.charAt(3) == s1.charAt(1)) || (s2.charAt(3) == s1.charAt(2)) & (s2.charAt(3) != s1.charAt(3))))
            n++;

        String output = m + "A" + n + "B";
        //System.out.println(output);
        if ((m == 4) & (n == 0)) {
            System.out.println(output);
            System.out.println("你很厉害呀!");
            return false;
        }
        else {
            System.out.println(j + ": " + output);
            return true;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值