Ajuda código

1 resposta
java
B
* To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package powersgame;
import java.util.ArrayList;

import javax.swing.JOptionPane;
/**
 *
 * @author Bernardo
 */
public class Powersgame {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
	Avatar a1 = new Avatar();
	ArrayList <Avatar>avatares = new ArrayList<>();
	a1.setNome(((JOptionPane.showInputDialog("Digite o seu nome: "))));
	while(a1.getNome() == null) {
		JOptionPane.showMessageDialog(null, "Digite o seu nome");
		a1.setNome(((JOptionPane.showInputDialog("Digite o seu nome: "))));;
	}
	
	System.out.println("NOME: "+ a1.getNome());
    
    a1.setOp(Integer.parseInt (JOptionPane.showInputDialog("1.Viking\n 2.Mago\n 3.Paladino")));
    
    System.out.println(a1.getForca());

}

}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package powersgame;

/**
 *
 * @author Bernardo
 */
public class Avatar {
	private int forca;
	private int defesa;
	private int vida;
	private String arma;
	private int fe;
	private int magia;
	private String tipo;
	private int op;
	private String nome;
	
	

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public int getForca() {
		return forca;
	}

	public void setForca(int forca) {
		this.forca = forca;
	}

	public int getDefesa() {
		return defesa;
	}

	public void setDefesa(int defesa) {
		this.defesa = defesa;
	}

	public int getVida() {
		return vida;
	}

	public void setVida(int vida) {
		this.vida = vida;
	}

	public String getArma() {
		return arma;
	}

	public void setArma(String arma) {
		this.arma = arma;
	}

	public int getFe() {
		return fe;
	}

	public void setFe(int fe) {
		this.fe = fe;
	}

	public int getMagia() {
		return magia;
	}

	public void setMagia(int magia) {
		this.magia = magia;
	}

	public String getTipo() {
		return tipo;
	}

	public void setTipo(String tipo) {
		this.tipo = tipo;
	}

	public int getOp() {
		return op;
	}

	public void setOp(int op) {
		this.op = op;
	}

	{

		switch (op) {
		case 1:
			System.out.println("Viking");
			this.setForca((int) Math.random() * 10);
			System.out.println("Força: " + this.getForca());
			this.setVida((int) Math.random() * 10);
			System.out.println("Vida: " + this.getVida());
			this.setDefesa((int) Math.random() * 10);
			System.out.println("Defesa: " + this.getDefesa());
			this.setArma("Machado");
			System.out.println("Arma: " + this.getArma());
			this.setFe(0 * 10);
			System.out.println("Fé: " + this.getFe());
			this.setMagia(0 * 10);
			System.out.println("Magia:" + this.getMagia());
			break;
		case 2:
			System.out.println("Mago:");
			this.setForca((int) Math.random() * 10);
			System.out.println("Força: " + this.getForca());
			this.setVida((int) Math.random() * 10);
			System.out.println("Vida: " + this.getVida());
			this.setDefesa((int) Math.random() * 10);
			System.out.println("Defesa: " + this.getDefesa());
			this.setArma("Machado");
			System.out.println("Arma: " + this.getArma());
			this.setFe(0 * 10);
			System.out.println("Fé: " + this.getFe());
			this.setMagia(0 * 10);
			System.out.println("Magia:" + this.getMagia());
			break;
		case 3:
			System.out.println("Paladino:");
			this.setForca((int)Math.random()*10);
	        System.out.println("Força: "+this.getForca());
	        this.setVida((int)Math.random()*10);
	        System.out.println("Vida: "+this.getVida());
	        this.setDefesa((int)Math.random()*10);
	        System.out.println("Defesa: "+this.getDefesa());
	        this.setArma("Machado");
	        System.out.println("Arma: "+this.getArma());
	        this.setFe(0*10);
	        System.out.println("Fé: "+this.getFe());
	        this.setMagia(0*10);
	        System.out.println("Magia:"+this.getMagia());
	        break;
		}

	}

Meu problema ta sendo que toda vez que eu retorno a força fica com 0, e queria saber também como faço para resolver o erro se eu clicar no cancelar na escolha dos avatares

1 Resposta

staroski

Veja sua classe Powersgame:

import javax.swing.JOptionPane;

public class Powersgame {

    public static void main(String[] args) {
        // a instrução 'new' invoca o construtor da classe Avatar
        Avatar a1 = new Avatar();
        ArrayList<Avatar> avatares = new ArrayList<>();
        a1.setNome(((JOptionPane.showInputDialog("Digite o seu nome: "))));
        while (a1.getNome() == null) {
            JOptionPane.showMessageDialog(null, "Digite o seu nome");
            a1.setNome(((JOptionPane.showInputDialog("Digite o seu nome: "))));
        }

        System.out.println("NOME: " + a1.getNome());
        a1.setOp(Integer.parseInt(JOptionPane.showInputDialog("1.Viking\n 2.Mago\n 3.Paladino")));
        System.out.println(a1.getForca());
    }
}

E veja como implementou a classe Avatar:

public class Avatar {
    
    private int forca;
    private int defesa;
    private int vida;
    private String arma;
    private int fe;
    private int magia;
    private String tipo;
    private int op;
    private String nome;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public int getForca() {
        return forca;
    }

    public void setForca(int forca) {
        this.forca = forca;
    }

    public int getDefesa() {
        return defesa;
    }

    public void setDefesa(int defesa) {
        this.defesa = defesa;
    }

    public int getVida() {
        return vida;
    }

    public void setVida(int vida) {
        this.vida = vida;
    }

    public String getArma() {
        return arma;
    }

    public void setArma(String arma) {
        this.arma = arma;
    }

    public int getFe() {
        return fe;
    }

    public void setFe(int fe) {
        this.fe = fe;
    }

    public int getMagia() {
        return magia;
    }

    public void setMagia(int magia) {
        this.magia = magia;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public int getOp() {
        return op;
    }

    public void setOp(int op) {
        this.op = op;
    }

    /*
     * Isso aqui é um bloco de inicialização
     * Ele é executado antes do construtor ser executado
     */
    {
        switch (op) {
            case 1:
                System.out.println("Viking");
                this.setForca((int) Math.random() * 10);
                System.out.println("Força: " + this.getForca());
                this.setVida((int) Math.random() * 10);
                System.out.println("Vida: " + this.getVida());
                this.setDefesa((int) Math.random() * 10);
                System.out.println("Defesa: " + this.getDefesa());
                this.setArma("Machado");
                System.out.println("Arma: " + this.getArma());
                this.setFe(0 * 10);
                System.out.println("Fé: " + this.getFe());
                this.setMagia(0 * 10);
                System.out.println("Magia:" + this.getMagia());
                break;
            case 2:
                System.out.println("Mago:");
                this.setForca((int) Math.random() * 10);
                System.out.println("Força: " + this.getForca());
                this.setVida((int) Math.random() * 10);
                System.out.println("Vida: " + this.getVida());
                this.setDefesa((int) Math.random() * 10);
                System.out.println("Defesa: " + this.getDefesa());
                this.setArma("Machado");
                System.out.println("Arma: " + this.getArma());
                this.setFe(0 * 10);
                System.out.println("Fé: " + this.getFe());
                this.setMagia(0 * 10);
                System.out.println("Magia:" + this.getMagia());
                break;
            case 3:
                System.out.println("Paladino:");
                this.setForca((int) Math.random() * 10);
                System.out.println("Força: " + this.getForca());
                this.setVida((int) Math.random() * 10);
                System.out.println("Vida: " + this.getVida());
                this.setDefesa((int) Math.random() * 10);
                System.out.println("Defesa: " + this.getDefesa());
                this.setArma("Machado");
                System.out.println("Arma: " + this.getArma());
                this.setFe(0 * 10);
                System.out.println("Fé: " + this.getFe());
                this.setMagia(0 * 10);
                System.out.println("Magia:" + this.getMagia());
                break;
        }
    }
}

Aquele bloco de inicialização dentro da classe Avatar deveria ser retirado e essa lógica implementada na classe Powersgame.

Criado 26 de novembro de 2018
Ultima resposta 27 de nov. de 2018
Respostas 1
Participantes 2