胜利的高低游戏逻辑

时间:2014-11-09 16:27:47

标签: java

失去新手! 请改进它从未显示条件的代码。我需要连续赢得四场胜利才能赢得用户。请帮助!!

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);
            }


        }
    }}}}

1 个答案:

答案 0 :(得分:1)

你的代码有点奇怪和令人困惑,但我花了很多时间来完成它,任何问题随时都可以问:

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);
                }

            }
        }
    }
}

祝你学习顺利!

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

我使用了一个开关块,这是用来代替多个if语句,你可以传入变量然后在行中:你把它想要匹配它的情况,这看起来好多了,是比使用多个if语句更有效。

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

你应该尝试使用你的代码来减少重复文本的数量,我的代码远非完美,但如果是,那么你就没有任何东西需要研究。

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

希望这对你有用。