我遇到了这个程序的问题。我认为问题出在我的回复声明上。我可以让程序运行起来,直到确定胜利者然后崩溃。我只是一个初学者,任何帮助将不胜感激。我对这个程序有问题
这里是我的RPS类
/*
* 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 game;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Owner
*/
public class rPs {
Scanner in = new Scanner(System.in);
Random random = new Random();
String userChoice = "";
String cpuChoice;
String pickWinner;
String winner;
String tie;
String user;
String computer;
int compRand = 0;
//Get user choice
public String getUserChoice(){
System.out.println("Rock, Paper, or Scissors?:");
userChoice = in.next();
//Get user choice
while(!"rock".equals(userChoice)&& !"paper".equals(userChoice) &&
!userChoice.equals("scissors"))
{
System.out.println("Sorry, " + userChoice + " is not a valid entry");
System.out.println("Rock, Paper, or Scissors?:");
userChoice = in.next();
}//end while for r,p,s check
return userChoice;
}//End Get user
//Get the cpu Choice!!!!
public String getCPUChoice(){
compRand = random.nextInt(3);
if (compRand == 1) {
cpuChoice = "Rock";
}
if (compRand == 2){
cpuChoice = "Paper";
}
else if (compRand == 3){
cpuChoice = "Scissors";
}
return cpuChoice;
}//End Get cpu choice
//Pick a winner by passing 2 strings together
public String pickWinner(String userChoice,String cpuChoice){
//need a loop for ties
if (userChoice.equalsIgnoreCase(cpuChoice)) {
return winner = tie;
}//end tie if...
if (userChoice.equalsIgnoreCase("Rock")&& cpuChoice.equalsIgnoreCase
("scissors"))
{
System.out.println("Computer chooses Scissors. You win!");
return winner = user;
}//end if 1
if (userChoice.equalsIgnoreCase("Rock")&&cpuChoice.equalsIgnoreCase("Paper")){
return winner = computer;
}//end if 2
if (userChoice.equalsIgnoreCase("Paper") && cpuChoice.equals
("Scissors")) {
System.out.println("Computer chooses Scissors. You lose.");
return winner = computer;
}//end if 3
if (userChoice.equalsIgnoreCase("Paper") &&cpuChoice.equalsIgnoreCase
("Rock")){
System.out.println("Computer chooses Rock. You win!");
return winner = user;
}//edn if 4
if(userChoice.equalsIgnoreCase("Scissors")&&
cpuChoice.equalsIgnoreCase("Paper")) {
System.out.println("Computer chooses Scissors. You win!");
return winner = user;
}//end if 5
if (userChoice.equalsIgnoreCase("Scissors")&&
cpuChoice.equalsIgnoreCase("Rock")){
System.out.println("Computer chooses Rock. You lose!");
return winner = user;
}//end if 6
return winner;
}//END pick winner
}//End rPs Class
********************************** ************************************ 8 这是由讲师创建的主要课程。
package game;
import java.util.*;
public class Game
{
public static void main (String[] args)
{
Scanner in = new Scanner (System.in);
rPs rps = new rPs(); //***Your class
int numGames = 0;
String userChoice = "";
String cpuChoice = "";
String winner = "";
int userWins = 0;
int cpuWins = 0;
System.out.println("Welcome to Rock, Paper, Scissors!\n");
//Get odd number of games
System.out.println("How many rounds would you like to play?");
numGames = in.nextInt();
while (numGames % 2 == 0) //Even number
{
System.out.println("Sorry, number of games must be odd. Please try again:");
numGames = in.nextInt();
}
//Flush the buffer
in.nextLine();
//Play the game for the number of rounds the user entered
for (int i = 1; i <= numGames; i++)
{
//Get the user and computer choices
userChoice = rps.getUserChoice(); //***Your method
cpuChoice = rps.getCPUChoice(); //***Your method
System.out.println("Computer chooses " + cpuChoice);
//Pick winner
winner = rps.pickWinner(userChoice, cpuChoice); //***Your method
if (winner.equalsIgnoreCase("Tie"))
{
System.out.println("It's a tie! Play again.");
numGames++;
}
else
{
if (winner.equalsIgnoreCase("User"))
{
userWins++;
}
else if (winner.equalsIgnoreCase("Computer"))
{
cpuWins++;
}
else
{
System.out.println("Error in picking winner");
}
System.out.println(winner + " wins!");
}
} //end for
//Print results
System.out.println("\nUser wins: " + userWins);
System.out.println("Computer wins: " + cpuWins);
if (userWins > cpuWins)
{
System.out.println("\nThe user won!");
}
if (cpuWins > userWins)
{
System.out.println("The computer won!");
}
//Close game
System.out.println("\nThank you for playing!");
} //end main
} //end class
如果您发生车祸,如果您引用例外情况,诊断起来会更容易 – 2014-11-02 21:18:37