Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 石头剪刀计划_Java_For Loop_While Loop_Do While - Fatal编程技术网

Java 石头剪刀计划

Java 石头剪刀计划,java,for-loop,while-loop,do-while,Java,For Loop,While Loop,Do While,我对这个代码很失望。目标是创造一个石头剪刀布游戏,最好的3。我曾经尝试过做一个“while”循环,但我完全弄错了,最终还是放弃了我最初使用的非循环代码 如果有人能告诉我如何使它循环,直到计算机或玩家获得3分胜利,我将不胜感激!!多谢各位 抱歉,我需要澄清,它需要循环直到用户或计算机有3个胜利,所以他们会重新调整,直到用户或计算机达到3个胜利 import java.util.Scanner; import java.util.Random; public class RockPaper

我对这个代码很失望。目标是创造一个石头剪刀布游戏,最好的3。我曾经尝试过做一个“while”循环,但我完全弄错了,最终还是放弃了我最初使用的非循环代码

如果有人能告诉我如何使它循环,直到计算机或玩家获得3分胜利,我将不胜感激!!多谢各位 抱歉,我需要澄清,它需要循环直到用户或计算机有3个胜利,所以他们会重新调整,直到用户或计算机达到3个胜利

import java.util.Scanner;
import java.util.Random;
    
public class RockPaperScissors
{

public static void main(String[] args)
{
    Scanner key;
    Random randGen;
    char userChoice, computerChoice;
    int winner, compInt;

    int computerScore;
    int userScore;
    int scoreCounter;

    /***** INITIALIZATION SECTION *****/
    key = new Scanner(System.in);
    randGen = new Random();
    userChoice = ' ';
    computerChoice = ' ';
    //winner = -1;
    compInt = -1;
    computerScore = 0;
    userScore = 0;
    scoreCounter = 0;

    System.out.println("What is your name?");
    String name = key.nextLine();
    System.out.println(name + " play rock, paper, or scissors with me!!");

    /***** INPUT SECTION *****/
    System.out.println("Please enter R for rock, P for paper, or S for scissors:");
    userChoice = key.next().toUpperCase().charAt(0);

    //key.close();

    /***** PROCESSING SECTION *****/
    compInt=randGen.nextInt(3);
    if (compInt == 0)
    {
        computerChoice = 'R';
    }
    else if (compInt == 1)
    {
        computerChoice = 'P';
    }
    else if (compInt == 2)
    {
        computerChoice = 'S';
    }
    
    winner = RockPaperScissors.decideWinner(userChoice,computerChoice);

    /***** OUTPUT SECTION *****/
    System.out.printf("Player: %c%nComputer: %c%n", userChoice, computerChoice);

   switch (winner)
    {
        case 0:
            System.out.println("It's a tie!");
            System.out.println(name + ": " + userScore + ", Computer:" + computerScore );
            break;

        case 1:
            System.out.println("You won!");
            userScore++;
            System.out.println(name + ": " + userScore + ", Computer:" + computerScore );
            break;

        case 2:
            System.out.println("Computer won!");
            computerScore++;
            System.out.println(name + ": " + userScore + ", Computer:" + computerScore );
            break;

        default:
            System.out.println("Invalid choice, try again.");
            break;
    }
    //System.out.println("Thanks for playing!);
}

/***** STATIC METHODS *****/
/**Description: given two characters (R,P, or S) determines winner using rock
 * paper rules. Assume input is valid and error checking is done in main
 * programme. **/

public static int decideWinner(char p1,char p2)
{
    String combo;
    combo = String.format("%c%c", p1, p2);

    switch(combo)
    {
        case "RS":
        case "PR":
        case "SP":
            return 1;

        case "RP":
        case "PS":
        case "SR":
            return 2;

        case "RR":
        case "PP":
        case "SS":
            return 0;

    }
    return -1;
}

}
/***************************************************
 while(userScore == 3)
        {
            System.out.println("You won the game" +name+ "! Congratulations");
            scoreCounter++;
        }
        while(computerScore == 3)
        {
            System.out.println("Sorry " +name+ ", Computer won...");
            scoreCounter++;
        }
 **************************************************/

尝试将代码拆分为每个方法都有自己的任务的方法,这样您就可以更轻松地布局和控制程序的行为。例如,在用户输入的情况下,如果输入无效,您可以递归调用该方法。 在while循环中,我只是添加了一个计数器,在每场比赛后增加,直到3

import java.util.Scanner;
import java.util.Random;
    
public class RockPaperScissors
{

    private static int totalGames = 0;

public static void main(String[] args)
{
    Scanner key;
    Random randGen;
    char userChoice, computerChoice;
    int winner;

    int computerScore;
    int userScore;

    /***** INITIALIZATION SECTION *****/
    key = new Scanner(System.in);
    randGen = new Random();
    computerChoice = ' ';
    //winner = -1;
    computerScore = 0;
    userScore = 0;

    System.out.println("What is your name?");
    String name = key.nextLine();
    System.out.println(name + " play rock, paper, or scissors with me!!");


    while(totalGames < 3) {
        userChoice = getUserChoice(key, false);

        /* PROCESSING SECTION */
        computerChoice = getComputerChoice(computerChoice, randGen.nextInt(3));

        winner = RockPaperScissors.decideWinner(userChoice, computerChoice);

        totalGames++;

        /* OUTPUT SECTION */
        System.out.printf("Player: %c%nComputer: %c%n", userChoice, computerChoice);

        switch (winner) {
            case 0:
                System.out.println("It's a tie!");
                System.out.println(name + ": " + userScore + ", Computer:" + computerScore);
                break;

            case 1:
                System.out.println("You won!");
                userScore++;
                System.out.println(name + ": " + userScore + ", Computer:" + computerScore);
                break;

            case 2:
                System.out.println("Computer won!");
                computerScore++;
                System.out.println(name + ": " + userScore + ", Computer:" + computerScore);
                break;

            default:
                System.out.println("Invalid choice, try again.");
                break;
        }
    }
    //System.out.println("Thanks for playing!);
}

    private static char getComputerChoice(char computerChoice, int compInt) {
        if (compInt == 0)
        {
            computerChoice = 'R';
        }
        else if (compInt == 1)
        {
            computerChoice = 'P';
        }
        else if (compInt == 2)
        {
            computerChoice = 'S';
        }
        return computerChoice;
    }

    private static char getUserChoice(Scanner key, boolean retry) {
        char userChoice;
        /* INPUT SECTION */
        if(retry)
            System.out.println("You entered an invalid input, please try again...");
        System.out.println("Please enter R for rock, P for paper, or S for scissors:");
        try {
            userChoice = key.next().toUpperCase().charAt(0);
            if(userChoice != 'R' && userChoice != 'P' && userChoice != 'S')
                return getUserChoice(key, true);
        } catch (Exception e){
            return getUserChoice(key, true);
        }
        return userChoice;
    }

/***** STATIC METHODS *****/
/**Description: given two characters (R,P, or S) determines winner using rock
 * paper rules. Assume input is valid and error checking is done in main
 * programme. **/

public static int decideWinner(char p1,char p2)
{
    String combo;
    combo = String.format("%c%c", p1, p2);

    switch(combo)
    {
        case "RS":
        case "PR":
        case "SP":
            return 1;

        case "RP":
        case "PS":
        case "SR":
            return 2;

        case "RR":
        case "PP":
        case "SS":
            return 0;

    }
    return -1;
}

}
/***************************************************
 while(userScore == 3)
        {
            System.out.println("You won the game" +name+ "! Congratulations");
            scoreCounter++;
        }
        while(computerScore == 3)
        {
            System.out.println("Sorry " +name+ ", Computer won...");
            scoreCounter++;
        }
 **************************************************/
import java.util.Scanner;
导入java.util.Random;
公营剪纸机
{
私有静态整数totalGames=0;
公共静态void main(字符串[]args)
{
扫描键;
随机兰登;
字符用户选择,计算机选择;
int优胜者;
国际计算机评分;
int用户分数;
/*****初始化部分*****/
键=新扫描仪(System.in);
randGen=新随机数();
计算机选择=“”;
//获胜者=-1;
计算机评分=0;
userScore=0;
System.out.println(“你叫什么名字?”);
字符串名称=key.nextLine();
System.out.println(name+“和我一起玩石头、布或剪刀!!”;
while(totalGames<3){
userChoice=getUserChoice(key,false);
/*加工科*/
computerChoice=getComputerChoice(computerChoice,randGen.nextInt(3));
获胜者=石头剪纸机.decideWinner(用户选择,计算机选择);
totalGames++;
/*输出段*/
System.out.printf(“播放器:%c%n计算机:%c%n”,用户选择,计算机选择);
切换(优胜者){
案例0:
System.out.println(“打成平局了!”);
System.out.println(名称+”:“+userScore+”,计算机:“+computerScore”);
打破
案例1:
System.out.println(“你赢了!”);
userScore++;
System.out.println(名称+”:“+userScore+”,计算机:“+computerScore”);
打破
案例2:
System.out.println(“计算机赢了!”);
计算机评分++;
System.out.println(名称+”:“+userScore+”,计算机:“+computerScore”);
打破
违约:
System.out.println(“无效选择,请重试。”);
打破
}
}
//System.out.println(“感谢您的参与!);
}
私有静态char getComputerChoice(char computerChoice,int compInt){
如果(compInt==0)
{
计算机选择='R';
}
else if(compInt==1)
{
计算机选择='P';
}
else if(compInt==2)
{
计算机选择='S';
}
返回计算机选择;
}
私有静态字符getUserChoice(扫描键,布尔重试){
字符用户选择;
/*输入段*/
如果(重试)
System.out.println(“您输入的输入无效,请重试…”);
System.out.println(“请输入R代表石头,P代表纸,S代表剪刀:”;
试一试{
userChoice=key.next().toUpperCase().charAt(0);
if(userChoice!=“R”&&userChoice!=“P”&&userChoice!=“S”)
返回getUserChoice(key,true);
}捕获(例外e){
返回getUserChoice(key,true);
}
返回用户选择;
}
/*****静态方法*****/
/**描述:给定两个字符(R、P或S)将使用rock确定胜利者
*纸面规则。假设输入有效,并且错误检查在main中完成
*方案**/
公共静态int-decideWinner(字符p1、字符p2)
{
字符串组合;
combo=String.format(“%c%c”,p1,p2);
开关(组合)
{
案例“RS”:
案例“PR”:
案例“SP”:
返回1;
案例“RP”:
案例“PS”:
案例“SR”:
返回2;
案例“RR”:
案例“PP”:
案例“SS”:
返回0;
}
返回-1;
}
}
/***************************************************
while(userScore==3)
{
System.out.println(“你赢得了比赛”+name+“!祝贺你”);
记分计数器++;
}
while(computerScore==3)
{
System.out.println(“对不起”+name+“,计算机赢了…”);
记分计数器++;
}
**************************************************/

我在想,如果你想使用while循环,你只需要记录每个while((playerWins+computerWins)<3){//code}。@OmarAbdelBari如果最好是3,他只需要记录已经玩了多少回合。所以很简单:
while(回合<3){//code}
。您好,欢迎使用stack overflow。如果我的答案解决了您的问题,请将其标记为已接受:)@M0e这不完全正确,如果一轮是平局,则不算满足条件。3场比赛中最好的2场意味着2场胜利。如果有1场或3场平局,谁赢了呢?但这也提高了另一点,如果一名球员赢了2场,那么它就赢了技术上可以在两个非平局回合中结束。所以我想更准确的是while(playerWins<2&&computerWins<2)