将While循环纳入程序

时间:2016-11-14 03:33:31

标签: java while-loop

我正在创建一个程序,允许您玩类似于纸张,剪刀摇滚的游戏。

游戏有效,但我试图在代码中加入一个循环,询问用户是否希望继续播放。

如果是,它会要求他们提供另一个输入。 如果不是,该程序将简单地声明"感谢您玩#34;

以下是代码:

 import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
    public static void main(String[]args)
    {

    String yourMove;
    String compMove;


    int compInt;





    String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};

    Scanner input = new Scanner(System.in);
    Random rand = new Random(6);

    compInt = rand.nextInt(5)+1;

    if (compInt == 1) 
       compMove = "Monkey"; 
    else if (compInt == 2) 
       compMove = "Robot"; 
    else if (compInt == 3) 
       compMove = "Pirate"; 
    else if (compInt == 4)
        compMove = "Ninja";
    else if (compInt == 5)
        compMove = "Zombie";




    System.out.println("What do you choose?: "); 
    yourMove = input.next();
    //MONKEY
    if(yourMove == "Monkey" || compInt == 1)
        System.out.println("Tie");
    else if (yourMove== "Monkey" || compInt == 2)
        System.out.println("You Win! Monkey Unplugs Robot!");
    else if (yourMove=="Monkey" || compInt == 3)
        System.out.println("You Lose! Pirate Skewers Monkey!");
    else if (yourMove == "Monkey" || compInt==4)
        System.out.println("You Win! Monkey fools Ninja!");
    else if (yourMove== "Monkey" || compInt==5)
        System.out.println("You Lose! Zombie savages monkey!");

    //RoBOT
    else if(yourMove == "Robot" || compInt == 2)
        System.out.println("Tie");
    else if (yourMove== "Robot" || compInt == 1)
        System.out.println("You Lose! Monkey Unplugs Robot!");
    else if (yourMove=="Robot" || compInt == 3)
        System.out.println("You Lose! Pirate Drowns Robot!!");
    else if (yourMove == "Robot" || compInt==4)
        System.out.println("You Win! Robot Chokes Ninja");
    else if (yourMove== "Robot" || compInt==5)
        System.out.println("You win! Robot Crushes Zombie!");

    //PIRATE
    else if(yourMove == "Pirate" || compInt == 3)
        System.out.println("Tie");
    else if (yourMove== "Pirate" || compInt == 1)
        System.out.println("You Win! Pirate Skewers Monkey!");
    else if (yourMove=="Pirate" || compInt == 2)
        System.out.println("You Win! Pirate Drowns Robot!");
    else if (yourMove == "Pirate" || compInt==4)
        System.out.println("You Lose! Ninja Karate Chops Pirate!");
    else if (yourMove== "Pirate" || compInt==5)
        System.out.println("You Lose! Zombie Eats Pirate!");

    //NINJA
    else if(yourMove == "Ninja" || compInt == 4)
        System.out.println("Tie");
    else if (yourMove== "Ninja" || compInt == 1)
        System.out.println("You Lose! Monkey Fools Ninja!");
    else if (yourMove=="Ninja" || compInt == 2)
        System.out.println("You Lose! Robot Chokes Ninja!");
    else if (yourMove == "Ninja" || compInt==3)
        System.out.println("You Win! Ninja Karate Chops Pirate!");
    else if (yourMove== "Ninja" || compInt==5)
        System.out.println("You Win! Ninja Decapitates Zombie!");

    //ZOMBIE
    else if(yourMove == "Zombie" || compInt == 5)
        System.out.println("Tie");
    else if (yourMove== "Zombie" || compInt == 1)
        System.out.println("You Win! Zombie Savages Monkey!");
    else if (yourMove=="Zombie" || compInt == 2)
        System.out.println("You Lose! Robot Crushes Zombie!");
    else if (yourMove == "Zombie" || compInt==3)
        System.out.println("You Win! Zombie Eats Pirate!");
    else if (yourMove== "Zombie" || compInt==4)
        System.out.println("You Lose! Ninja Decapitates Zombie!");
    }
}

3 个答案:

答案 0 :(得分:1)

您可以将整个逻辑放在 do-while 循环中。如果输入的字符是==' y'或者' Y'。伪代码应为:

char choice='n';
do
{ 
    < Insert Game logic here >

    System.out.println("Do you wanna continue? Enter y or Y for Yes")'
    choice = <obtain input using Scanner here>;
} 
while(choice=='y'||choice=='Y');

System.out.println("Thanks for Playing");

答案 1 :(得分:0)

你可以这样做:

import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
    public static void main(String[]args)
    {
        String playAgain; 
    do{
        String yourMove;
        String compMove;


        int compInt;





        String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};

        Scanner input = new Scanner(System.in);
        Random rand = new Random(6);

        compInt = rand.nextInt(5)+1;

        if (compInt == 1) 
           compMove = "Monkey"; 
        else if (compInt == 2) 
           compMove = "Robot"; 
        else if (compInt == 3) 
           compMove = "Pirate"; 
        else if (compInt == 4)
            compMove = "Ninja";
        else if (compInt == 5)
            compMove = "Zombie";




        System.out.println("What do you choose?: "); 
        yourMove = input.next();
        //MONKEY
        if(yourMove == "Monkey" || compInt == 1)
            System.out.println("Tie");
        else if (yourMove== "Monkey" || compInt == 2)
            System.out.println("You Win! Monkey Unplugs Robot!");
        else if (yourMove=="Monkey" || compInt == 3)
            System.out.println("You Lose! Pirate Skewers Monkey!");
        else if (yourMove == "Monkey" || compInt==4)
            System.out.println("You Win! Monkey fools Ninja!");
        else if (yourMove== "Monkey" || compInt==5)
            System.out.println("You Lose! Zombie savages monkey!");

        //RoBOT
        else if(yourMove == "Robot" || compInt == 2)
            System.out.println("Tie");
        else if (yourMove== "Robot" || compInt == 1)
            System.out.println("You Lose! Monkey Unplugs Robot!");
        else if (yourMove=="Robot" || compInt == 3)
            System.out.println("You Lose! Pirate Drowns Robot!!");
        else if (yourMove == "Robot" || compInt==4)
            System.out.println("You Win! Robot Chokes Ninja");
        else if (yourMove== "Robot" || compInt==5)
            System.out.println("You win! Robot Crushes Zombie!");

        //PIRATE
        else if(yourMove == "Pirate" || compInt == 3)
            System.out.println("Tie");
        else if (yourMove== "Pirate" || compInt == 1)
            System.out.println("You Win! Pirate Skewers Monkey!");
        else if (yourMove=="Pirate" || compInt == 2)
            System.out.println("You Win! Pirate Drowns Robot!");
        else if (yourMove == "Pirate" || compInt==4)
            System.out.println("You Lose! Ninja Karate Chops Pirate!");
        else if (yourMove== "Pirate" || compInt==5)
            System.out.println("You Lose! Zombie Eats Pirate!");

        //NINJA
        else if(yourMove == "Ninja" || compInt == 4)
            System.out.println("Tie");
        else if (yourMove== "Ninja" || compInt == 1)
            System.out.println("You Lose! Monkey Fools Ninja!");
        else if (yourMove=="Ninja" || compInt == 2)
            System.out.println("You Lose! Robot Chokes Ninja!");
        else if (yourMove == "Ninja" || compInt==3)
            System.out.println("You Win! Ninja Karate Chops Pirate!");
        else if (yourMove== "Ninja" || compInt==5)
            System.out.println("You Win! Ninja Decapitates Zombie!");

        //ZOMBIE
        else if(yourMove == "Zombie" || compInt == 5)
            System.out.println("Tie");
        else if (yourMove== "Zombie" || compInt == 1)
            System.out.println("You Win! Zombie Savages Monkey!");
        else if (yourMove=="Zombie" || compInt == 2)
            System.out.println("You Lose! Robot Crushes Zombie!");
        else if (yourMove == "Zombie" || compInt==3)
            System.out.println("You Win! Zombie Eats Pirate!");
        else if (yourMove== "Zombie" || compInt==4)
            System.out.println("You Lose! Ninja Decapitates Zombie!");

        System.out.println("Would you like to play again? Type yes to play again."); 
        playAgain = input.next();
        } while(playAgain.equals("yes"));
    }

}

答案 2 :(得分:0)

嗯......我们在这里说实话。你的代码实际上不起作用。我的意思是,是的,它运行但它绝对不会正确地进行游戏。

用于计算机游戏角色选择的随机生成器将无法正常工作。你需要这样做:

Random rand = new Random();
compInt = rand.nextInt(5)+1;

将rand初始化保留为6。

设置所有IF和IF / ELSE语句的条件中的逻辑,以便任何用户输入检查都不会超过MONKEY游戏角色,这是因为您使用OR(||)运算符而不是AND(&amp;&amp;)运算符。你设置的方式,如果计算机选择ROBOT并且用户选择ZOMBIE然后包含在其中的条件:

else if (yourMove == "Monkey" || compInt == 2) {
    System.out.println("You Win! Monkey Unplugs Robot!");
}

将始终为true并显示消息:

You Win! Monkey Unplugs Robot!

即使用户输入了Zombie。记住......条件基本上设置为或者哪个是你不想要的。您希望两个条件都为真,因此您需要使用AND(&amp;&amp;)运算符。

除此之外:

yourMove == "Monkey"

总会给出意想不到的结果,而这里的why。请改用String.equals()方法,例如:

yourMove.equals("Monkey")

您将字符变量声明为字符串数组,但您没有使用该数组。为什么甚至打扰。只需将字符变量设置为以空格分隔的字符串,并利用它来查看用户是否实际在游戏中提供了一个字符,例如:

String characters = "Monkey Robot Pirate Ninja Zombie";

yourMove = "";
while (yourMove.equals("")) {
    System.out.println("What do you choose? --> "); 
    yourMove = input.nextLine().toLowerCase();
    if (yourMove.equals("quit")) { 
        System.out.println("Thanks for playing. Bye Bye"); 
        System.exit(0); 
    }
    if (!characters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
        System.out.println("You entered an invalid Game Character! Try again...\n"); 
        continue;
    }
}

无论如何,这是一个完整的工作游戏:

package okekpejmoropinzee;

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

public class OkekpeJMoropinzee {
     static boolean playAgain = true;

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


        while (playAgain == true) {
            playGame(input);
        }

        System.out.println("Thanks for playing");
        input.close();
    }

    private static void playGame(Scanner input) {
        String yourMove = "", compMove = "";
        int compInt = 0;
        String gameCharacters = "Monkey Robot Pirate Ninja Zombie";

        Random rand = new Random();
        compInt = rand.nextInt(5)+1;

        if (compInt == 1) { compMove = "Monkey"; }
        else if (compInt == 2) { compMove = "Robot"; }
        else if (compInt == 3) { compMove = "Pirate"; }
        else if (compInt == 4) { compMove = "Ninja"; }
        else if (compInt == 5) { compMove = "Zombie"; }
        System.out.println(compMove + " --- " + compInt);

        yourMove = "";
        while (yourMove.equals("")) {
            System.out.println("What do you choose? --> "); 
            yourMove = input.nextLine().toLowerCase();
            if (yourMove.equals("quit")) { 
                System.out.println("Thanks for playing. Bye Bye"); 
                System.exit(0); 
            }
            if (!gameCharacters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
                System.out.println("You entered an invalid Game Character! Try again...\n"); 
                continue;
            }
        }

        //MONKEY
        if (yourMove.equals("monkey") && compInt == 1) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("monkey") && compInt == 2) { 
            System.out.println("You Win! Monkey Unplugs Robot!"); 
        }
        else if (yourMove.equals("monkey") && compInt == 3) { 
            System.out.println("You Lose! Pirate Skewers Monkey!"); 
        }
        else if (yourMove.equals("monkey") && compInt == 4) { 
            System.out.println("You Win! Monkey fools Ninja!"); 
        }
        else if (yourMove.equals("monkey") && compInt == 5) { 
            System.out.println("You Lose! Zombie savages monkey!"); 
        }

        //ROBOT
        else if (yourMove.equals("robot") && compInt == 2) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("robot") && compInt == 1) { 
            System.out.println("You Lose! Monkey Unplugs Robot!"); 
        }
        else if (yourMove.equals("robot") && compInt == 3) { 
            System.out.println("You Lose! Pirate Drowns Robot!!"); 
        }
        else if (yourMove.equals("robot") && compInt == 4) { 
            System.out.println("You Win! Robot Chokes Ninja"); 
        }
        else if (yourMove.equals("robot") && compInt == 5) { 
            System.out.println("You win! Robot Crushes Zombie!"); 
        }

        //PIRATE
        else if (yourMove.equals("pirate") && compInt == 3) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("pirate") && compInt == 1) { 
            System.out.println("You Win! Pirate Skewers Monkey!"); 
        }
        else if (yourMove.equals("pirate") && compInt == 2) { 
            System.out.println("You Win! Pirate Drowns Robot!"); 
        }
        else if (yourMove.equals("pirate") && compInt == 4) { 
            System.out.println("You Lose! Ninja Karate Chops Pirate!"); 
        }
        else if (yourMove.equals("pirate") && compInt == 5) { 
            System.out.println("You Lose! Zombie Eats Pirate!"); 
        }

        //NINJA
        else if(yourMove.equals("ninja") && compInt == 4) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("ninja") && compInt == 1) { 
            System.out.println("You Lose! Monkey Fools Ninja!"); 
        }
        else if (yourMove.equals("ninja") && compInt == 2) { 
            System.out.println("You Lose! Robot Chokes Ninja!"); 
        }
        else if (yourMove.equals("ninja") && compInt == 3) { 
            System.out.println("You Win! Ninja Karate Chops Pirate!"); 
        }
        else if (yourMove.equals("ninja") && compInt == 5) { 
            System.out.println("You Win! Ninja Decapitates Zombie!"); 
        }

        //ZOMBIE
        else if(yourMove.equals("zombie") && compInt == 5) { 
            System.out.println("Tie"); 
        }
        else if (yourMove.equals("zombie") && compInt == 1) { 
            System.out.println("You Win! Zombie Savages Monkey!"); 
        }
        else if (yourMove.equals("zombie") && compInt == 2) { 
            System.out.println("You Lose! Robot Crushes Zombie!"); 
        }
        else if (yourMove.equals("zombie") && compInt==3) { 
            System.out.println("You Win! Zombie Eats Pirate!"); 
        }
        else if (yourMove.equals("zombie") && compInt==4) { 
            System.out.println("You Lose! Ninja Decapitates Zombie!"); 
        }

        //Ask if User wants to play the game again...
        String playMore = "";
        while (!playMore.equals("y") && !playMore.equals("n")) {
            System.out.println("\nDo you want to play another game? (y/n) ");
            playMore = input.nextLine().toLowerCase();
        }
        if (playMore.equals("n")) { playAgain = false; }
    }
}