Scanner的使用 猜数字 +猜电影
猜数字
public class Main { public static void main(String[] args) {
int random=(int)(Math.random()*100)+1;
System.out.println("Guess 1 ~100,you have 10 times.");
Scanner scanner=new Scanner(System.in);
boolean success=false; for (int i=10;i>0;i--){
int number=scanner.nextInt();
if (number<random){
System.out.println("small");
}else if(number>random){
System.out.println("big");
} else {
success=true;
break;
}
} if (success){
System.out.println("you win !");
}else {
System.out.println("you lose, Correct number was:"+random);
} }
}
游戏的技巧就是二分搜索法 范围是0~100;
第一次猜测50,如果更小,范围在0~50,猜测25。反之,范围在50~100,猜测75,不断重复。
实际上,最多只需猜测7次。 猜电影
movies.txt
the shawshank redemption
the godfather
the dark knight
schindler's list
pulp fiction
the lord of the rings
the good the bad and the ugly
fight club
the lord of the rings
forrest gump
star wars
inception
the lord of the rings
the matrix
samurai
star wars
city of god
the silence of the lambs
batman begins
die hard
chinatown
room
dunkirk
fargo
no country for old men
Movie
public class Movie { public static String random(){
List<String> list=new ArrayList<>();
File file=new File("movies.txt");
try {
Scanner scanner=new Scanner(file);
while (scanner.hasNextLine()){
String movieLine=scanner.nextLine();
list.add(movieLine);
}
}catch (FileNotFoundException e){
System.out.println("not found movies.txt");
} String movie=list.get((int)(Math.random()*list.size()));
return movie; }
}
Main.java public class Main { public static void main(String[] args) { String randomMovie = Movie.random();
boolean success = false;
StringBuilder guessedLetters = new StringBuilder(" ");
StringBuilder wrongLetters = new StringBuilder();
int length = randomMovie.length();
int wrongLettersCounter = 0;
System.out.println("Guess a letter,You have 10 times. ");
System.out.println("The length is: " + length); while (wrongLettersCounter < 10) {
Scanner scanner = new Scanner(System.in);
String newLetter = scanner.nextLine(); if (randomMovie.contains(newLetter)) {
guessedLetters.append(newLetter);
int location = randomMovie.indexOf(newLetter) + 1;
System.out.println("The position: " + location);
} else {
wrongLetters.append(newLetter);
wrongLettersCounter++;
} String displayHiddenMovie = randomMovie.replaceAll("[^" + guessedLetters + "]", "_");
if (displayHiddenMovie.equals(randomMovie)) {
success = true;
break;
}
System.out.println("You are guessing: " + displayHiddenMovie);
System.out.println("You have guessed " + wrongLettersCounter + " wrong letters: " + wrongLetters); } if (success) {
System.out.println("You win!");
} else {
System.out.println("You Lose! Correct movie was: " + randomMovie);
} } }
游戏的技巧:英语中,出现频率最高的前五个字母依次是:e, t, a, o, i
Scanner的使用 猜数字 +猜电影的更多相关文章
- A - 猜数字
http://acm.hdu.edu.cn/showproblem.php?pid=1172 猜数字 猜数字游戏是gameboy最喜欢的游戏之一.游戏的规则是这样的:计算机随机产生一个四位数,然后玩家 ...
- java 猜数字游戏
作用:猜数字游戏.随机产生1个数字(1~10),大了.小了或者成功后给出提示. 语言:java 工具:eclipse 作者:潇洒鸿图 时间:2016.11.10 >>>>> ...
- Java基础知识强化之IO流笔记70:Properties练习之 如何让猜数字小游戏只能玩5次的案例
1. 使用Properties完成猜数字小游戏只能玩5次的案例: 2. 代码实现: (1)猜数字游戏GuessNumber: package cn.itcast_08; import java.uti ...
- java猜数字小游戏
/* * * 猜数字小游戏 * * 先由系统生成一个2-100之间的随机数字, * * 然后捕获用户从控制台中输入的数字是否与系统生成的随机数字相同, * * 如果相同则统计用户所猜的次数,并给出相应 ...
- Java完成简单猜数字游戏v2.0
猜数字游戏v2.0 优化了获取随机数.输入数据超出边界值的代码,并增加了异常处理,能够在玩家输入错误数据错误时给出可靠指引,希望对和我一样的新人有帮助, 最后希望有大神愿意帮我解决代码优化的问题,谢谢 ...
- java课程设计--猜数字(团队博客)
java课程设计--猜数字(团队博客) 1.团队名称以及团队成员介绍 团队名称:cz 团队成员:陈伟泽,詹昌锦 团队照片: 2.项目git地址 http://git.oschina.net/Devil ...
- java使用for循环做猜数字游戏
package org.llh.test;import java.util.Random;import java.util.Scanner;/** * 猜数字游戏 * * @author llh * ...
- 猜数字游戏,判断输入的数字与系统产生的数字是否一致(Math.random()与if嵌套循环)
package com.summer.cn; import java.util.Scanner; public class Test041509 { /** * java 随机数 Math * Mat ...
- 猜数字游戏 在控制台运行--java详解!了;来玩
import java.util.Scanner;//导入包 import java.util.Scanner; 注意格式 符号的使用 public class Demo{ //猜数字游戏 练习 pu ...
随机推荐
- 禁用 Chrome 的黑色模式/Dark Mode
macOS Mojave 中引入了系统层面的黑色模式,Chrome 73 在应用中支行了这一模式,即系统设置为黑色模式时,Chrome 会自动适应切换到 Dark Mode. Chrome 跟随系统设 ...
- 理解 Linux 中 `ls` 的输出
ls 的输出会因各 Linux 版本变种而略有差异,这里只讨论一般情况下的输出. 下面是来自 man page 关于 ls 的描述: $ man ls ls - list directory cont ...
- SpringBoot进阶教程(二十三)Linux部署Quartz
在之前的一篇文章中<SpringBoot(九)定时任务Schedule>,已经详细介绍了关于schedule框架的配置和使用,有收到一些朋友关于部署的私信,所以抽时间整理一个linux部署 ...
- 限制TextBox只允许输入数字和字母
设置TextBox控件属性 ImeMode=Disable ShortcutsEnabled=False VB.NET Private Sub TextBox1_KeyDown(sender As O ...
- tomcat部署项目后,war包是否可刪?war包存在必要性!
在tomcat中webapps目錄上傳war包后, 对war解压时候. war不能在tomcat运行时删除,否则会删除自动解压的工程. 你可以停止tomcat后删除war. 当你重新部署的时候,如果 ...
- JFreeChart画图+jsp页面显示实现统计图
1 开发环境: 1.eclipse(可替换) 2.jfreechart-1.0.19 2 说明: (1) source目录:为 jfreechart的源码目录:不会的主要看这里.因为他的文档是收费的. ...
- 使用curl制作简易百度搜索
这几天研究了一下php中的curl类库,做了一个简单的百度搜索,先上代码 <div style="width:200px;height:100px;"> <div ...
- 【译】.NET Core 3.0 中的新变化
.NET Core 3.0 是 .NET Core 平台的下一主要版本.本文回顾了 .Net Core 发展历史,并展示了它是如何从基本支持 Web 和数据工作负载的版本 1,发展成为能够运行 Web ...
- Vue项目需求实现记录(永久更新)
1.表单校验功能: 在el-form标签中定义:rules="rules";ref="reference" 在el-form-item定义prop=" ...
- python3 tkinter报错:_tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by grid
报错: _tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by ...