Reputation: 9
I was doing some Java just to practice, and decided to do a Dungeons & Dragons type code. (This is just rolling for character stats)
I typed in everything, and it keeps saying (character)'s greatest stat was vitality. Help?
import java.util.Random;
import java.util.Scanner;
public class DAndD {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Random generator = new Random();
System.out.println("Choose a name for your Dungeons & Dragons character.");
String character = input.next();
System.out.print("Ah, " + character + " has a nice ring to it.\nLet's roll for stats shall we?");
int str = generator.nextInt(21);
int dex = generator.nextInt(21);
int intel = generator.nextInt(21);
int wis = generator.nextInt(21);
int vit = generator.nextInt(21);
if (str > 0);
String trait = "strength";
int large = str;
if (dex > large);
trait = "dexterity";
large = dex;
if (intel > large);
trait = "intelligence";
large = intel;
if (wis > large);
trait = "wisdom";
large = wis;
if (vit > large);
trait = "vitality";
large = vit;
if(large == str){
System.out.println("\n" + character + "'s best stat is its strength!");
}
else if (large == dex){
System.out.println("\n" + character + "'s best stat is its dexterity!");
}
else if (large == intel){
System.out.println("\n" + character + "'s best stat is its intelligence!");
}
else if (large == wis){
System.out.println("\n" + character + "'s best stat is its wisdom!");
}
else if (large == vit){
System.out.println("\n" + character + "'s best stat is its vitality");
}
System.out.println("\nStrength: " + str + "\nDexterity:" + dex + "\nIntelligence: " + intel + "\nWisdom: " + wis + "\nVitality: " + vit);
}
}
So, this is what the end result looks like as of now:
OUTPUT
Choose a name for your Dungeons & Dragons character.
*Toetar*
Ah, Toetar has a nice ring to it.
Let's roll for stats shall we?
Toetar's best stat is its vitality
Strength: 5
Dexterity: 19
Intelligence: 1
Wisdom: 6
Vitality: 4
Upvotes: 0
Views: 153
Reputation: 15644
The statements after your if
are never evaluated when the the condition expression inside if
is true
because you have kept a semi-colon
after your if
which won't consider your next statement to be part of conditional if
block:
For example :
if (str > 0); // you have kept ; semi-colon so it does nothing
String trait = "strength"; // this is independent of above statement
Also, as suggested by @MadProgrammer in above answer, if you use this :
if (str > 0) {
String trait = "strength";
int large = str;
}
Your variable trait
,large
will be accessible only inside your if
block.
So better first declare them before you start your ifs
:
String trait = "";
int large = 0;
if (str > 0) {
trait = "strength";
large = str;
} else if (dex > large) {
trait = "dexterity";
large = dex;
}//and so on
Upvotes: 1
Reputation: 347184
You have semicolons (;
) after your if statements (if (vit > large);
), which are negating their functionality. This is why it's important to use {...}
I "assume" you mean something more like...
String trait = "";
int large = -1;
if (str > 0) {
trait = "strength";
large = str;
} else if (dex > large) {
trait = "dexterity";
large = dex;
} else if (intel > large) {
trait = "intelligence";
large = intel;
} else if (wis > large) {
trait = "wisdom";
large = wis;
} else if (vit > large) {
trait = "vitality";
large = vit;
}
Upvotes: 5