147380124 发表于 2021-3-19 20:14:22

猜数字游戏遇到的问题

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后,游戏直接结束,不知道哪里错了,希望大佬们能看一看{:10_266:}

洋洋痒 发表于 2021-3-19 20:25:55

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

public class test1 {

        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.equals("Yes")) {    //字符串比较不要使用‘==’下面while行同理
                                 System.out.println("游戏结束");
                         }
                }while(chs.equals("Yes")) ;
        }
}

147380124 发表于 2021-3-19 21:06:54

洋洋痒 发表于 2021-3-19 20:25


感谢大佬!
页: [1]
查看完整版本: 猜数字游戏遇到的问题