方法不能应用于给定类型,试图调用我的getMenuChoice()方法

时间:2013-11-05 06:59:32

标签: java

我正在尝试在我的main方法中调用我的getMenuChoice()方法来输出我的getMenuChoice()方法中的东西,但它告诉我该方法不能应用于给定的类型; required:String,找到的字符串:无参数

这是为什么?感谢任何帮助。谢谢。

 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;
    }

}
}

4 个答案:

答案 0 :(得分:0)

您需要将两个String参数传递给getMenuChoice();方法。

需要更改

choice = getMenuChoice();

String choice = getMenuChoice(footballTeam1 ,footballTeam2);

System.out.println("Choice is :" + choice );

<强>更新

你正在使用&amp;&amp;操作员在你的条件不正确的情况下。

替换为下面的

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;

}

答案 1 :(得分:0)

您声明了getMenuChoice(String footballTeam1,String footballTeam2)

这意味着在调用此方法时,必须提供两个String参数:

choice = getMenuChoice(footballTeam1, footballTeam2);
顺便说一句,没有宣布选择。您需要在为其分配值之前(或何时)声明它:

String choice = getMenuChoice(footballTeam1, footballTeam2);

答案 2 :(得分:0)

System.out.println("Game Score:");
System.out.println(footballTeam1 + ":0");
System.out.println(footballTeam2 + ":0");

choice = getMenuChoice(footballTeam1,footballTeam2);

答案 3 :(得分:0)

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;



    }


}