单人猜数游戏java

 

 //这是一个以1到10为例的单人猜数游戏

public static void main(String[] args) {
        int b=(int)(Math.random()*10+1);//b是一个1到10的随机数
        //System.err.println(b);
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入1到10以内的数字:");
        int a=sc.nextInt();//a是用户猜测的数字
        int i = 1;//i是用户猜测次数
        int sum=0;
        while(a!=b & i<10){
        System.out.println(a>b?"很抱歉:大了":"很抱歉:小了");
        System.out.println("请再次输入猜测数字:");
        a=sc.nextInt();
        i++;
        sum++;
        }
        String s ="";
        if (i<3){
        s="你猜对了!";
        }else if(i<6) {
        s="可算猜对了";
        }else s="9次都猜不到 你没谁了";
        System.out.println("恭喜你猜数正确!"+"答案是:"+b+".您共记猜测了"+i+"次。"
        +"系统对您的评价是:"+s);
    }
原文地址:https://www.cnblogs.com/tytr/p/5652977.html