English 中文(简体)
我可以说明如何在我的java法典中为一个班级项目确定白天空间。 我确信,守则应当提供预期产出
原标题:I can t figure out how to fix the whitespace in my java code for a class project. I m sure that the code should provide the intended output

这是我迄今为止制定的准则。

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

public class GuessANumber {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a random seed: ");
        int seed = scanner.nextInt();
        Random rnd = new Random (seed);
        
        int rightAnswer = rnd.nextInt(200)+1;
        
        int numAttempts = 0;
        
        System.out.print("Enter a guess between 1 and 200: ");
        int guess = scanner.nextInt();
        numAttempts ++;
        
        while (guess != rightAnswer) {
           if (guess < 1 || guess > 200) {
              System.out.println("Your guess is out of range. Pick a number between 1 and 200.");
           }
           if (guess > rightAnswer) {
              System.out.println("Your guess was too high - try again.");
           }
           else if (guess < rightAnswer) {
              System.out.println("Your guess was too low - try again.");
           }
           System.out.println();
           System.out.print("Enter a guess between 1 and 200: ");
           guess = scanner.nextInt();
           numAttempts ++;
        }
        
        if (guess == rightAnswer) {
           System.out.println("Congratulations!  Your guess was correct!");
           System.out.println();
           System.out.println("I had chosen " + rightAnswer + " as the target number.");
           System.out.println("You guessed it in " + numAttempts + " tries.");
        }
        
        if (numAttempts == 1) {
           System.out.println("That was impossible!");
        }
        else if (numAttempts == 2 || numAttempts == 3) {
           System.out.println("You re pretty lucky!");
        }
        else if (numAttempts >= 4 && numAttempts <= 7) {
           System.out.println("Not bad, not bad...");
        }
        else if (numAttempts == 8) {
           System.out.println("That was not very impressive.");
        }
        else if (numAttempts == 9 || numAttempts == 10) {
           System.out.println("Are you having any fun at all?");
        }
        else if (numAttempts >= 11) {
           System.out.println("Maybe you should play something else.");
        }
        
        scanner.close();

    }
}

这是产出应当做到的:

Enter a random seed: Enter a guess between 1 and 200: Your guess is out of range.  Pick a number between 1 and 200.
Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess is out of range.  Pick a number between 1 and 200.
Your guess was too high - try again.

然而,我关于这一试验的法规规定,除此外,还有一线白色空间,如:

Enter a random seed: Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess was too low - try again.

Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too high - try again.

E

请让我了解任何建议。

问题回答

<<>code>if (guess ==rightAnswer>{>,应改为in while loop body. 此外,它还需要一个<条码>突破。 我还建议采用<条码>。 接着,你可以 lo。 我也不会把无效的炉.视为企图。 把这一切结合起来,它可能会看像这样的东西。

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a random seed: ");
int seed = scanner.nextInt();
Random rnd = new Random(seed);

int rightAnswer = rnd.nextInt(200) + 1;
int numAttempts = 0;

do {
    System.out.println("Enter a guess between 1 and 200: ");
    int guess = scanner.nextInt();
    numAttempts++;
    if (guess < 1 || guess > 200) {
        System.out.println("Your guess is out of range. Pick a number between 1 and 200.");
        numAttempts--;
    } else if (guess > rightAnswer) {
        System.out.println("Your guess was too high - try again.");
    } else if (guess < rightAnswer) {
        System.out.println("Your guess was too low - try again.");
    } else if (guess == rightAnswer) {
        System.out.println("Congratulations!  Your guess was correct!");
        System.out.println();
        System.out.println("I had chosen " + rightAnswer + " as the target number.");
        System.out.println("You guessed it in " + numAttempts + " tries.");
        break;
    }
} while (true);

if (numAttempts == 1) {
    System.out.println("That was impossible!");
} else if (numAttempts <= 3) {
    System.out.println("You re pretty lucky!");
} else if (numAttempts <= 7) {
    System.out.println("Not bad, not bad...");
} else if (numAttempts == 8) {
    System.out.println("That was not very impressive.");
} else if (numAttempts <= 10) {
    System.out.println("Are you having any fun at all?");
} else {
    System.out.println("Maybe you should play something else.");
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签