gpt4 book ai didi

java - Windows 高低游戏逻辑

转载 作者:行者123 更新时间:2023-12-01 12:18:49 25 4
gpt4 key购买 nike

新手迷路了!请改进代码,它永远不会显示条件。我还需要连续四场胜利才能让用户获胜。请帮忙!!

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


public class HiLoGame {
public static void main (String[] args) {

String guess=null;
String high= "high";
String low="low";
String equal = "equal";
int nextCard;
int card=3;
System.out.println("Current card is: "+ card);
if(card==11){
System.out.println ("Which means it is card jack!");
}else if(card==12){
System.out.print("which means it is card queen!");
}else if(card==13){
System.out.println("Which means it is card king!");
}else if (card==14){
System.out.println("Which means it is card Ace!");
}
System.out.println("WELCOME! to High-Low game.");
System.out.println("Guess four times in a row to win.");

while(true){
Random generator = new Random();
nextCard = generator.nextInt(14)+1;
System.out.println("Next card will be high, low or equal?");
Scanner input = new Scanner(System.in);
guess = input.next();
System.out.println("It is --> "+ nextCard);
card = generator.nextInt(14)+2;
nextCard = generator.nextInt(14)+2;
System.out.println("Next card will be high, low or equal?");
Scanner input1 = new Scanner(System.in);
guess = input1.next();
System.out.println("It is --> "+ nextCard);
while(true){
if (guess.equals(high))
{
if (card < nextCard)
{
System.out.println("NICE GUESS ");
System.out.println("KEEP PLAYING");
break;
}
else
{
System.out.println("Sorry WRONG GUESS!");
System.out.println("Better luck next time");
System.exit(0);
}
}
else if (guess.equals(low))
{
if (card > nextCard)
{
System.out.println("NICE GUESS");
System.out.println("KEEP PLAYING");
break;
}
else
{
System.out.println("Sorry WRONG GUESS!");
System.out.println("Better luck next time");
}
}
else if(guess.equals(equal))
{

if (card==nextCard)
{
System.out.println("NICE GUESS");
System.out.println("KEEP PLAYING");
break;
}
else
{
System.out.println("Sorry WRONG GUESS");
System.out.println("Better luck next time!");
System.exit(0);
}


}
}}}}

最佳答案

您的代码有点奇怪和令人困惑,但我已经花时间仔细检查了它,有任何问题请随时提出:

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

public class HiLoGame {

public static void main(String[] args) {
Random generator = new Random();
Scanner input = new Scanner(System.in);
String guess = null, result = null;
Boolean won = false;
int nextCard, card = 3, count = 0;
System.out.println("Current card is: " + card);
switch (card) {
case 11:
System.out.println("Which means it is card jack!");
break;
case 12:
System.out.print("which means it is card queen!");
break;
case 13:
System.out.print("which means it is card king!");
break:
case 14:
System.out.println("Which means it is card Ace!");
break;
}

System.out.println("WELCOME! to High-Low game.\nGuess four times in a row to win.");

while (!won) {

nextCard = generator.nextInt(14) + 1;

System.out.println("You current card is: " + card + "\nWill the next card be high, low or equal?");

guess = input.next().toLowerCase();
System.out.println("The next card is:" + nextCard);

if(card<nextCard){
result = "high";
}
else if(card>nextCard){
result = "low";
}
else if(card==nextCard){
result = "equal";
}

if(guess.equals(result)){
System.out.println("NICE GUESS\nKEEP PLAYING");
card = nextCard;
count++;
}
else {
System.out.println("Sorry WRONG GUESS!\nBetter luck next time");
count=0;
}

if(count==4){
System.out.println("Congratulations, you have beaten the game!!!\nWould you like to play again? Yes/No");
guess = input.next().toLowerCase();
if(guess.equals("yes"))
{
count = 0;
card = generator.nextInt(14) + 1;
}
else{
System.out.println("Thank you for playing, goodbye");
System.exit(0);
}

}
}
}
}

祝你学习顺利!

与相关代码相比,此代码中使用的一些内容是:

我使用了一个 switch block ,它用来代替多个 if 语句,您可以传入变量,然后在行中:如果您输入要与之匹配的内容,这看起来更好并且是比使用多个 if 语句效率更高。

我还在打印行中使用了\n,这是换行符的转义 xhar。您可以使用它来代替多个打印行语句。

您应该尝试在代码中尽量减少重复文本的数量,我的代码远非完美,但如果是这样,您就没有任何需要研究的了。

不要调用多个 if 语句,而是尝试执行一组 if 语句来检查条件,然后提前保存结果,这会改变比较量...

希望这对您有意义。

关于java - Windows 高低游戏逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26830377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com