数字猜大小,并统计直到猜对时输入的次数

package com.example.practice;

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

public class Test10 {
    public static void main(String[] args) {
        Random random = new Random();
        int i = random.nextInt(100);
        System.out.println(i);
        Scanner scanner = new Scanner(System.in);
        int count = 0;
        while (true) {
            count++;
            System.out.println("请输入你要猜的数字:");
            int guess = scanner.nextInt();
            if (guess > i) {
                System.out.println("猜大了");
            } else if (guess < i) {
                System.out.println("猜小了");
            } else {
                System.out.println("猜对了,总共猜了"+count+"次");
                break;
            }
        }
    }

}

posted @ 2023-03-15 10:49  竹狼  阅读(58)  评论(0编辑  收藏  举报
<style id="ceshicss"> @media (max-width: 767px){ #set_btn_box {width: 100vw;left: 0;right: 0;bottom: 0;background: hsla(0,0%,100%,.6);height: 49px;display: flex;justify-content: space-between;align-items: center;padding: 12px 40px;border-top: 1px solid #e8e8e8;box-sizing: border-box;} .set_btn {margin-top: 0;} .set_btn.fly_top.fadeIn.animated {position: absolute;right: 10px;bottom: 60px;} .container{bottom:50px}} #mainContent{width:90%} </style> <link href="https://blog-static.cnblogs.com/files/miluluyo/tippy.min.css" rel="stylesheet"> <script src="https://unpkg.com/@popperjs/core@2.4.2/dist/umd/popper.min.js"></script> <script src="https://blog-static.cnblogs.com/files/miluluyo/tippy.js"></script> <link rel='stylesheet' href='https://cdn.bootcss.com/animate.css/3.7.2/animate.min.css'> <script src="https://at.alicdn.com/t/font_1825850_klax1ao4o6.js"></script> <script src="https://blog-static.cnblogs.com/files/miluluyo/three.min.js"></script> <script src='https://blog-static.cnblogs.com/files/miluluyo/star.js'></script> <link rel="stylesheet" href="https://blog-static.cnblogs.com/files/miluluyo/OwO.min.css" /> <script src="https://blog-static.cnblogs.com/files/miluluyo/OwO2.min.js"></script> <script src="https://blog-static.cnblogs.com/files/miluluyo/cute-cnblogs2.js"></script> <script src="https://blog-static.cnblogs.com/files/miluluyo/monitoring2.js"></script> <script> miluframe({ Youself:'https://www.cnblogs.com/faith007/', /*个人的博客园链接*/ /*博客园导航信息*/ custom:[{ name:'首页', link:'https://www.cnblogs.com/faith007/', istarget:false },{ name:'联系', link:'https://msg.cnblogs.com/send/%E7%AB%B9%E7%8B%BC', istarget:true },{ name:'留言板', link:'https://www.cnblogs.com/faith007/p/11578505.html', istarget:false },{ name:'相册', link:'https://www.cnblogs.com/faith007/gallery.html', istarget:false },{ name:'管理', link:'https://i.cnblogs.com/', istarget:true }], /*向别人展示自己的友链信息*/ resume:{ "name":"竹狼", "link":"https://www.cnblogs.com/faith007/", "headurl":"https://files-cdn.cnblogs.com/files/blogs/436273/VCG211321527184.bmp", "introduction":"大道至简,知易行难。" }], /*友链表格头信息,这个可以忽略*/ details:[{ field: 'name', literal: '昵称', },{ field: 'introduction', literal: '标语', },{ field: 'url', literal: '链接地址', },{ field: 'headurl', literal: '头像地址', }], /*浏览器顶部小图标*/ logoimg:'https://images.cnblogs.com/cnblogs_com/miluluyo/1765646/o_200519070633f12.png', /*文章页面标题前的图标,此处图标有扩展,下面会提到图标*/ cuteicon:['icon-caomei','icon-boluo','icon-huolongguo','icon-chengzi','icon-hamigua','icon-lizhi','icon-mangguo','icon-liulian','icon-lizi','icon-lanmei','icon-longyan','icon-shanzhu','icon-pingguo','icon-mihoutao','icon-niuyouguo','icon-xigua','icon-putao','icon-xiangjiao','icon-ningmeng','icon-yingtao','icon-taozi','icon-shiliu','icon-ximei','icon-shizi'], /*赞赏,若true则显示此按钮,false则不显示*/ isGratuity:false, /*赞赏按钮焦点显示赞赏内容,内容可自行更改*/ gratuity:'<div class="popper_box"><p><b>要请我喝奶茶吗 (づ。◕ᴗᴗ◕。)づ</b> </p><div class="popper_box_con"><div class="popper_box_con_li"><img src="https://images.cnblogs.com/cnblogs_com/miluluyo/1765646/o_200521053817wx.png" alt="">微信扫码</div><div class="popper_box_con_li"><img src="https://images.cnblogs.com/cnblogs_com/miluluyo/1765646/o_200521053827zfb.png" >支付宝扫码</div></div><p><b>留下一句你觉得很励志与美的话给我吧~</b>&nbsp;&nbsp;<b><a href="https://www.cnblogs.com/miluluyo/p/12930946.html">GO</a></b></div>' }) </script> <!-- 点赞 --> <canvas width="1777" height="841" style="position: fixed; left: 0px; top: 0px; z-index: 2147483647; pointer-events: none;"></canvas><script src="https://blog-static.cnblogs.com/files/miluluyo/mouse-click.js"></script> <!-- 以下内容是否添加你随意 --> <script> /*在文章页面添加古诗词*/ $("#navigator").after('<div class="poem-wrap"><div class="poem-border poem-left"></div><div class="poem-border poem-right"></div><h1>念两句诗</h1><div id="poem_sentence"></div><div id="poem_info"></div></div>') </script> <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script> <script type="text/javascript"> jinrishici.load(function(result) { var sentence = document.querySelector("#poem_sentence") var info = document.querySelector("#poem_info") sentence.innerHTML = result.data.content info.innerHTML = '【' + result.data.origin.dynasty + '】' + result.data.origin.author + '《' + result.data.origin.title + '》' }); </script> <script type="text/javascript"> /* 鼠标特效,我觉得太花哨了就注释了,喜欢的自己打开注释就可以 */ /*var a_idx = 0; jQuery(document).ready(function($) { $("body").click(function(e) { var a = new Array("❤去活出你自己。❤","❤今天的好计划胜过明天的完美计划。❤","❤不要轻言放弃,否则对不起自己。❤","❤紧要关头不放弃,绝望就会变成希望。❤","❤如果不能改变结果,那就完善过程。❤","❤好好活就是干有意义的事,有意义的事就是好好活!❤","❤你真正是谁并不重要,重要的是你的所做所为。❤","❤你不想为你的信仰冒一下险吗?难道想等你老了,再后悔莫及吗?❤","❤有些鸟儿是关不住的,它的每一根羽毛都闪耀着自由的光辉。❤","❤决定我们成为什么样人的,不是我们的能力,而是我们的选择。❤","❤掉在水里你不会淹死,呆在水里你才会淹死,你只有游,不停的往前游。❤","❤有些路,只能一个人走。❤","❤希望你眼眸有星辰,心中有山海。❤","❤从此以梦为马,不负韶华。❤","❤人的成就和差异决定于其业余时间。❤","❤佛不要你皈依,佛要你欢喜。❤","❤ダーリンのこと 大好きだよ❤","❤小猫在午睡时,地球在转。❤","❤我,混世大魔王,申请做你的小熊软糖。❤","❤决定好啦,要暗暗努力。❤","❤呐,做人呢最紧要开心。❤","❤好想邀请你一起去云朵上打呼噜呀。❤","❤永远年轻,永远热泪盈眶。❤","❤我生来平庸,也生来骄傲。❤","❤我走得很慢,但我从不后退。❤","❤人间不正经生活手册。❤","❤我是可爱的小姑娘,你是可爱。❤","❤数学里,有个温柔霸道的词,有且仅有。❤","❤吧唧一口,吃掉难过。❤","❤你头发乱了哦。❤","❤健康可爱,没有眼袋。❤","❤日月星辰之外,你是第四种难得。❤","❤你是否成为了了不起的成年人?❤","❤大家都是第一次做人。❤","❤何事喧哗?!❤","❤人间有味是清欢。❤","❤你笑起来真像好天气。❤","❤风填词半句,雪斟酒一壶。❤","❤除了自渡,他人爱莫能助。❤","❤昨日种种,皆成今我。❤","❤一梦入混沌 明月撞星辰❤","❤保持独立 适当拥有❤","❤谢谢你出现 这一生我很喜欢❤","❤做自己就好了 我会喜欢你的❤","❤太严肃的话,是没办法在人间寻欢作乐的❤","❤愿你余生可随遇而安,步步慢。❤","❤黄瓜在于拍,人生在于嗨❤","❤奇变偶不变,符号看象限。❤","❤从来如此,便对么?❤","❤今天我这儿的太阳,正好适合晒钙 你呢❤","❤未来可期,万事胜意。❤","❤星光不问赶路人 时光不负有心人❤","❤我当然不会试图摘月,我要月亮奔我而来❤","❤女生要修炼成的五样东西: 扬在脸上的自信,长在心底的善良, 融进血里的骨气,刻进命里的坚强,深到骨子里的教养❤","❤燕去燕归,沧海桑田。纵此生不见,平安惟愿❤","❤我想认识你 趁风不注意❤","❤我一直想从你的窗子里看月亮❤","❤长大应该是变温柔,对全世界都温柔。❤","❤别在深夜做任何决定❤","❤山中何事,松花酿酒,春水煎茶。❤","❤桃李春风一杯酒,江湖夜雨十年灯。❤","❤欲买桂花同载酒,终不似,少年游。❤"); var le = Math.ceil(Math.random()*a.length); var $i = $("<span></span>").text(a[le]);/*a[a_idx]*/ /*a_idx = (a_idx + 1) % a.length; var x = e.pageX, y = e.pageY; $i.css({ "z-index": 999999999999999999999999999999999999999999999999999999999999999999999, "top": y - 20, "left": x, "position": "absolute", "font-weight": "bold", "color": "rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")" }); $("body").append($i); $i.animate({ "top": y - 180, "opacity": 0 }, 2000, function() { $i.remove(); }); }); });*/ </script> <!--音乐,只在PC端宽度>1000px时显示--> <!-- <link rel="stylesheet" href="https://blog-static.cnblogs.com/files/miluluyo/APlayer.min.css"> <div id="player" class="aplayer aplayer-withlist aplayer-fixed" data-id="3116636104" data-server="netease" data-type="playlist" data-order="random" data-fixed="true" data-listfolded="true" data-theme="#2D8CF0"></div> <script src="https://blog-static.cnblogs.com/files/miluluyo/APlayer.min.js"></script> <script src="https://blog-static.cnblogs.com/files/miluluyo/Meting.min.js"></script> --> <!--猫,只在PC端显示,移动端不加载了,因为会卡顿页面--> <!-- <script src="https://eqcn.ajz.miesnfu.com/wp-content/plugins/wp-3d-pony/live2dw/lib/L2Dwidget.min.js"></script> <script> var mobile_flag = isMobile(); if(mobile_flag){ //console.info("移动端") }else{ //console.info("PC端") L2Dwidget.init({ "model": { "jsonPath": "https://unpkg.com/live2d-widget-model-hijiki/assets/hijiki.model.json", "scale": 1 }, "display": { "position": "left", "width": 100, "height": 200, "hOffset": 70, "vOffset": 0 }, "mobile": { "show": true, "scale": 0.5 }, "react": { "opacityDefault": 0.7, "opacityOnHover": 0.2 } }); window.onload = function(){ $("#live2dcanvas").attr("style","position: fixed; opacity: 0.7; left: 70px; bottom: 0px; z-index: 1; pointer-events: none;") } } </script> --> <script> <script language="javascript" type="text/javascript"> // 生成目录索引列表 // ref: http://www.cnblogs.com/wangqiguo/p/4355032.html // modified by: zzq function GenerateContentList() { var mainContent = $('#cnblogs_post_body'); var h3_list = $('#cnblogs_post_body h3');//如果你的章节标题不是h2,只需要将这里的h2换掉即可 if(mainContent.length < 1) return; if(h3_list.length>0) { var content = '<a name="_labelTop"></a>'; content += '<div id="navCategory" style="color:#152e97;">'; content += '<p style="font-size:18px;"><b>目录</b></p>'; content += '<ul>'; for(var i=0; i<h3_list.length; i++) { var go_to_top = '<div style="text-align: right;"><a href="#_labelTop" style="color:#f68a33">回到顶部</a><a name="_label' + i + '"></a></div>'; $(h3_list[i]).before(go_to_top); var h4_list = $(h3_list[i]).nextAll("h4"); var li3_content = ''; for(var j=0; j<h4_list.length; j++) { var tmp = $(h4_list[j]).prevAll('h3').first(); if(!tmp.is(h3_list[i])) break; var li3_anchor = '<a name="_label' + i + '_' + j + '"></a>'; $(h4_list[j]).before(li3_anchor); li3_content += '<li><a href="#_label' + i + '_' + j + '">' + $(h4_list[j]).text() + '</a></li>'; } var li3_content = ''; if(li3_content.length > 0) li3_content = '<li><a href="#_label' + i + '">' + $(h3_list[i]).text() + '</a><ul>' + li3_content + '</ul></li>'; else li3_content = '<li><a href="#_label' + i + '">' + $(h3_list[i]).text() + '</a></li>'; content += li3_content; } content += '</ul>'; content += '</div><p>&nbsp;</p>'; content += '<hr style="height:1px;border:none;border-top:1px dashed #0066CC;"/>'; if($('#cnblogs_post_body').length != 0 ) { $($('#cnblogs_post_body')[0]).prepend(content); } } } GenerateContentList(); </script>