鱼C论坛

 找回密码
 立即注册
查看: 4965|回复: 2

[已解决]猜数字游戏遇到的问题

[复制链接]
发表于 2021-3-19 20:14:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
package test2;

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

public class Guess {

        public static void main(String[] args) {
               
                 Random random = new Random();
                 
                 Scanner sc = new Scanner(System.in);
               
                 int num = (int)(random.nextInt(101));
                 
                 System.out.println(num);
                 
                 int i = 0;
                 String chs;
                 do {
                         while(true) {
                                 long firstTime = System.currentTimeMillis();
                                 System.out.println("请输入你猜测的数值: ");
                                 int guess = sc.nextInt();
                                 
                                 if(guess < num) {
                                        System.out.println("你猜小了!");
                                        i++;
                                 }
                                 else if(guess > num) {
                                        System.out.println("你猜大了!");
                                        i++;
                                 }
                                 else {
                                        long secondTime = System.currentTimeMillis();
                                        System.out.println("你猜对了!");
                                        i++;
                                        System.out.println("你一共猜了 " +i +"次。");
                                        System.out.println("你一共用时:"+(secondTime - firstTime) / 1000+"秒");
                                        break;
                                 }
                         }
                         System.out.println("请选择是否继续游戏?请输入:Yes/No");
                         Scanner sca = new Scanner(System.in);
                         chs = sca.nextLine();
                         if(chs != "Yes") {
                                 System.out.println("游戏结束");
                         }
                }while(chs == "Yes") ;
                         

        }

}



代码如上,最后do while循环是想有一个继续游戏的功能,输入Yes则继续开始一局新游戏,但是实际上输入Yes后,游戏直接结束,不知道哪里错了,希望大佬们能看一看
最佳答案
2021-3-19 20:25:55
  1. package test;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. import java.util.Date;

  5. public class test1 {

  6.         public static void main(String[] args) {

  7.                
  8.                  Random random = new Random();
  9.                  
  10.                  Scanner sc = new Scanner(System.in);
  11.                
  12.                  int num = (int)(random.nextInt(101));
  13.                  
  14.                  System.out.println(num);
  15.                  
  16.                  int i = 0;
  17.                  String chs;
  18.                  do {
  19.                          while(true) {
  20.                                  long firstTime = System.currentTimeMillis();
  21.                                  System.out.println("请输入你猜测的数值: ");
  22.                                  int guess = sc.nextInt();
  23.                                  
  24.                                  if(guess < num) {
  25.                                         System.out.println("你猜小了!");
  26.                                         i++;
  27.                                  }
  28.                                  else if(guess > num) {
  29.                                         System.out.println("你猜大了!");
  30.                                         i++;
  31.                                  }
  32.                                  else {
  33.                                         long secondTime = System.currentTimeMillis();
  34.                                         System.out.println("你猜对了!");
  35.                                         i++;
  36.                                         System.out.println("你一共猜了 " +i +"次。");
  37.                                         System.out.println("你一共用时:"+(secondTime - firstTime) / 1000+"秒");
  38.                                         break;
  39.                                  }
  40.                          }
  41.                          System.out.println("请选择是否继续游戏?请输入:Yes/No");
  42.                          Scanner sca = new Scanner(System.in);
  43.                          chs = sca.nextLine();
  44.                          if(!chs.equals("Yes")) {    //字符串比较不要使用‘==’下面while行同理
  45.                                  System.out.println("游戏结束");
  46.                          }
  47.                 }while(chs.equals("Yes")) ;
  48.         }
  49. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-19 20:25:55 | 显示全部楼层    本楼为最佳答案   
  1. package test;
  2. import java.util.Scanner;
  3. import java.util.Random;
  4. import java.util.Date;

  5. public class test1 {

  6.         public static void main(String[] args) {

  7.                
  8.                  Random random = new Random();
  9.                  
  10.                  Scanner sc = new Scanner(System.in);
  11.                
  12.                  int num = (int)(random.nextInt(101));
  13.                  
  14.                  System.out.println(num);
  15.                  
  16.                  int i = 0;
  17.                  String chs;
  18.                  do {
  19.                          while(true) {
  20.                                  long firstTime = System.currentTimeMillis();
  21.                                  System.out.println("请输入你猜测的数值: ");
  22.                                  int guess = sc.nextInt();
  23.                                  
  24.                                  if(guess < num) {
  25.                                         System.out.println("你猜小了!");
  26.                                         i++;
  27.                                  }
  28.                                  else if(guess > num) {
  29.                                         System.out.println("你猜大了!");
  30.                                         i++;
  31.                                  }
  32.                                  else {
  33.                                         long secondTime = System.currentTimeMillis();
  34.                                         System.out.println("你猜对了!");
  35.                                         i++;
  36.                                         System.out.println("你一共猜了 " +i +"次。");
  37.                                         System.out.println("你一共用时:"+(secondTime - firstTime) / 1000+"秒");
  38.                                         break;
  39.                                  }
  40.                          }
  41.                          System.out.println("请选择是否继续游戏?请输入:Yes/No");
  42.                          Scanner sca = new Scanner(System.in);
  43.                          chs = sca.nextLine();
  44.                          if(!chs.equals("Yes")) {    //字符串比较不要使用‘==’下面while行同理
  45.                                  System.out.println("游戏结束");
  46.                          }
  47.                 }while(chs.equals("Yes")) ;
  48.         }
  49. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-19 21:06:54 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-5 05:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表