需要在一个循环中获取整个内容,哪个循环最适合?我应该在哪里应用它?

Need to get the entire thing in a loop, which loop is best suited? Where should I apply it?

我的 Java 项目即将完成。 objective是用户和电脑玩剪刀石头布的游戏。计算机生成一个随机数,该数字必须与三个选项中的任何一个相关联。我把它弄到了可以成功玩的地方,但这里有一个问题,在平局的情况下,游戏将重复并重新开始,否则将终止。我有它终止的地方,但无法弄清楚如何让它在领带的情况下重复整个过程。这些方法必须保持原样,但我的教授说答案是如果整个事情都在循环中。我的问题是我应该使用哪个循环以及它应该放在哪里?这是代码:

public class FahrDylanRockPaperScissors{
public static void main (String [] args){
    String computer = computerChoice();
    String user = userChoice();
    determineWinner(computer, user);

}
public static String computerChoice( ){
    Random rand = new Random();
     int cinput = rand.nextInt(3)+ 1;
     String computer = "thing";
    if (cinput == 1)
    computer = "Rock";
    if (cinput == 2)
    computer = "Paper";
    if (cinput == 3)
    computer = "Scissors";
    return computer;
}
public static String userChoice(){
    Scanner sc = new Scanner (System.in);
    String user = "default";
    do{
        System.out.println ("Let's Play a game! Rock, Paper, Scissors!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + "\nPlease enter either Rock, Paper, or Scissors: " + "\nGood Luck!");
        user = sc.nextLine();
    }

    while (isValidChoice (user) == false);
    return user;
}
public static boolean isValidChoice(String choice){
    boolean status;
    if (choice.compareTo("Rock")== 0)
        status = true;
    else if (choice.compareTo("Paper")== 0)
        status = true;
    else if (choice.compareTo("Scissors")== 0)
        status = true;

    else{
        status = false;
        System.out.println("Error! Make sure you are capitalizing your choices");
    }

    return status;
}
public static void determineWinner(String computer, String user){
    System.out.println (" Computer Choice: " + computer);
    System.out.println ("Your Choice : " + user);
    if (computer.compareTo( "Rock" ) == 0 && user.compareTo  ("Scissors") == 0)
    System.out.println (" Computer wins! Better luck next time!");
    if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
    System.out.println (" Computer wins! Better luck next time!");
    if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
    System.out.println (" Computer wins! Better luck next time!");
    if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
    System.out.println (" You win!!");
    if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
    System.out.println (" You win!!");
    if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
    System.out.println (" You win!!");
    else if (computer.compareTo(user) == 0 )
    System.out.println(" Tie! the game must be played again.");
}

}

您可以使用do-while循环,因为您的代码至少需要执行一次。

public static void main (String [] args){
    boolean win = false;
    do{
        String computer = computerChoice();
        String user = userChoice();
        win = determineWinner(computer, user);
    }while(!win);
}

你第一次执行完整的代码。然后检查谓词,如果有人赢了,do-while 就会停止。但是如果win等于false就会再次执行

您可以仅使用 while 循环或其他循环来实现相同的效果。但是因为您的代码至少需要 运行 一次,所以 do-while 很合适。

编辑:

您需要更改代码,以便 determineWinner return 在有人获胜 (return true) 或平局 (return false) 时返回。我在发布答案时没有看到它目前没有 return 类型。

使 determineWinner 方法起作用的简单方法如下。

public static boolean determineWinner(String computer, String user){
    System.out.println (" Computer Choice: " + computer);
    System.out.println ("Your Choice : " + user);
    if (computer.compareTo( "Rock" ) == 0 && user.compareTo  ("Scissors") == 0)
    System.out.println (" Computer wins! Better luck next time!");
    if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
    System.out.println (" Computer wins! Better luck next time!");
    if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
    System.out.println (" Computer wins! Better luck next time!");
    if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
    System.out.println (" You win!!");
    if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
    System.out.println (" You win!!");
    if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
    System.out.println (" You win!!");
    else if (computer.compareTo(user) == 0 ){
        System.out.println(" Tie! the game must be played again.");
        return false;
    }
    return true;
}

以及您的编码风格:

if/else/for... 使用括号 {} 是一种很好的做法,即使您只有一个语句,因为它提高了代码的可读性。

编辑 2:

因为你不能改变一些东西,最简单的方法可能是以下:

public static void main(String[] args){
     boolean tie = true;
        do{
            String computer = computerChoice();
            String user = userChoice();
            tie = (computer.compareTo(user) == 0);
            determineWinner(computer, user);
        }while(tie);
}

即使 determineWinner 确定了获胜者,您也需要它给您反馈。如果您无法获得反馈,只需确定您的 main-方法中是否会出现平局,如果出现平局,请重复。

使用旗帜打平局,像这样:

boolean tie = false;

结合do while:

do {


} while (tie);

如果 tie == true 则继续前进直到它为 false。

do { ... } while () 循环对于这个问题来说是最自然的,但几乎任何循环结构都可以使用。

虽然你说

The methods must remain the way they are

如果 determineWinner() 方法仍然完全,则您无法执行此操作。该方法需要通过某种方式与 main() 沟通是否有平局,否则它必须自己管理 运行 平局时的新游戏。如果我可以自由地这样做,我会让它 return 一个 boolean 值来指示是否有平局。如果它不能被修改为 return 一个值,那么另一种选择是设置一个静态变量来指示是否有平局。或者,您可以使用递归解决此问题,但这不会涉及循环。

您只需制作三行即可:

public static void playGame()
{
      String computer = computerChoice();
       String user = userChoice();
       determineWinner(computer, user);
}

加入一种新方法,例如 playGame()。然后在主函数中,你可以调用playGame(),在determineWinner()的最后,你可以:

else if (computer.compareTo(user) == 0 )
    {
        System.out.println(" Tie! the game must be played again.");
        playGame();
    }

再次玩游戏。

此外,请确保包含随机和扫描仪的导入语句。并确保关闭扫描仪。