zoukankan      html  css  js  c++  java
  • 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

     
  • 相关阅读:
    Json To CSharp
    一种C#泛型方法在lua中表示的设计
    FSM Code Generator
    vmware无法安装vmware authorization&windows无法启动VMware Authorization Service服务
    攻击树威胁建模
    自适应安全架构的历史和演进
    网络攻击如何影响物理世界
    网络安全红蓝军对抗完整战术周期
    工作组渗透-内网搜集实战
    AV-TEST杀毒软件能力测试(2018年1月-12月)杀毒软件排名
  • 原文地址:https://www.cnblogs.com/neowu/p/10742924.html
Copyright © 2011-2022 走看看