How to make toString method within object?

六眼飞鱼酱① 提交于 2019-12-02 09:17:53

Your toString() method is inside your Card(int number, String suite) constructor. Move it out.

the String method is inside of the constructor of the class Cards, move it outside so you can use it, and so you can have a valid Card constr.

Place the toString outside of your constructor.

 public Card(int number, String suit) {
    this.number = number;
    this.suit = suit;
}
 @Override
 public String toString() {

          String[] high = {
            "Jack",
            "Queen",
            "King"
          };

            String type;

            if (number < 10) {
              return String.valueOf(this.number) + " of " + this.suit;
            }
            else {
              return high[this.number-10] + " of " + this.suit;
            }
    }

Your toString() method is inside your constructor;

here is the corrected code

public class Card {

  int number;
  String suit;

  public Card(int number, String suit) {
    this.number = number;
    this.suit = suit;
  }


      @Override
        public String toString() {

          String[] high = {
            "Jack",
            "Queen",
            "King"
          };

            String type;

            if (number < 10) {
              return String.valueOf(this.number) + " of " + this.suit;
            }
            else {
              return high[this.number-10] + " of " + this.suit;
            }

            //return suit + " of " + type;

          //return String.valueOf(number) + " of " + suit;
        }

// your main starts from here..

dont forget to format if youre in eclipse ctrl+shift+f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!