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 ...
随机推荐
- JPA中EntityListeners注解的使用
使用场景 EntityListeners在jpa中使用,如果你是mybatis是不可以用的 它的意义 对实体属性变化的跟踪,它提供了保存前,保存后,更新前,更新后,删除前,删除后等状态,就像是拦截器一 ...
- 一个简洁的小H车调运模型
一个简洁的小H车调运模型 不久前, 帝都B城市到处都是小H车, 理想的小H车应该是布朗运动\均匀分布,可是现实上它们就是不均匀.于是有如下问题: 观察帝都 HD区SY村区域,将其划分成10个用车点,用 ...
- SmartSql 动态代理仓储
SmartSql 动态代理仓储,一个高生产力的组件.该组件看似很难懂,实际上仅做了映射Statement,转发请求的功能.但却意义重大. SmartSql提供了一个通用泛型仓储接口 SmartSql. ...
- Spring Boot(六):如何优雅的使用 Mybatis
*:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...
- SLAM+语音机器人DIY系列:(一)Linux基础——1.Linux简介
摘要 由于机器人SLAM.自动导航.语音交互这一系列算法都在机器人操作系统ROS中有很好的支持,所以后续的章节中都会使用ROS来组织构建代码:而ROS又是安装在Linux发行版ubuntu系统之上的, ...
- 使用C#开发windows服务定时发消息到钉钉群_群组简单消息
前言:本提醒服务,是由C#语言开发的,主要由windows服务项目和winform项目组成,运行服务可实现功能:向钉钉自定义机器人群组里,定时,定次,推送多个自定义消息内容,并实现主要功能的日志记录. ...
- Spring Boot 2.X 如何添加拦截器?
最近使用SpringBoot2.X搭建了一个项目,大部分接口都需要做登录校验,所以打算使用注解+拦截器来实现,在此记录下实现过程. 一.实现原理 1. 自定义一个注解@NeedLogin,如果接口需要 ...
- java开发注解总结笔记
目录 1.最基础注解(spring-context包下的org.springframework.stereotype) 1.1.@Controller @Service @Repository @Co ...
- Web前端 web的学习之路2
2019 年 Web 开发技术指南和趋势 2019/01/23 · JavaScript · 趋势 转载:原文出处: 李棠辉(http://web.jobbole.com/95622/) 以下内 ...
- 前端入门21-JavaScript的ES6新特性
声明 本篇内容全部摘自阮一峰的:ECMAScript 6 入门 阮一峰的这本书,我个人觉得写得挺好的,不管是描述方面,还是例子,都讲得挺通俗易懂,每个新特性基本都还会跟 ES5 旧标准做比较,说明为什 ...