错误:实际和正式参数列表的长度不同。(Error: actual and formal argument lists differ in length. Object

编程入门 行业动态 更新时间:2024-10-26 15:15:56
错误:实际和正式参数列表的长度不同。(Error: actual and formal argument lists differ in length. Object-Oriented Programming)

所以我正在做我的第一个面向对象的编程任务,当我尝试编译时我想出了一个错误: winner = rps.pickWinner(userChoice,cpuChoice); // ***您的方法^必需:找不到参数:字符串,字符串原因:实际和正式参数列表长度不同任何帮助都将非常感激。

import java.util.Scanner; **strong text**public class KyleAbelWeek10 { public static void main(String[] args) { Scanner in = new Scanner(System.in); RockPaperScissors rps = new RockPaperScissors(); //***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 } import java.util.*; public class RockPaperScissors { public String getUserChoice () { //Utility Scanner in = new Scanner(System.in); //Variables String userChoice = ""; System.out.println("Rock, Paper, or Scissors?:"); userChoice = in.nextLine(); while (!userChoice.equalsIgnoreCase("rock") && !userChoice.equalsIgnoreCase("paper") && !userChoice.equalsIgnoreCase("scissors")) { System.out.println("Sorry," + userChoice + " is not a valid entry.Please enter Rock, Paper, or Scissors."); userChoice = in.nextLine(); } return userChoice; } public String getCPUChoice () { //Get Random Number Random r = new Random (); //Variables int randomnumber = 0; String cpuChoice = ""; //Make a random choice randomnumber = r.nextInt(3)+1; if (randomnumber == 1) { cpuChoice = ("rock"); } if (randomnumber == 2) { cpuChoice = ("paper"); } if (randomnumber == 3) { cpuChoice = ("scissors"); } return cpuChoice; } public String pickWinner () { //Variables String winner = ""; String userChoice = ""; String cpuChoice = ""; //Decide who wins the round if (userChoice.equalsIgnoreCase("rock")) { if (cpuChoice.equalsIgnoreCase("scissors")) { winner = ("user"); } else if (cpuChoice.equalsIgnoreCase("paper")) { winner = ("computer"); } else { winner = ("tie"); } } else if (userChoice.equalsIgnoreCase("paper")) { if (cpuChoice.equalsIgnoreCase("scissors")) { winner = ("computer"); } else if (cpuChoice.equalsIgnoreCase("rock")) { winner = ("user"); } else { winner = ("tie"); } } else if (userChoice.equalsIgnoreCase("scissors")) { if (cpuChoice.equalsIgnoreCase("rock")) { winner = ("computer"); } else if (cpuChoice.equalsIgnoreCase("paper")) { winner = ("user"); } else { winner = ("tie"); } } return winner; }

}

So I am doing my first object oriented programming assignment and I am coming up with an error when I try and compile: winner = rps.pickWinner(userChoice, cpuChoice); //***Your method ^ required: no arguments found: String,String reason: actual and formal argument lists differ in length Any help would be greatly appreciated.

import java.util.Scanner; **strong text**public class KyleAbelWeek10 { public static void main(String[] args) { Scanner in = new Scanner(System.in); RockPaperScissors rps = new RockPaperScissors(); //***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 } import java.util.*; public class RockPaperScissors { public String getUserChoice () { //Utility Scanner in = new Scanner(System.in); //Variables String userChoice = ""; System.out.println("Rock, Paper, or Scissors?:"); userChoice = in.nextLine(); while (!userChoice.equalsIgnoreCase("rock") && !userChoice.equalsIgnoreCase("paper") && !userChoice.equalsIgnoreCase("scissors")) { System.out.println("Sorry," + userChoice + " is not a valid entry.Please enter Rock, Paper, or Scissors."); userChoice = in.nextLine(); } return userChoice; } public String getCPUChoice () { //Get Random Number Random r = new Random (); //Variables int randomnumber = 0; String cpuChoice = ""; //Make a random choice randomnumber = r.nextInt(3)+1; if (randomnumber == 1) { cpuChoice = ("rock"); } if (randomnumber == 2) { cpuChoice = ("paper"); } if (randomnumber == 3) { cpuChoice = ("scissors"); } return cpuChoice; } public String pickWinner () { //Variables String winner = ""; String userChoice = ""; String cpuChoice = ""; //Decide who wins the round if (userChoice.equalsIgnoreCase("rock")) { if (cpuChoice.equalsIgnoreCase("scissors")) { winner = ("user"); } else if (cpuChoice.equalsIgnoreCase("paper")) { winner = ("computer"); } else { winner = ("tie"); } } else if (userChoice.equalsIgnoreCase("paper")) { if (cpuChoice.equalsIgnoreCase("scissors")) { winner = ("computer"); } else if (cpuChoice.equalsIgnoreCase("rock")) { winner = ("user"); } else { winner = ("tie"); } } else if (userChoice.equalsIgnoreCase("scissors")) { if (cpuChoice.equalsIgnoreCase("rock")) { winner = ("computer"); } else if (cpuChoice.equalsIgnoreCase("paper")) { winner = ("user"); } else { winner = ("tie"); } } return winner; }

}

最满意答案

您的RockPaperScissors类定义:

public String pickWinner ()

意思是它没有参数。 然而你的main称它为:

winner = rps.pickWinner(userChoice, cpuChoice);

其中一个是错误的,需要改变。


最有可能的是它将是RockPaperScissors功能,因为在调用它之前你已经有了用户和计算机的选择。 我会考虑改变:

public String pickWinner () { String winner = ""; String userChoice = ""; String cpuChoice = "";

成:

public String pickWinner (String userChoice, String cpuChoice) { String winner = "";

对于它的价值,您可以通过以下方式使该功能更具可读性:

public String pickWinner (String userChoice, String cpuChoice) if (userChoice.equalsIgnoreCase("rock")) { if (cpuChoice.equalsIgnoreCase("scissors")) return "user"; if (cpuChoice.equalsIgnoreCase("paper")) return "computer"; return "tie"; } if (userChoice.equalsIgnoreCase("scissors")) { if (cpuChoice.equalsIgnoreCase("paper")) return "user"; if (cpuChoice.equalsIgnoreCase("rock")) return "computer"; return "tie"; } if (userChoice.equalsIgnoreCase("paper")) { if (cpuChoice.equalsIgnoreCase("rock")) return "user"; if (cpuChoice.equalsIgnoreCase("scissors")) return "computer"; return "tie"; } return "???" }

Your RockPaperScissors class defines:

public String pickWinner ()

meaning that it has no parameters. And yet your main calls it:

winner = rps.pickWinner(userChoice, cpuChoice);

One of those is wrong and will need to be changed.


Most likely it'll be the RockPaperScissors function since you already have the user and computer choices before calling it. I'd look into changing:

public String pickWinner () { String winner = ""; String userChoice = ""; String cpuChoice = "";

into:

public String pickWinner (String userChoice, String cpuChoice) { String winner = "";

For what it's worth, you could make that function a little more readable with something like:

public String pickWinner (String userChoice, String cpuChoice) if (userChoice.equalsIgnoreCase("rock")) { if (cpuChoice.equalsIgnoreCase("scissors")) return "user"; if (cpuChoice.equalsIgnoreCase("paper")) return "computer"; return "tie"; } if (userChoice.equalsIgnoreCase("scissors")) { if (cpuChoice.equalsIgnoreCase("paper")) return "user"; if (cpuChoice.equalsIgnoreCase("rock")) return "computer"; return "tie"; } if (userChoice.equalsIgnoreCase("paper")) { if (cpuChoice.equalsIgnoreCase("rock")) return "user"; if (cpuChoice.equalsIgnoreCase("scissors")) return "computer"; return "tie"; } return "???" }

更多推荐

本文发布于:2023-08-04 23:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:长度   错误   参数   正式   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!