在没有列表/数组的猜谜游戏中跟踪最低分数(Java)

在没有列表/数组的猜谜游戏中跟踪最低分数(Java),java,Java,我被困在一个我一直在做的短程序中。我需要找到一种方法来存储用户运行游戏多次后的最佳分数(最低分数)。这是我的程序代码- import java.util.*; import java.util.Random; import java.util.Scanner; public class GuessingGame { public static void main(String[] args){ Random rand = new Random(); Scanner promp

我被困在一个我一直在做的短程序中。我需要找到一种方法来存储用户运行游戏多次后的最佳分数(最低分数)。这是我的程序代码-

import java.util.*;
import java.util.Random;
import java.util.Scanner;
public class GuessingGame
{
public static void main(String[] args){


    Random rand = new Random();
    Scanner prompt = new Scanner(System.in);

    String play = "";
    boolean playAgain = true;
    int playTimes = 0;
    int lowScore = 0; 
    int count = 0;

    do{
        int target = rand.nextInt(100) + 1;

        System.out.print("\nEnter a number between 1 and 100: ");

        int guess = prompt.nextInt();

        while(guess != target){
            if(target > guess){
                System.out.println("The number is higher. Try again.");
                count++;
            }
            else{
                System.out.println("The number is lower. Try again.");
                count++;
            }

        System.out.print("Enter a number between 1 and 100: ");
        guess = prompt.nextInt();
        }

        System.out.println("You guessed correctly! Congratulations!");
        count++;
        System.out.println("Your score is: " + count);



        System.out.print("\nWould you like to play again? Yes or no?: ");
        play = prompt.next();

        if(play.equalsIgnoreCase("no")){
            playAgain = false;
        }
        else if(play.equalsIgnoreCase("yes")){
            count = 0;
            playAgain = true;
        }

        playTimes++;

        if(count < lowScore){
            lowScore = count;
        } 

    }while(playAgain);


    System.out.println("\nGame Summary");
    System.out.print("      Games played: " + playTimes);

    System.out.println("\n        Best Score: " +lowScore);
import java.util.*;
导入java.util.Random;
导入java.util.Scanner;
公开课猜谜游戏
{
公共静态void main(字符串[]args){
Random rand=新的Random();
扫描仪提示=新扫描仪(System.in);
弦乐‘’;
布尔值=真;
int播放时间=0;
int lowScore=0;
整数计数=0;
做{
int target=rand.nextInt(100)+1;
System.out.print(“\n输入一个介于1和100之间的数字:”);
int guess=prompt.nextInt();
while(猜测!=目标){
如果(目标>猜测){
System.out.println(“数字更高,请重试”);
计数++;
}
否则{
System.out.println(“数字较低,请重试”);
计数++;
}
System.out.print(“输入一个介于1和100之间的数字:”);
guess=prompt.nextInt();
}
System.out.println(“你猜对了!祝贺你!”);
计数++;
System.out.println(“您的分数是:“+count”);
System.out.print(“\n是否要再次播放?是或否:”;
play=prompt.next();
if(play.equalsIgnoreCase(“no”)){
再次播放=错误;
}
else if(play.equalsIgnoreCase(“是”)){
计数=0;
再次播放=正确;
}
游戏时间++;
如果(计数<低分){
低分=计数;
} 
}边玩边玩;
System.out.println(“\n名称摘要”);
系统输出打印(“已玩游戏:+游戏时间”);
System.out.println(“\n最佳分数:“+lowScore”);

问题是我一直在运行这个程序,“最佳分数”一直显示为0。我尝试将“if”语句带到while循环之外,但它仍显示为0。有人能帮我解释一下我的逻辑吗?

count
被初始化为0,并且只会递增,因此除非发生溢出,否则它不会为负值

lowScore
被初始化为0,并且任何非负数都不小于,因此
count
的概率太小

您应该将
lowScore
初始化为
Integer.MAX\u VALUE
或引入一个变量来记住
lowScore
是否具有如下有效分数:

int lowScore = 0;
int count = 0;
boolean isLowScoreValid = false; // the variable

if(!isLowScoreValid || count < lowScore){ // update if any value were't set
    lowScore = count;
    isLowScoreValid = true; // now a value is set
}

System.out.println("\n        Best Score: " +(isLowScoreValid ? lowScore : "(none)"));
int lowScore=0;
整数计数=0;
布尔值isLowScoreValid=false;//变量
如果(!isLowScoreValid | | count
计数
初始化为0,并且仅递增,因此除非发生溢出,否则不会为负值

lowScore
被初始化为0,并且任何非负数都不小于,因此
count
的概率太小

您应该将
lowScore
初始化为
Integer.MAX\u VALUE
或引入一个变量来记住
lowScore
是否具有如下有效分数:

int lowScore = 0;
int count = 0;
boolean isLowScoreValid = false; // the variable

if(!isLowScoreValid || count < lowScore){ // update if any value were't set
    lowScore = count;
    isLowScoreValid = true; // now a value is set
}

System.out.println("\n        Best Score: " +(isLowScoreValid ? lowScore : "(none)"));
int lowScore=0;
整数计数=0;
布尔值isLowScoreValid=false;//变量
如果(!isLowScoreValid | | count
您需要将
低分数
初始化为可能的最高整数。请尝试

int lowScore=Integer.MAX_VALUE;
然后运行您的程序。干杯!

您需要将
lowScore
初始化为可能的最高整数。尝试

int lowScore=Integer.MAX_VALUE;
然后运行你的程序。干杯!

我建议你应该正确格式化你的代码。是的,这是因为当你玩游戏时,lowScore一开始永远不会大于count。因此lowScore总是…0!看看代码
如果(count
这永远不会是真的。在每场比赛之前不应该初始化
count
吗?如果用户输入“yes”使play再次为真,count将重新初始化为0。如果用户输入“hello”怎么办?我建议你应该正确地格式化你的代码。是的,那是因为当你玩游戏时,lowScore一开始永远不会大于count。因此lowScore总是…0!如果(count这永远不会是真的。是否应该在每次游戏前初始化
count
?如果用户输入“yes”使play再次为真,count将重新初始化为0。如果用户输入“hello”该怎么办?