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 3 of 3

Thread: Can som1 help me with this code

  1. #1
    Junior Member
    Join Date
    Mar 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can som1 help me with this code

    why does this not work?

    import java.util.Scanner;
    public class Main {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Set Your Health");
    int health = scanner.nextInt();
    boolean IsAlive = health > 0;
    if(IsAlive) {
    System.out.println("Your Health Has Been Set To: " + health);
    } else {
    System.out.println("You Cannot Set Your Health To Negatives");
    }
    System.out.println("You Find A Legendary Sword. Do You Pick It up?");
    String Awnser1 = scanner.next();
    if(Awnser1 == "Yes") {
    System.out.println("You Pick Up \"Xyronyth, The Tempests Embrace\" And Venture Onwards");
    System.out.println("You Get Attacked By A Dragon And Draw Your Sword");
    var ddmg = Math.random() * 17;
    ddmg = Math.round(ddmg);
    health -= ddmg;
    if(IsAlive) {
    System.out.println("You Took" + ddmg + "Damage");
    System.out.println("You Attack The Dragon With Your Sword And It Dies");
    } else {
    System.out.println("You Died");
    }
    } else if(Awnser1 == "No") {
    System.out.println("You Leave The Sword Laying And Venture Onwards");
    System.out.println("You Get Attacked By A Dragon, It woud be Very Good If You Woud Have Something To Defend Yourself with");
    while(Awnser1 == "No") {
    double ddmg = Math.random() * 17;
    ddmg = Math.round(ddmg);
    health -= ddmg;
    if(IsAlive) {
    System.out.println("You Took" + ddmg + "Damage");
    } else {
    System.out.println("You Died");
    }
    }
    }

    }
    }

    (also why does it not indent on here)

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    279
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Can som1 help me with this code

    What doesn't work here?
    Whatever you are, be a good one

  3. #3
    Member
    Join Date
    Jan 2024
    Posts
    46
    Thanks
    0
    Thanked 1 Time in 1 Post

    Post Re: Can som1 help me with this code

    It seems like you're encountering a couple of issues in your code. Firstly, when comparing strings in Java, you should use the .equals() method instead of the == operator. So, instead of if (Awnser1 == "Yes"), you should write if (Awnser1.equals("Yes")). Similarly, for the comparison in the else if block.

    Secondly, the reason for not indenting properly here might be due to how you pasted the code. Ensure that the indentation is consistent when you paste it into an editor or an online platform.

    Here's the corrected version of the relevant parts of your code:

    import java.util.Scanner;

    public class Main {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Set Your Health");
    int health = scanner.nextInt();
    boolean IsAlive = health > 0;
    if (IsAlive) {
    System.out.println("Your Health Has Been Set To: " + health);
    } else {
    System.out.println("You Cannot Set Your Health To Negatives");
    }
    System.out.println("You Find A Legendary Sword. Do You Pick It up?");
    String Answer1 = scanner.next();
    if (Answer1.equals("Yes")) {
    System.out.println("You Pick Up \"Xyronyth, The Tempests Embrace\" And Venture Onwards");
    System.out.println("You Get Attacked By A Dragon And Draw Your Sword");
    double ddmg = Math.random() * 17;
    ddmg = Math.round(ddmg);
    health -= ddmg;
    if (IsAlive) {
    System.out.println("You Took " + ddmg + " Damage");
    System.out.println("You Attack The Dragon With Your Sword And It Dies");
    } else {
    System.out.println("You Died");
    }
    } else if (Answer1.equals("No")) {
    System.out.println("You Leave The Sword Laying And Venture Onwards");
    System.out.println("You Get Attacked By A Dragon, It would be Very Good If You Would Have Something To Defend Yourself with");
    while (Answer1.equals("No")) {
    double ddmg = Math.random() * 17;
    ddmg = Math.round(ddmg);
    health -= ddmg;
    if (IsAlive) {
    System.out.println("You Took " + ddmg + " Damage");
    } else {
    System.out.println("You Died");
    }
    }
    }
    }
    }

    This should fix the string comparison issue and provide better indentation for your code.

    If you're still facing challenges with your Java assignment, remember that programming is often a journey of trial and error. Don't hesitate to seek assistance from various resources available online. From community forums to educational websites, there are plenty of places where you can find guidance and support to improve your coding skills. Whether you're grappling with string comparisons or tackling complex algorithms, every hurdle you overcome brings you one step closer to mastery. If you find yourself in need of additional help with Java assignment, consider exploring reputable platforms like programminghomeworkhelp.com that offer programming homework assistance. They can provide valuable insights and tips to enhance your understanding of the language. After all, in the world of programming, every bit of knowledge gained is a step towards becoming a proficient coder.

Similar Threads

  1. Replies: 0
    Last Post: May 31st, 2022, 11:40 AM
  2. Replies: 6
    Last Post: August 5th, 2014, 11:19 AM
  3. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  4. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM