Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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程序上的while循环吗?该程序用于使用整数用户输入的石头、布、剪刀游戏_Java_Arrays_Random_While Loop_Credentials - Fatal编程技术网

需要帮助结束一个java程序上的while循环吗?该程序用于使用整数用户输入的石头、布、剪刀游戏

需要帮助结束一个java程序上的while循环吗?该程序用于使用整数用户输入的石头、布、剪刀游戏,java,arrays,random,while-loop,credentials,Java,Arrays,Random,While Loop,Credentials,我和我的团队决定为CSIS 1400的学期末项目编写一个关于石头、纸、剪刀的游戏的java程序,并将其大部分编写出来,但不确定如何使用整数用户输入终止while循环。我们希望用户键入0并使循环停止。我们试图使用do while循环,但必须在循环外部初始化pChoice,这会干扰程序的功能 以下是我们目前掌握的代码: package RPS; import java.util.Scanner; import java.util.Random; //import java.io.Console;

我和我的团队决定为CSIS 1400的学期末项目编写一个关于石头、纸、剪刀的游戏的java程序,并将其大部分编写出来,但不确定如何使用整数用户输入终止while循环。我们希望用户键入0并使循环停止。我们试图使用do while循环,但必须在循环外部初始化pChoice,这会干扰程序的功能

以下是我们目前掌握的代码:

package RPS;

import java.util.Scanner;
import java.util.Random;
//import java.io.Console;
public class Game 
{

public static void main (String[] args) 
{

String username;
String password;

System.out.print ("Username: ");
username = Security.user();

System.out.print ("Password: ");
password = Security.pswd ();

//Console console = System.console ();
//password = new String (console.readPassword ("Password: ")); 

if ( username.equals (Security.jacobUser  ()) && password.equals (Security.jacobPswd  ()) || 
 username.equals (Security.tuckerUser ()) && password.equals (Security.tuckerPswd ()) || 
 username.equals (Security.austinUser ()) && password.equals (Security.austinPswd ())  )
{

Scanner input = new Scanner(System.in);
Random randRoll = new Random();

int choiceArray[] = {1, 2, 3};

System.out.println ("Hello, Welcome to Rock-Paper-Scissors!");
System.out.println ("Here are your choices:");
System.out.printf ("%d for Rock \n", choiceArray[0]);
System.out.printf ("%d for Paper \n", choiceArray[1]);
System.out.printf ("%d for Scissors \n", choiceArray[2]);

String doPlay = "yes";
String dontPlay = "no";
String playChoice;
int choice;

System.out.println ("Do you want to play?");
playChoice = input.nextLine ();

if (playChoice.equals (dontPlay))
{
System.out.println ("Program Terminated");
}

int cChoice;
int pChoice;

int cScore = 0, pScore = 0, tie = 0, rounds = 0;

    while (playChoice.equals (doPlay) || Game.done ().equals (dontPlay))
        {

            System.out.println ("Please make a selection, or use 0 to end the game:");
            cChoice = randRoll.nextInt (3) + 1;
            pChoice = input.nextInt ();

            if (pChoice == choiceArray[0] || pChoice == choiceArray[1] || pChoice == choiceArray[2])
            {



            if (pChoice == cChoice)
                { 
                    System.out.println ("Tie Game!");
                    System.out.println ();
                    tie++;
                    rounds++;
                } 

            else
                {
                    if (cChoice==1 && pChoice==3)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==1 && pChoice==2)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==3)
                        {
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==1) 
                        { 
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==1)  
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==2) 
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        }
                }

                System.out.println ("Scores: " + rounds +" rounds:");
                System.out.println ("You\tComputer\tTies");
                System.out.println (" "+ pScore +"\t   " + cScore + "\t\t " + tie);
            }
        }
}
else
{
System.out.println ("Incorrect Credentials");
}
}
}
所有控制台内容都在注释中,因为netbeans在运行时会抛出一个空指针异常,但它在命令行中工作得很好,我们现在只想继续在IDE中工作,直到完成为止。如果playChoice=dontPlay,我们还希望它终止

任何帮助都将不胜感激,谢谢

以下是我们完成的源代码:

package RPS;

import java.util.Scanner;
import java.util.Random;
import java.io.Console;
public class Game 
{

public static void main (String[] args) 
{

System.out.println ("Credentials required.");

String username;
String password;

System.out.print ("Username: ");
username = Security.user();

Console console = System.console ();
password = new String (console.readPassword ("Password: ")); 

if ( username.equals (Security.jacobUser  ()) && password.equals (Security.jacobPswd  ()) || 
 username.equals (Security.tuckerUser ()) && password.equals (Security.tuckerPswd ()) || 
 username.equals (Security.austinUser ()) && password.equals (Security.austinPswd ())  )
{

Random randRoll = new Random();
Scanner input = new Scanner (System.in);   

int choiceArray[] = {1, 2, 3};

System.out.println ("Hello, Welcome to Rock-Paper-Scissors!");
System.out.println ("Here are your choices:");
System.out.printf ("%d for Rock \n", choiceArray[0]);
System.out.printf ("%d for Paper \n", choiceArray[1]);
System.out.printf ("%d for Scissors \n", choiceArray[2]);

int cChoice;
int pChoice;

int cScore = 0, pScore = 0, tie = 0, rounds = 0;


    do
        {

            System.out.println ("Please make a selection, or use 0 to end the game:");
            cChoice = randRoll.nextInt (3) + 1;
            pChoice = input.nextInt ();

            if (pChoice == choiceArray[0] || pChoice == choiceArray[1] || pChoice == choiceArray[2])
            {



            if (pChoice == cChoice)
                { 
                    System.out.println ("Tie Game!");
                    System.out.println ();
                    tie++;
                    rounds++;
                } 

            else
                {
                    if (cChoice==1 && pChoice==3)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==1 && pChoice==2)
                        {
                            System.out.println ("Computer picked Rock!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==3)
                        {
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==2 && pChoice==1) 
                        { 
                            System.out.println ("Computer picked Paper!");
                            System.out.println ("Paper beats Rock!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==1)  
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Rock beats Scissors!");
                            System.out.println ("**Player Wins!**");
                            System.out.println ();
                            pScore++;
                            rounds++;
                        } 

                    if (cChoice==3 && pChoice==2) 
                        {
                            System.out.println ("Computer picked Scissors!");
                            System.out.println ("Scissors beats Paper!");
                            System.out.println ("**Computer Wins!**");
                            System.out.println ();
                            cScore++;
                            rounds++;
                        }
                }
            }
        }while (pChoice != 0); 

        System.out.println ("Here are the final standings.");
        System.out.println ("Rounds: " + rounds);
        System.out.println ("You\tComputer\tTies");
        System.out.println (" "+ pScore +"\t   " + cScore + "\t\t " + tie);
}
else
{
System.out.println ("Incorrect Credentials");
}
    }
} 
安全等级:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package RPS;

import java.util.Scanner;

/**
 *
 * @author Jacob, Tucker, Austin
 */
public class Security {

public static String user ()
{
    Scanner input = new Scanner (System.in);
    String username = input.nextLine ();
    return username;
}

public static String pswd ()
{
    Scanner input = new Scanner (System.in);
    String password = input.nextLine ();
    return password;
}

public static String jacobUser ()
{
    String username = "jacob";
    return username;
}

public static String jacobPswd ()
{
    String password = "password";
    return password;
}

public static String tuckerUser ()
{
    String username = "tucker";
    return username;
}

public static String tuckerPswd ()
{
    String password = "password";
    return password;
}

public static String austinUser ()
{
    String username = "austin";
    return username;
}

public static String austinPswd ()
{
    String password = "password";
    return password;
}
}

我们现在在使用javac时遇到编译错误。下面的评论中列出了详细信息

使用任何你觉得最容易的循环。但是把一个
中断内部命令。大概是

if (pChoice == 0) {
    break;
}

这将导致执行从循环中退出。

您可以将pchoice初始化为一个不会影响游戏的数字,以便使用while循环


另一种可能性是在循环之前初始化一个布尔变量,如果pchoice==0,该循环将被更改。

在处理可重复的用户输入时,实现Do While循环通常是最好的方法

    do {

        System.out.println ("Please make a selection, or use 0 to end the game:");
        cChoice = randRoll.nextInt (3) + 1;
        pChoice = input.nextInt ();

        if (pChoice == choiceArray[0] || pChoice == choiceArray[1] || pChoice == choiceArray[2])
        {
            ...
            // All your normal game stuff
            ...
        }
    }while(pChoice != 0)

pChoice在第一次通过循环时初始化

您不需要将playChoice作为循环条件的一部分进行测试。它只用于初始化,如果他们不想玩,可以使用if语句退出

您可以调用game.done()来测试它们是否完成了,但不显示该方法的作用。如果game.done()将输入的整数与1、2或3一起测试,则将终止循环。由于您的玩家在游戏逻辑之前输入该数字,因此您还需要一个if语句来确定他们是否选择在输入后但在游戏逻辑之前结束游戏。类似于
if(!(game.done().equals(dontPlay))
这样,只有当他们输入另一个选项时,游戏逻辑才会执行;这将跳过所有的游戏逻辑,当他们说他们想停止,然后退出循环的底部


祝你好运

而不是半途而废。这将有助于我们在必须在循环外初始化pChoice时使用do,这将干扰问题中所述的程序功能。我们希望循环在用户选择石头、布或剪刀时继续执行,并根据玩家或计算机是否获胜或两者是否平局来增加分数。这将要求在循环内部初始化pChoice。为什么在do while循环外部初始化pChoice会影响程序的功能?抱歉,当我尝试实现do while循环时,我没有删除初始while(playChoice.equals(doPlay))这使得最后的while循环认为pChoice没有初始化。这就是为什么我认为我必须在循环之外初始化pChoice。我已经完成了我的源代码,但当我运行javac时,它不接受我的方法调用,但netbeans接受。它说找不到符号变量安全性。我无法通过netbeans运行它,因为console.readPassword只能在命令行中运行。Netbeans出现空指针异常错误。是否使用!input.equalsIgnoreCase在将我的整数强制转换为布尔值,然后将其分配给新的布尔变量之后?如果您指的是在循环之前使用布尔变量,则在(continuePlaying)
期间它看起来像
,然后在用户输入之后,您只需执行
if(pchoice==0){continuePlaying=false;}
if(playtchoice==“dontPlay.equalsIgnoreCase()){continueplay=false;}
但是像其他人建议的那样使用do-while循环可能更干净、更简单。
if(playtchoice==“dontPlay.equalsIgnoreCase())
。。。你确定吗,@yitzih?oops`(如果是playChoice.equalsIgnoreCase(“dontPlay”)“我的错,谢谢@davidwallace这是有意义的。我不知道break会在while循环中工作。我只在switch语句中使用过它。