简体   繁体   中英

How do I return from a void method

I have a problem where the comment in the code is, and it is as follows: My code is trying to pull 7 random cards from My getFullCollection() method, however, it does not allow Me to return any value because it states that void methods can not return a value. The code:

public class Packs {
    Deck classicSet = new Deck() {
    public void getFullCollection() {
    classicSet.getFullCollection();
    Random PO = new Random();
    int number;
    for(int counter = 1; counter<=7;  counter++) {
        number = 1+PO.nextInt(10000000);
        if(number < 41650) {
            return packag.Cards.Rarity.Common; //the problem
        }

it says:

Void methods cannot return a value

public void getFullCollection() throws IOException{
            Cards Shuriken = new Attacks(3, 2, packag.Cards.Effects.Return, packag.Cards.Class.Ninja, packag.Cards.Rarity.Common, ImageIO.read(new File("Shuriken.png")));
            System.out.println(Shuriken.toString());
            this.cards.add(Shuriken);

I tried deleting the void return type on my getFullCollection() class and replacing it with public int getFullCollection() , but it does not work

    class Attacks extends Cards {   
    private int damage;
    private int Mana;
    private final Effects effects;
    private BufferedImage card;
    private final Rarity rarity;
    private final Class class1;
    Attacks(int damage, int Mana, final Effects effects, final Class class1, final Rarity rarity, BufferedImage card) {
    super(Mana, effects, rarity, class1, card);
        this.damage = damage;
        this.rarity = rarity;
        this.class1 = class1;
        this.effects = effects;
        this.Mana = Mana;           
        this.card = card;
    }

This method is how I make cards in the getFullCollection method.

Are you creating a special form of deck with overwriting the getFullCollection() method?

Could u provide a simple description what the first code snippet should do, so i can unserstand better?

and if you do that you cant use a return

i am assuming this for loop is there to determine wich Rarity your cards schould be, so you would maybe have to source that out to another method if you want to use your return that way

As @MartinScheuchenpflug pointed out , I cannot use a return statement here. Below is the code on how to dance around that return statement:

if (number < 10000001) {
    Random cards = new Random();
    int i1 = cards.nextInt(7);
    if(i1 == 0) {
        pack_cards.add(packag.Deck.Shuriken);
                                                         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM