接受Java中的方法参数

时间:2013-11-14 17:13:51

标签: java methods

我的问题令我感到困惑,因为我之前已经这样做了,并且它在以前的程序中有效,但是这个特定的程序不起作用。我只需要使用方法typeInput和popularNumber以及我传递给它们的2个变量,但是我无法在我的main方法中调用它们而没有错误。 “)”或“;”预期会发生,在我看来,在需要的地方有括号和半冒号。我确信这是一个快速解决方案,并希望学习如何解决它。谢谢!

 public static void main(String[] args) {
        // TODO code application logic here
        nameInput();
         typeInput(Scanner keyboard, CartoonStar star);
       popularityNumber();


    }

    /**
     *
     * @param name
     */
    public static void nameInput() {

        System.out.println("What is the name of the cartoon character : ");
        Scanner keyboard = new Scanner(System.in);
        CartoonStar star = new CartoonStar();
        String name = keyboard.next();
        star.setName(name);

    }

    public static void typeInput(Scanner keyboard, CartoonStar star){
        System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n"
                + "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT");

        switch (keyboard.nextInt())
                {case 1 :
                    star.setType(CartoonType.FOX);
                    break;

                case 2 :
                    star.setType(CartoonType.CHICKEN);
                    break;
                case 3 :
                    star.setType(CartoonType.RABBIT);
                    break;
                case 4 :
                    star.setType(CartoonType.MOUSE);
                    break;
                case 5 : 
                    star.setType(CartoonType.DOG);
                    break;
                case 6 : 
                    star.setType(CartoonType.CAT);
                    break;
                case 7 :
                    star.setType(CartoonType.BIRD);
                    break;
                case 8 :
                    star.setType(CartoonType.FISH);
                    break;
                case 9 :
                    star.setType (CartoonType.DUCK);
                    break;
                case 10 : 
                    star.setType(CartoonType.RAT);
                    break;
        }
    }


        public static void popularityNumber(Scanner keyboard, CartoonStar star){
        System.out.println("What is the cartoon popularity number?");
        int popularity = keyboard.nextInt();
        star.setPopularityIndex(popularity);
        System.out.println(star.getName() + star.getType() + star.getPopularityIndex());
    }
}







CartoonStar class (just in case you want it): 




public class CartoonStar {


  private String name;   
  private CartoonStar.CartoonType type;  
  enum CartoonType {
      FOX(1),CHICKEN(2),RABBIT(3),MOUSE(4),DOG(5),CAT(6),BIRD(7),FISH(8),DUCK(9),RAT(10);

  private final int animalType;
  private static Map <Integer, CartoonType> map = new HashMap <Integer, CartoonType>();
  private CartoonType(int animalType){

  this.animalType=animalType;
  }

  public int getAnimlType(){
  return animalType;}


  }//enum types
  private int popularityIndex;  //1 to 10 10 being the most popular

  public CartoonStar() {
  }//end no argument construtor

  public CartoonStar(String name,CartoonStar.CartoonType type, int popularityIndex) {
    setName(name);
    setType(type);
    setPopularityIndex(popularityIndex);
  }//end full constructor

  //getters and setters
  public void setName(String name) {
    this.name = name;
  }      
  public String getName() {
    return name;
  }
  public void setType(CartoonStar.CartoonType type) {
    this.type = type;
  }
  public CartoonStar.CartoonType getType() {
    return type;
  }
  public void setPopularityIndex(int popularityIndex){
    this.popularityIndex = popularityIndex;
  }
  public int getPopularityIndex(){
    return popularityIndex;
  }  

}

1 个答案:

答案 0 :(得分:3)

在main方法中,您可以按如下方式调用方法:

typeInput(Scanner keyboard, CartoonStar star);

typeInput方法需要声明 keyboard声明 star。你没有错误地调用它。

您最好的选择如下:

public static void main(String[] args) {
        // TODO code application logic here
    nameInput();
    popularityNumber();
}

public static void nameInput() {

    System.out.println("What is the name of the cartoon character : ");
    Scanner keyboard = new Scanner(System.in);
    CartoonStar star = new CartoonStar();
    String name = keyboard.next();
    star.setName(name);

    typeInput(keyboard, star);
}

我在typeInput()方法中添加了nameInput(),并将其从main()方法中移除。