简体   繁体   English

Java:剪刀石头布游戏循环

[英]Java: Rock Paper Scissors game loop

I'm trying to get this loop to work, but can't get it figured out, tried a few different kinds and haven't had any luck, gone back through some of my studying and poked around to try to get some insight but haven't been able to successfully get it to work.我试图让这个循环工作,但无法弄清楚,尝试了几种不同的方法,但没有任何运气,回顾了我的一些学习并四处寻找,试图获得一些见解,但是还没有能够成功地让它工作。 the base program code is as follow, basically this was a project i did a few weeks ago, and a new project wants us to go back in and have it so the game continuously plays until the user inputs a "3".基本程序代码如下,基本上这是我几周前做的一个项目,一个新项目希望我们回去拥有它,这样游戏就会一直玩,直到用户输入“3”。 I can't figure it out, I can't seem to find any examples or help online.我想不通,我似乎无法在网上找到任何示例或帮助。 I'm not looking for someone to just give an answer, just looking for a nudge in the right direction.我不是在找人来回答,只是在寻找正确方向的推动。

TL;DR: the game should repeat until the user inputs 3 TL;DR:游戏应该重复直到用户输入 3

import java.util.Scanner;

public class Main {

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

        System.out.print("scissor (0), rock (1), paper (2): ");
        int user = input.nextInt();

        int computer = (int) (Math.random() * 3);

        System.out.print("The Computer is ");
        switch (computer) {
            case 0:
                System.out.print("scissor. ");
                break;
            case 1:
                System.out.print("rock. ");
                break;
            case 2:
                System.out.print("paper. ");
        }
        System.out.print(" You are ");
        switch (user) {
            case 0:
                System.out.print("scissor");
                break;
            case 1:
                System.out.print("rock");
                break;
            case 2:
                System.out.print("paper");
        }

        if (computer == user) {
            System.out.println(" too.  It is a draw");
        } else {
            boolean win = (user == 0 && computer == 2)
                    || (user == 1 && computer == 0)
                    || (user == 2 && computer == 1);
            if (win) {
                System.out.println(". You won!");
            } else {
                System.out.println(". You lose!");
            }

        }
    }
}

Your code has no loops at all, though.但是,您的代码根本没有循环。

You can use the while construct, or the do/while construct, which is quite similar:您可以使用 while 构造或 do/while 构造,它们非常相似:

boolean playing = true;

while (playing) {
    ... all the code you currently have ....
}

would keep looping;会一直循环; until you set playing to false, of course, which you can do when the user enters 3 .直到您将playing设置为 false,当然,您可以在用户输入3时执行此操作。

You can put all your code in your main method into an infinite loop and exit the program when the user inputs 3 like this.您可以将 main 方法中的所有代码放入一个无限循环中,并在用户这样输入 3 时退出程序。

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    while(true) { //start of the loop

        Scanner input = new Scanner(System.in);

        //setting the variable to an incorrect value, 
        //so the text is printed always at least once
        int user = -1;

        //while the input is incorrect (lower than 0 or higher than 3)
        while(user < 0 || user > 3) {

            //ask for the input
            System.out.print("scissor (0), rock (1), paper (2), exit (3): ");

            //try reading an integer, as the user might input whatever (String, float,..)
            try {
                user = input.nextInt(); //trying to read an integer
            } catch (Exception e) { //in case of an invalid input (not an integer)
                //I still want to "read" the tokens, 
                //because the .nextInt() did not process the input
                input.next();
            }
            if (user == 3) System.exit(0);
        }

        //rest of your code

    } //end of the loop
}

You can see, that I used try and catch to check for other inputs than an integer.您可以看到,我使用trycatch来检查除整数之外的其他输入。 I also repeat asking for the input until it is valid.我也重复要求输入,直到它有效。 You might not necessarily need that if it is not part of your focus right now and exchange it just for the following.如果它现在不是您关注的一部分,您可能不一定需要它,并将其交换为以下内容。

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    while(true) { //start of the loop, loops forever unless the user inputs 3

        Scanner input = new Scanner(System.in);

        System.out.print("scissor (0), rock (1), paper (2): ");
        int user = input.nextInt(); //trying to read an integer
        if (user == 3) System.exit(0); //if the input is 3, exit the program

        //rest of your code

    } //end of the loop
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM