栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA:猜数字游戏!

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA:猜数字游戏!

问题:系统随机生成一个1-100的数,猜错进行提示,直到猜对为止,并记录猜对所用的次数!

代码:

package day;

import java.util.Scanner;
import java.util.Random;
public class Day0401 {
public static void main(String[] args) {
	// TODO Auto-generated method stub
	Random random = new Random();
	int guess1 = 10 + random.nextInt(100);
	System.out.println("guess1 = " + guess1);
	int guess2 = 10 + (int)(Math.random()*91);
	System.out.println("guess2 = " + guess2);
	Scanner sc = new Scanner(System.in);
	int count = 0;
	while((guess1>0)&&(guess1<100)){
		System.out.println("please input a number:"); 
		int a = sc.nextInt();
	    count += 1;
	    if(guess1 < a) {
	    	System.out.println("猜大了!");
	    }else if(guess1 > a) {
	    	System.out.println("猜小了!");
	    }else {
	    	System.out.println("猜对了!");
	    	break;
	    }
	}
	System.out.println("你猜对一共用的次数是:"+ count);  
}

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/270148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号