Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Weapons Test fails under certain conditions

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Weapons Test fails under certain conditions

    I'm making a Weapons Class and a Weapons Test.

    The Weapons Class gives me the damage power of each weapon (which changes each time) and the Weapons Test is supposed to show me the value and tell me which weapon "wins" based on score. If it's a tie, then it says "It's a Tie!" My weapons are Shortspear, Throwing Axe, and Flail.

    If the Shortspear wins, it will always say the Shortspear wins.

    But if the Throwing Axe wins, it will only state the Throwing Axe wins IF the Shortspear has a higher number than the Flail or if the Shortspear and Flail tie. If the Flail is a higher number than the Shortspear, it will not say anything, even when the Throwing Axe has the highest number.

    For example:

    Which weapon is the strongest?
    Shortspear: 3
    Throwing Axe: 6
    Flail: 2

    Throwing Axe wins!


    ^ This is fine.

    Which weapon is the strongest?
    Shortspear: 2
    Throwing Axe: 5
    Flail: 4


    ^ This is all the test shows. It doesn't acknowledge the Axe's victory.

    The same thing happens to the Flail, but only when the Shortspear has a higher number than the Throwing Axe. If they are tied or the Throwing Axe has a higher number, then it acknowledges the Flail's victory.

    This is my code for the Weapon Class:

    /**
     * A weapon class
     *
     * @author Erik
     * @version 1
     */
     
    import java.util.Random;
    public class Weapon
    {
    //fields
    String name;
    String size;
    String cat;
    String type;
    String description;
    public int cost;
    public int maxDamage;
    public int critMult;
    public int range;
    //initialize
    public Weapon(String name, String size, String cat, String type, String description, int cost, int maxDamage, int critMult, int range)
    {
    this.name=name;
    this.size=size;
    this.cat=cat;
    this.type=type;
    this.description=description;
    this.cost=cost;
    this.maxDamage=maxDamage;
    this.critMult=critMult;
    this.range=range;
    }
    public int getDamage(){
    int total=0;
    Random rand= new Random ();
    total+=rand.nextInt(maxDamage)+1;
    return total;
    }
    public int getCriticalDamage(){
    int total=0;
    Random rand= new Random ();
    total+=(rand.nextInt(maxDamage)*critMult)+1;
    return total;
    }
     
    //methods
    }

    This is my code for the Weapon Test:

    /**
     * A test for the weapon class
     *
     * @author Erik
     * @version 1
     */
    public class WeaponTest
    {
    static int SpearDamage;
    static int FlailDamage;
    static int AxeDamage;
    public static void main(String[] args)
    {
    Weapon myShortspear = new Weapon( "Shortspear", "Long", "Spear", "P", "A spear with narrow range that deals heavy damage. Easily blocked with shields.", 1, 6, 2, 20);
    Weapon myThrowingAxe = new Weapon( "Throwing Axe", "Short", "Axe", "S", "A powerful ranged weapon with short distance.", 8, 6, 2, 10);
    Weapon myFlail = new Weapon( "Flail", "Medium", "Flail", "B", "An unpredictable weapon that's hard to master.", 8, 6, 2, 5);
    System.out.println("Shortspear: A spear with narrow range that deals heavy damage. Easily blocked with shields.");
    System.out.println("Throwing Axe: A powerful ranged weapon with short distance.");
    System.out.println("Flail: An unpredictable weapon that's hard to master.");
    System.out.println("   ");
    System.out.println("Which weapon is the strongest?");
    SpearDamage=myShortspear.getDamage();
    AxeDamage=myThrowingAxe.getDamage();
    FlailDamage=myFlail.getDamage();
    System.out.println("Shortspear: " + SpearDamage);
    System.out.println("Throwing Axe: " + AxeDamage);
    System.out.println("Flail: " + FlailDamage);
    System.out.println("   ");
    if(SpearDamage>AxeDamage){
    if(SpearDamage>FlailDamage){
    System.out.println("Shortspear wins!");
    }
    else if(SpearDamage==FlailDamage){
    System.out.println("It's a tie!");
    }
    }else{
    if(FlailDamage>SpearDamage){
    if(FlailDamage>AxeDamage){
    System.out.println("Flail wins!");
    }
    else if(FlailDamage==AxeDamage){
    System.out.println("It's a tie!");
    }
    }else{
    if(AxeDamage>SpearDamage){
    if(AxeDamage>FlailDamage){
    System.out.println("Throwing Axe wins!");
    }
    else if(AxeDamage==FlailDamage){
    System.out.println("It's a tie!");
    }
    }
    }
    }
    }
    }

    Can anyone see the issue?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Weapons Test fails under certain conditions

    Is your code properly indented in your editor? If not, indent it properly and the reason for the results may become more clear. If it is indented properly in your editor, then please post it that way here.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Weapons Test fails under certain conditions

    Your logic is missing a lot of cases. I have attached a picture, which is your logic in truth-table form (done with excel). You should easily be able to see any logical mistakes by looking at a truth table.
    truthtable.jpg
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Weapons Test fails under certain conditions

    Thank you for the truth table, but is there a way to make it bigger? I'm having trouble reading it. ^^; Also, what do the -1s mean?

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Weapons Test fails under certain conditions

    I couldn't upload a larger picture. Try downloading it and zooming it on a picture viewer. The -1, 0, and 1 in the first 3 columns were just sample values to easily compare the axe, spear, and Flail damages instead of using obscure values like 56 or 99 or whatever. All the other 1's and 0's in the table represent true and false, respectfully.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Weapons Test fails under certain conditions

    public void test(){
     
    		int axedamage = 4;
    		int speardamage = 2;
    		int sworddamage = 5;
     
    		if((axedamage > speardamage) && (axedamage > sworddamage)){
     
    			System.out.println("Axe Wins with " + axedamage +" damage!");
     
    		}else if((axedamage >= speardamage) && (axedamage >= sworddamage)){
     
    			if(axedamage == speardamage){
    				System.out.println("Axe tied with Spear at "+axedamage+" damage!");
    			}
    			if(axedamage == sworddamage){
    				System.out.println("Axe tied with Sword at "+axedamage+" damage!");	
    			}
    		}else
     
    		if((speardamage > axedamage) && (speardamage > sworddamage)){
     
    			System.out.println("Spear Wins with " + speardamage +" damage!");
     
    		}else if((speardamage >= axedamage) && (speardamage >= sworddamage)){
     
    			if(speardamage == axedamage){
    				System.out.println("Spear tied with Axe at "+speardamage+" damage!");
    			}
    			if(speardamage == sworddamage){
    				System.out.println("Spear tied with Sword at "+speardamage+" damage!");	
    			}
     
    		}else
     
    		if((sworddamage > axedamage) && (sworddamage > speardamage)){
     
    			System.out.println("Sword Wins with " + sworddamage +" damage!");
     
    		}else if((sworddamage >= axedamage) && (sworddamage >= speardamage)){
     
    			if(sworddamage == axedamage){
    				System.out.println("Sword tied with axe at "+sworddamage+" damage!");
    			}
    			if(sworddamage == speardamage){
    				System.out.println("Sword tied with Spear at "+sworddamage+" damage!");
    			}
    		}
     
    	}

    Be ABSOLUTELY sure you know how to use your ||, &&, ==, !=, >, < and (). You can have lots of control over what statements can do.

    With values:

    axe = 4
    spear = 2
    sword = 2
    Result: Axe Wins with 4 damage!

    axe = 3
    spear = 3
    sword = 2
    Result: Axe tied with Spear at 3 damage!

    axe = 1
    spear = 2
    sword = 3
    Result: Sword Wins with 3 damage!

    I would go even further to take off the else statements between each object's comparison block and log all the calculations in case you wanted the stats of damage against armor or effects.
    Last edited by micecd; September 13th, 2014 at 12:48 AM.

Similar Threads

  1. Conditions?
    By Clizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2013, 01:06 PM
  2. JComboBox conditions?
    By maronski in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2013, 06:28 PM
  3. Trying to compile Fails
    By d_davis90 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 14th, 2012, 09:32 AM
  4. Test Fails but I don't know why, please help
    By cutekill0 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 22nd, 2011, 12:31 PM