猜猜两个随机选择的数字之间的数字

问题描述 投票:0回答:2

我正在编写一个程序,我生成两个随机数,然后用户选择一个数字。如果用户选择一个介于两个随机数之间的数字,请说“好猜”,如果它不在两个数字之间说“超出范围”。程序提示用户输入“是”再次播放。当用户停止播放时,它将返回正确猜测的百分比。

我一直在尝试使用while循环,我认为我的概念是正确的。

public static void main(String[] args) {
    Scanner get = new Scanner(System.in);
    Scanner get2 = new Scanner(System.in);
    Scanner get3 = new Scanner(System.in);

    Random ran = new Random();

    int start = ran.nextInt(99) + 1;
    int end = ran.nextInt(99) + 1;
    int win = 0;
    int loss = 0;
    boolean keepGoing = false;

    System.out.println("enter a number: ");
    int num = get.nextInt();
    displayGuessResults(start, end, num);

    if (displayGuessResults(start, end, num) == true) {
        win++;
    }
    if (displayGuessResults(start, end, num) == false) {
        loss++;
    }

    System.out.println("enter 'yes' to continue: ");
    String ans = get2.nextLine();

    if (ans.equalsIgnoreCase("yes")) {
        keepGoing = true;

    } else {
        keepGoing = false;
        System.out.println("There are " + win + " guess(es) within the range");
        double percent = (win / (win + loss));
        System.out.println("Correct guess percentage is " + (percent*100) + "%");
    }
    while (keepGoing == true) {
        start = ran.nextInt(99) + 1;
        end = ran.nextInt(99) + 1;

        displayGuessResults(start, end, getValidGuess(get));
        if (displayGuessResults(start, end, num) == true) {
            win++;
        }

        if (displayGuessResults(start, end, num) == false) {
            loss++;
        }
        System.out.println("enter 'yes' to continue: ");
        String ans2 = get2.nextLine();
        if (ans2.equalsIgnoreCase("yes")) {
            keepGoing = true;

        } else {
            keepGoing = false;

        }
    }
    if (keepGoing == false) {
        System.out.println("There are " + win + " guess(es) within the range");
        double percent1 = (win / (win + loss));
        System.out.println("Correct guess percentage is " + (percent1) + "%");

    }

}

 public static int getValidGuess(Scanner get3) {
    int num = 0;
    System.out.println("enter a number: ");
    num = get3.nextInt();

    if (num < 100 && num > 0) {
        return num;
    } else {
        return -1;
    }

}

public static boolean displayGuessResults(int start, int end, int num) {
    boolean results = false;
    int min = 0, max = 0;
    if (start > end) {
        max = start;
        min = end;
    } else if (start < end) {
        min = start;
        max = end;

    }

    if (min <= num && num <= max) {
        System.out.println("You entered: " + num + " the nubmers are " + start + " and " + end);
        System.out.println("good guess");
        return results = true;

    } else {
        System.out.println("You entered: " + num + " the nubmers are " + start + " and " + end);
        System.out.println("out of bounds");
        return results = false;
    }

}

预期成绩:

输入一个数字:12

你输了12,这两个随机数是22和45

出界

输入yes重启yes

输入一个数字:45

你输了12,两个随机数是43和67好猜

输入yes重启yes

输入一个数字:98

你输入了98这两个随机数是23和87出界

输入yes以重新启动否

范围内有(1)猜测

正确的猜测百分比是33.3%

实际结果:

输入一个数字:43

您输入了:43数字是33和30超出范围您输入:43数字是33和30超出范围您输入:43 nubmers是33和30超出范围

输入'yes'继续:是的


输入一个数字:34您输入:34数字是81和98超出范围您输入:43数字是81和98超出范围您输入:43 nubmers是81和98超出范围

输入'yes'继续:是的


输入一个数字:98你输入:98数字是96和90超出范围你输入:43数字是96和90超出范围你输入:43 nubmers是96和90越界

输入'yes'继续:是的


输入一个数字:25你输入:25数字是54和40超出范围你输入:43数字是54和40好猜测你输入:43 nubmers是54和40好猜测

输入'yes'继续:no

在该范围内有1个猜测正确的猜测百分比是0.0%

java loops methods
2个回答
0
投票

首先是这一部分

int num = get.nextInt();
displayGuessResults(start, end, num);

if (displayGuessResults(start, end, num) == true) {
    win++;
}
if (displayGuessResults(start, end, num) == false) {
    loss++;
}

每次调用displayGuessResults()时,你都会重新打印到控制台,我认为这不是你想要的行为。你应该做点什么

    boolean result = displayGuessResults(start, end, num);

    if (result) {
        win++;
    }
    else {
        loss++;
    }

第二件事是你的程序中的逻辑流程,似乎你在while循环中有重复的代码,在它之前它违背了循环的目的,你可以从keepGoing = true开始;并删除while循环外的代码或使用do-while。您的代码中有很多需要修复的地方,您需要告诉我们您需要帮助的确切内容。


0
投票

好吧,我猜你在问题开始时做了正确的逻辑但没有完成它,你的displayGuessResults()应该是这样的:

public static boolean displayGuessResults(int start, int end, int num) {
    int min = 0, max = 0;
    if (start > end) {
        max = start;
        min = end;
    } else if (start < end) {
        min = start;
        max = end;

    }

    if (min <= num && num <= max) {
        System.out.println("You entered: " + num + " the nubmers are " + min + " and " + max);
        System.out.println("good guess");
        return true;

    } else {
        System.out.println("You entered: " + num + " the nubmers are " + min + " and " + max);
        System.out.println("out of bounds");
        return false;
    }

}

此外,当你计算正确猜测的百分比时,你必须在分割时从int转换为double,否则它将取出小数位。你要做到这一点:

double percent1 = ((double) win) / (win + loss)

© www.soinside.com 2019 - 2024. All rights reserved.