设计一个应用程序,该应用程序会从1到100的随机数中进行选择,然后提示用户猜测该数字,并警告用户只有5次尝试才能正确猜测。每次猜测之后,向用户报告他或她是正确的还是猜测过高或过低。如果猜对了,则打印出祝贺消息。如果猜错5次失败,请打印一条消息,说明游戏已结束。在游戏结束时,提示用户指示他/她是否希望再次玩游戏。
我特别需要结尾的帮助,我想代码的布局也要...我运行它并且它不断运行,我尝试使用了多个网站和我的教科书,但是没有运气,在此先谢谢您! !
Java,Net bean 8.2
<p-card>
<p-header>
<div style="float: left;">
<i class="pi pi-pencil" style="font-size: 1.5em" (click)="edit()" style="cursor: pointer; margin-left: 8px;"></i>
<i class="pi pi-trash" style="font-size: 1.5em" (click)="delete()" style="cursor: pointer; margin-left: 8px;"></i>
</div>
<div style="float: right;">
<p style="margin-right: 8px; font-size: 10px;">Posted by {{username}} <i class="pi pi-star" style="font-size: 1.0em"></i>{{points}} points</p>
</div>
</p-header>
<div class="chart-container" style="position: relative; width: 325px !important;">
<canvas id="{{pollKey}}"></canvas>
<div class="checkbox-container" style="position: absolute; top: 32px; left: 300px;">
<input *ngFor="let choice of inputChoices" class="checkbox" type="checkbox" (click)="vote(choice.id)" id="{{choice.id}}" style="display: block; cursor: pointer;">
</div>
</div>
<p-footer>
<div style="text-align: left;
background-color: rgba(19, 32, 98, 0.7);
width: fit-content;
padding: 5px;
color: white;
border-radius: 20px;
font-size: 8px;
font-weight: 600;">
{{getScoringType()}}
</div>
</p-footer>
</p-card>
}
输入您的猜测(0退出):20 你的猜测太低了
输入您的猜测(0退出):35 你的猜测太低了
输入您的猜测(0退出):80 你的猜测太高了
输入您的猜测(0退出):74 你的猜测太高了
输入您的猜测(0退出):56 对!猜猜:5
再次播放(y / n)?
答案 0 :(得分:2)
评论对:
answer = generator.nextInt(100)+1;
public static void main
外部创建一个单独的方法。这是常见的做法,这也使您可以再次调用自身的方法。这就是我要解决的方法:
import java.util.Random; import java.util.Scanner;
class Main {
public static Scanner scan = new Scanner(System.in);
public static void main (String[]args) {
runGame();
}
public static void runGame() {
String another = "";
int answer, guess,attemptsNum = 0;
final int maxAttempts = 5;
Random generator = new Random();
answer = generator.nextInt(100)+1;
System.out.println("Guess a number between 1 and 100");
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
while (guess != answer && attemptsNum < maxAttempts-1 && guess != 0 ) {
if(guess==answer)
System.out.println("Right!");
else if (guess<answer) {
System.out.println("Your guess was too LOW.");
attemptsNum++;
guess = scan.nextInt();
}
else {
System.out.println("Your guess was too HIGH.");
attemptsNum++;
guess = scan.nextInt();
}
}
System.out.println ("The number was " + answer);
System.out.println("Want to Play again?(y/n)");
another = scan.next();
System.out.println("another = " + another);
if(another.equals("y")) {
runGame();
} else {
System.out.println("Goodbye!");
}
}
}
但是无论哪种方式,只要继续练习即可。您会掌握的!
答案 1 :(得分:1)
我看到外部的while循环需要重新构造,并且某些逻辑放错了位置。试用修订版,希望对您有所帮助。如果您有任何问题,请评论我。
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int answer, guess, attemptsNum = 0;
final int maxAttempts = 5;
String str, another = "y";
Scanner scan = new Scanner(System.in);
Random generator = new Random();
answer = generator.nextInt(101) + 1;
while (another.equals("y") || another.equals("Y")) {
System.out.println("Guess a number between 1 and 100");
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
attemptsNum = 0;
while (guess != 0)
{
attemptsNum++;
if (guess == answer) {
System.out.println("Right!");
break;
} else if (guess < answer)
System.out.println("Your guess was too LOW.");
else if (guess > answer)
System.out.println("Your guess was too HIGH.");
if (attemptsNum == maxAttempts) {
System.out.println("The number was " + answer);
break;
}
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
}
System.out.println("Want to Play again?(y/n)");
another = scan.next();
}
System.out.println("Thanks for playing");
}
}
答案 2 :(得分:0)
我可以看到javapedia.net首先回答了这个问题,但是我独立提出了这个答案。我添加了括号以提高可读性。 letsNum ++被添加到if语句中,以使猜测值过高或过低。
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int answer, guess, attemptsNum = 0;
final int maxAttempts = 5;
String str, another = "y";
Scanner scan = new Scanner(System.in);
Random generator = new Random();
answer = generator.nextInt(101) + 1;
System.out.println("Guess a number between 1 and 100");
System.out.println("Enter your guess (0 to quit):");
guess = scan.nextInt();
while (guess != 0) {
if (guess == answer) {
System.out.println("Right!");
guess = 0;
} else if (guess < answer) {
System.out.println("Your guess was too LOW.");
attemptsNum++;
guess = scan.nextInt();
} else {
System.out.println("Your guess was too HIGH.");
attemptsNum++;
guess = scan.nextInt();
}
if (attemptsNum >= (maxAttempts-1)) {
System.out.println("The number was " + answer);
}
}
}
}