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 ...
随机推荐
- DotNetCore跨平台~dotnet pack打包详细介绍
回到目录 dotnet pack 命令生成项目并创建 NuGet 包.这个操作的结果是两个 nupkg 扩展名的包.一个包含代码,另一个包含调试符号. 该项目被依赖的 NuGet 包装被添加到 nus ...
- 教你编写百度搜索广告过滤的chrome插件
1 前言 目前百度搜索列表首页里,广告5条正常内容是10条,而且广告都是前1到5条的位置,与正常内容的显示样式无异.对于我们这样有能力的开发者,其实可以简单的实现一个chrome插件,在百度搜索页面里 ...
- 微软开发者大会:VS 2019 Preview 发布;Windows UX 主要技术开源
美国当地时间12月4日,微软正式举行 Microsoft Connect(); 2018 开发者大会,本次大会的 slogan 是"Build the apps of tomorrow, t ...
- 从PRISM开始学WPF(番外)共享上下文 RegionContext-更新至Prism7.1
RegionContext共享上下文 There are a lot of scenarios where you might want to share contextual information ...
- 使用wepy开发微信小程序01——常用的组件
1.axio.js 'use strict' import axios from 'axios' import wepyAxiosAdapter from 'wepy-plugin-axios/dis ...
- SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用
为什么要使用分布式系统? 容错 减少延迟/提高性能 可用性 负载均衡 总而言之,其实目的只有一个,”用户体验“. 什么是分布式系统? 分布式系统是由使用分发中间件连接的自治计算机组成的网络.它们有助于 ...
- 数据结构——Java实现单链表
一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...
- 备忘录模式 Memento 快照模式 标记Token模式 行为型 设计模式(二十二)
备忘录模式 Memento 沿着脚印,走过你来时的路,回到原点. 苦海翻起爱恨 在世间难逃避命运 相亲竟不可接近 或我应该相信是缘份 一首<一生所爱>触动了多少 ...
- Django常见问题
1.什么是中间件? 中间件是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出. 中间件一般做认证或批量请求处理,django中的中间 ...
- 重建程序员能力(2)-如何使asp.net mvc应用增加js和其他功能
1. 在Visual Studio的解决方案资源管理器,找到项目右键展开右键菜单后选择 管理NuGet程序包. 2.在打开的页面中,可以按需要选择Jquery.BootStrap等页面展现框架. 有工 ...