0

>>> where's the bug in my java source code ? <<<

heyyo fellow solo learners ! been banging my keyboard for coding a simple java-based text-based rpg ... can anyone help me find the bugs in this source code ? - ph4n70m ------------------------------------------------------------------------------------------- public class Enemy { private String name; private String type; private int health; private int level; private int attack; Enemy() { this.setName(); this.setType(); } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setType(String type) { this.type = type; } public String getType() { return type; } public static void main(String [] args) { Enemy enemy = new Enemy("Ratto","Rat"); System.out.println(enemy.getName()); System.out.println(enemy.getType()); } } -------------------------------------------------------------------------------------------

25th Oct 2017, 1:06 PM
ph4n70m
ph4n70m - avatar
4 ответов
+ 5
First off, love that you're making a text based rpg. :D I use to make online text adventure games back in the 90s (MUDs) and that brings back good memories. Basically, you have your defautl constructor and it's not setting anything. So either change it to a simple variable assignment in the default, or put quotes inside of the set methods so you can set it to an empty string. Also, when you create your object, you're feeding it arguments that don't even exist in the constructor declaration, so create a second constructor to handle that input. https://code.sololearn.com/ca4N0EtT93s1/#java EXAMPLE: Enemy() { this.setName(""); this.setType(""); } Enemy(String name, String type) { this.setName(name); this.setType(type); }
25th Oct 2017, 1:25 PM
AgentSmith
+ 2
@ Tobias Hallmans @ Daniel Fernández Hidalgo ty :)
25th Oct 2017, 2:27 PM
ph4n70m
ph4n70m - avatar
25th Oct 2017, 1:39 PM
Daniel
Daniel - avatar
0
https://code.sololearn.com/c1t7jUgNAPi9/?ref=app ----- I modified the source code , but now it doesn't execute in the play ground ...
25th Oct 2017, 2:48 PM
ph4n70m
ph4n70m - avatar