java - 将 While 循环合并到程序中

标签 java while-loop

我正在创建一个程序,可以让你玩类似于石头剪刀布的游戏。

游戏可以运行,但我试图在代码中加入一个循环,询问用户是否希望继续玩。

如果是,它会要求他们提供另一个输入。 如果没有,程序将简单地说明“感谢您玩”

代码如下:

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

最佳答案

您可以将整个逻辑放入 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");

关于java - 将 While 循环合并到程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40581509/

相关文章:

java - 即使进程已停止,如何在Linux中找到进程(Java)的端口

java - Bukkit-Plugin 中的位置未更新

c++ - 使用 cin 在一行上读取多个 int

java - 老师坚持使用 if 语句而不是 while 语句,而 with while 语句我的代码可以工作

php - 合并 2 个 while 循环结果

java - 如何访问 Xtext validator 中的上下文信息?

java - Groovy ScriptingEngine 线程安全吗?

java - 测试实现接口(interface)的类

java - 基于 JPA 注释生成 ERD(实体关系图)的工具

c - 从 3 个不同的文件中获取数据并将其打印为 C 语言的输出?