Java 方法无法应用于给定类型,正在尝试调用我的getMenuChoice()方法
我试图在main方法中调用getMenuChoice()方法来输出getMenuChoice()方法中的内容,但它告诉我该方法不能应用于给定的类型;必需:字符串,找到字符串:无参数 为什么会这样?谢谢你的帮助Java 方法无法应用于给定类型,正在尝试调用我的getMenuChoice()方法,java,Java,我试图在main方法中调用getMenuChoice()方法来输出getMenuChoice()方法中的内容,但它告诉我该方法不能应用于给定的类型;必需:字符串,找到字符串:无参数 为什么会这样?谢谢你的帮助 package footballgame; import java.util.Scanner; public class FootballGame { static Scanner keyboard = new Scanner(System.in); public
package footballgame;
import java.util.Scanner;
public class FootballGame {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String footballTeam1;
String footballTeam2;
System.out.print("Enter a name for a team:");
footballTeam1 = keyboard.nextLine();
System.out.print("Enter a name for another team:");
footballTeam2 = keyboard.nextLine();
System.out.println("Game Score:");
System.out.println(footballTeam1 + ":0");
System.out.println(footballTeam2 + ":0");
choice = getMenuChoice();
}
public static String getMenuChoice(String footballTeam1,String footballTeam2){
String choice;
do{
System.out.println("Select an option:");
System.out.println("A:" + footballTeam1 + "scored");
System.out.println("B:" + footballTeam2 + "scored");
System.out.println("C: game ended.");
System.out.println("?:");
choice = keyboard.nextLine();
if (choice.equalsIgnoreCase("A")){
choice = (footballTeam1);
} else if (choice.equalsIgnoreCase("B")){
choice = (footballTeam2);
} else if (choice.equalsIgnoreCase("C")){
choice = ("Game over!");
}
}while(!choice.equals("A") && !choice.equals("B") && !choice.equals("C"));
return choice;
}
}
这是我的新代码
package footballgame;
import java.util.Scanner;
public class FootballGame { static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
String choice;
Scanner keyboard = new Scanner(System.in);
String footballTeam1;
String footballTeam2;
System.out.print("Enter a name for a team:");
footballTeam1 = keyboard.nextLine();
System.out.print("Enter a name for another team:");
footballTeam2 = keyboard.nextLine();
System.out.println("Game Score:");
System.out.println(footballTeam1 + ":0");
System.out.println(footballTeam2 + ":0");
choice = getMenuChoice(footballTeam1, footballTeam2);
}
public static String getMenuChoice(String footballTeam1,String footballTeam2){
String choice;
String input;
do{
System.out.println("Select an option:");
System.out.println("A:" + footballTeam1 + " scored");
System.out.println("B:" + footballTeam2 + " scored");
System.out.println("C: game ended.");
System.out.println("?:");
input = keyboard.nextLine();
if (input.equalsIgnoreCase("A")){
choice = (footballTeam1);
} else if (input.equalsIgnoreCase("B")){
choice = (footballTeam2);
} else if (input.equalsIgnoreCase("C")){
choice = ("Game over!");
}
}while(!input.equals("A") && !input.equals("B") && !input.equals("C"));
return choice;
}
}
下面是我的另一个类,底部是addscore方法。
公营足球队{
private String name;
private int score;
public static int TOUCHDOWN = 6;
public static int FIELD_GOAL = 3;
public static int SAFETY = 2;
public static int TWO_POINT_CONVERSION = 2;
public static int EXTRA_POINT = 1;
public FootballTeam(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public boolean addScore(int points) {
if (points == TOUCHDOWN || points == FIELD_GOAL || points == SAFETY || points == TWO_POINT_CONVERSION || points == EXTRA_POINT) {
score = points + score;
return true;
} else {
return false;
}
}
}
您需要将两个字符串参数传递给
getMenuChoice();
方法
需要改变
choice = getMenuChoice();
到
更新
您在while条件下使用的&&运算符不正确
替换为下面的一个
while (!choice.equals("A") || !choice.equals("B")
|| !choice.equals("C"));
找到匹配的值后,无需再次迭代,即可返回匹配的值
下面是您更正的方法
public static String getMenuChoice(String footballTeam1,
String footballTeam2) {
String choice;
do {
System.out.println("Select an option:");
System.out.println("A:" + footballTeam1 + "scored");
System.out.println("B:" + footballTeam2 + "scored");
System.out.println("C: game ended.");
System.out.println("?:");
Scanner keyboard = null;
choice = keyboard.nextLine();
if (choice.equalsIgnoreCase("A")) {
return footballTeam1;
} else if (choice.equalsIgnoreCase("B")) {
return footballTeam2;
} else if (choice.equalsIgnoreCase("C")) {
return "Game over!";
}
} while (!choice.equals("A") || !choice.equals("B")
|| !choice.equals("C"));
return choice;
}
您声明了
getMenuChoice(stringfootballteam1,stringfootballteam2)
这意味着调用此方法时,必须给出两个字符串参数:
choice = getMenuChoice(footballTeam1, footballTeam2);
顺便说一句,选项未声明。您需要在为其赋值之前(或何时)声明它:
String choice = getMenuChoice(footballTeam1, footballTeam2);
您的问题是调用getMenuChoice()时没有参数
。更改它,使您有两个输入参数:footballTeam1
和footballTeam2
啊,谢谢,现在它给了我一个选择错误,告诉我它找不到符号选择。我要它做的就是调用该方法并输出其中的内容。啊,好吧,现在它在我的输出中不断显示菜单,即使是UGh我输入了正确的字母与否。就好像它无限地通过循环并一直显示它。为什么?啊,好吧,现在它在我的输出中一直显示菜单,即使我输入了正确的字母与否。就好像它无限地通过循环并一直显示它。为什么?调试它怎么样?使用debugger或打印一些可以帮助你的值。提示-考虑一下你的do-while条件。当你输入A、B或C时,检查选择是什么。当前尝试这样做,以前从未使用过。很遗憾,好的,我现在上传新代码。我收到一个错误,我现在没有启动选择,但我认为我启动了。是的,这起作用了,这就是我所遇到的,但即使我输入了正确的值,它仍会继续循环。输入内容:我为您附加了我的日志:为一个团队输入名称:a为另一个团队输入名称:游戏分数:a:0 a:0选择一个选项:a:Ascored B:Ascored C:Game ended.?:{a}进程结束,退出代码为0,因为您有choice=keyboard.nextLine();所以您需要在C:游戏结束了“,”?:“我已经更改了我的代码并将其编辑为我的原始代码,我将输入改为输入,而不是选择,试图让它停止循环
String choice = getMenuChoice(footballTeam1, footballTeam2);
System.out.println("Game Score:");
System.out.println(footballTeam1 + ":0");
System.out.println(footballTeam2 + ":0");
choice = getMenuChoice(footballTeam1,footballTeam2);
Run this code:
package footballgame;
import java.util.Scanner;
public class FootballGame { static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
String choice;
Scanner keyboard = new Scanner(System.in);
String footballTeam1;
String footballTeam2;
System.out.print("Enter a name for a team:");
footballTeam1 = keyboard.nextLine();
System.out.print("Enter a name for another team:");
footballTeam2 = keyboard.nextLine();
System.out.println("Game Score:");
System.out.println(footballTeam1 + ":0");
System.out.println(footballTeam2 + ":0");
choice = getMenuChoice(footballTeam1, footballTeam2);
}
public static String getMenuChoice(String footballTeam1,String footballTeam2){
String choice ="";
String input;
do{
System.out.println("Select an option:");
System.out.println("A:" + footballTeam1 + " scored");
System.out.println("B:" + footballTeam2 + " scored");
System.out.println("C: game ended.");
System.out.println("?:");
input = keyboard.nextLine();
if (input.equalsIgnoreCase("A")){
choice = (footballTeam1);
} else if (input.equalsIgnoreCase("B")){
choice = (footballTeam2);
} else if (input.equalsIgnoreCase("C")){
choice = ("Game over!");
}
}while(!input.equals("A") && !input.equals("B") && !input.equals("C"));
return choice;
}
}
And the log is:
A is what you need input
Enter a name for a team:A
Enter a name for another team:A
Game Score:
A:0
A:0
Select an option:
A:A scored
B:A scored
C: game ended.
?:
A //------------this place is what you need input to stop loop.
Process finished with exit code 0
Hand socre:
package footballgame;
import java.util.Scanner;
public class FootballTeam {
private String name;
private int score;
public static int TOUCHDOWN = 6;
public static int FIELD_GOAL = 3;
public static int SAFETY = 2;
public static int TWO_POINT_CONVERSION = 2;
public static int EXTRA_POINT = 1;
static Scanner keyboard = new Scanner(System.in);
public FootballTeam() {
}
public FootballTeam(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public boolean addScore(int points) {
if (points == TOUCHDOWN || points == FIELD_GOAL || points == SAFETY || points == TWO_POINT_CONVERSION || points == EXTRA_POINT) {
score = points + score;
return true;
} else {
return false;
}
}
public String toString() {
return this.getName() + ":" + this.getScore();
}
public static void main(String[] args) {
String choice;
FootballTeam teamA = new FootballTeam();
FootballTeam teamB = new FootballTeam();
System.out.print("Enter a name for a team A:");
teamA.setName(keyboard.nextLine());
System.out.print("Enter a name for another team B :");
teamB.setName(keyboard.nextLine());
System.out.print("Enter a source for a team A :");
teamA.setScore(Integer.valueOf(keyboard.nextLine()));
System.out.print("Enter a name for another team B:");
teamB.setScore(Integer.valueOf(keyboard.nextLine()));
System.out.println("Game Score:");
System.out.println(teamA.getName() + ":" + teamA.getScore());
System.out.println(teamB.getName() + ":" + teamB.getScore());
choice = getMenuChoice(teamA, teamB);
System.out.println(choice);
}
public static String getMenuChoice(FootballTeam teamA, FootballTeam teamB){
String choice ="";
String input;
do{
System.out.println("Select an option:");
System.out.println("A:" + teamA.getName() + " scored");
System.out.println("A:" + teamA.getName() + " scored");
System.out.println("C: game ended.");
System.out.println("?:");
input = keyboard.nextLine();
if (input.equalsIgnoreCase("A")){
choice = teamA.toString();
} else if (input.equalsIgnoreCase("B")){
choice = teamB.toString();
} else if (input.equalsIgnoreCase("C")){
choice = ("Game over!");
}
}while(!input.equals("A") && !input.equals("B") && !input.equals("C"));
return choice;
}
}