java - 我如何正确调用我创建的方法以便我能够在另一个中使用它?
问题描述
我正在用以下方法制作石头、纸、剪刀:
displayRoundResults()
displayMatchResults()
chooseHandShape()
winningHand()
//etc
该winningHand()
方法比较字符并返回一个值(0,1,2)。在我的displayRoundResults()
方法中,我尝试将该winningHand()
方法调用到该displayRoundResults()
方法中,以便它可以从中获取整数winningHand()
,然后根据返回的值添加到玩家的任一分数。
我想我的方法写得很好,我只是停留在如何调用方法上。
public class RockPaperScissors
{
private final char _YES = 'Y';
private final int _HIGH_BESTOF = 5;
// ***********************************************************************
// central method that holds the majority of the game's logic
public void playGame()
{
char player1, player2; //used to display the handshape from method
int playToWins;
int winningHand;
int p1Score = 0;
int p2Score = 0;
int numberOfRounds;
while (true) //add loop so that it keeps going until user decides to end the game
System.out.println("Welcome to Rock, Paper, Scissors..");
//call playToWins Method to retrieve how many rounds will be played
//playToWins(scan);
//will output different outcomes until winner has won x amount of times (the number retrieved from playToWins)
for (int i = 1; i <= numberOfRounds; i++)
{
player1 = chooseHandShape();
player2 = chooseHandShape();
//displays rock,paper, or scissor for each player
System.out.println("P1: " + player1 + " P2: " + player2);
//winningHand is called to compare the two values and then return an int (0,1,2)
winningHand = winningHand(player1, player2);
//displayRoundResult is called to calculate score for each round that is played
displayRoundResult(p1Score, p2Score);
}
//displayMatchResult here
//
// majority of code should be here
//scanner should be here
}
// display round results
private void displayRoundResult(int p1Score, int p2Score)
{
int winningHand;
//properly call winningHand method here
if (winningHand == 1)
{
p1Score++;
}
if (winningHand == 2)
{
p2Score++;
}
//if winning equals ... then p1score goes up by 1 or p2score does
}
// display match results
private void displayMatchResult(int round, int p1Score, int p2Score)
{
System.out.println("Player 1 has " + p1Score + " points & Player 2 has " + p2Score + " points");
}
// get what will be the round goal score
private int playToWins(Scanner scan)
{
int numberOfRounds;
System.out.println("Play round to? (Max is 5)");
numberOfRounds = scan.nextInt();
while (numberOfRounds > _HIGH_BESTOF)
{
System.out.println("Please enter a value between 1 and 5, your last input was incorrect!");
numberOfRounds = scan.nextInt();
}
return numberOfRounds;
}
// given two hands choose which one wins
// possible values for either parameter are 'R','P' or 'S'
// use the RPS rules to determine the winner
// return 0 for tie, 1 for player 1 win or 2 for player 2 win
private int winningHand(char player1, char player2)
{
int winningHand;
char R,P,S;
//tie
if (player1 == (player2))
{
winningHand = 0;
}
//if player 1 wins
if (player1 == R && player2 == S)
{
winningHand = 1;
}
if (player1 == S || player2 == P )
{
winningHand = 1;
}
if (player1 == P || player2 == R)
{
winningHand = 1;
}
//if player 2 wins
if (player1 == S || player2 == R)
{
winningHand = 2;
}
if (player1 == P || player2 == S)
{
winningHand = 2;
}
if (player1 == R || player2 == P)
{
winningHand = 2;
}
return winningHand;
}
// method that randomly chooses a hand shape
// returns 'R' for rock, 'P' for paper and 'S' for scissors
private char chooseHandShape()
{
Random cChoice = new Random();
Random pChoice = new Random();
//0 = Scissors, 1 = Rock, 2 = Paper
int cChoiceInt = cChoice.nextInt(2);
int pChoiceInt = pChoice.nextInt(2);
//player 1 randomized
char pChoice1;
switch (pChoiceInt)
{
case 0:
pChoice1 = 'S';
break;
case 1:
pChoice1 = 'R';
break;
case 2:
pChoice1 = 'P';
break;
}
//player 2 randomized
char cChoice1;
switch (cChoiceInt)
{
case 0:
cChoice1 = 'S';
break;
case 1:
cChoice1 = 'R';
break;
case 2:
cChoice1 = 'P';
break;
}
return cChoice1;
return pChoice1;
}
// Yes/No response
// Returns true if the user enters a 'y' or 'Y'
//
private boolean yesResponse(Scanner scan)
{
System.out.println("Would you like to play again? Yes(Y) or No(N)?");
//scan.nextChar();
return scan.nextLine().toUpperCase().charAt(0) == _YES;
}
}
解决方案
好的,所以你绝对是编程新手。知道了这一点,我建议尽可能多地阅读/观看有关编程基础知识的指南。
无论如何,正如您可以在此处阅读的那样,您首先需要编写函数的名称(您说对了那部分)。但是,Java 和许多语言中的函数也由括号定义,()
如果有参数,则用于传递参数(但即使没有参数,这些仍然是必需的)。此外,在函数返回一个像你一样的值的情况下,如果你想在以后使用它(就像你的情况一样),你需要将它的结果分配给一个变量。这将是一种正确的方法:
int thisRoundWinningHand = winningHand(player1Hand, player2Hand)
但是,请注意,这在您当前的实现中是不正确的,因为您当前在函数中采用类型值char
但winningHand()
您displayRoundResults()
只收到分数?两个玩家作为 type 的值int
。此外,变量的名称是不同的,因为您希望轻松区分函数和变量(但在 Java 中不是强制性的)。
关于您的代码还有很多需要指出的地方,这就是为什么我再次敦促您首先阅读适当的指南(我在此处链接的 W3Schools 对初学者有好处,您可以从介绍开始)。
希望能帮助到你
推荐阅读
- python - Sublime Text 3 完成后不会杀死进程
- c++ - 'CryptQueryObject' 未在此范围内声明
- mysql - MySQL:项目数与选定项目不同
- terminate - 你如何告诉 Dafny 在验证终止时使用引理
- c# - AuthorizationCode 流上的 Swagger UI 身份验证错误
- python - 如何在 Python 的特定行中访问文本文件中的数据?
- java - 我想交换字符串的每隔一个字符
- python - 如何将列表值保存在 python 的列表中?
- swiftui - 由于 NavigationLink 多次调用 SwiftUI onAppear
- python - 如何在 Tkinter 中打开同一文件的多个窗口?