This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

Help with If / Else If / Else

Jean Fontaine
Greenhorn
Posts: 12
Hey everybody !
Here's my problem : I'm trying to write a very simple program, since I started learning Java yesterday... It's a fight between two guys, and you can choose to "attack" or to "limit break". By the way, to see if you hit or not, you have to answer to a math exercise, hard if you take limit break, but ends up giving more damage, and easy if you take attack, but deals less damage.
So my problem is : if the player enters "attaque" or "Attaque", do the if statement. This works.
Then, else if player writes "limit break", "Limit Break" or "Limit break", do the block of code. This works too.
If the player enters something else, then the opponent damages the player for a few HP.

What's wrong is : When I enter "attaque" or "Attaque", if I answer right, my opponent takes 25 dmg, if I answer wrong, I take 25 dmg. But when my loop starts again at the beginning of the function, then it automatically does the "else" statement and I take 10 damage.

Here's the code : ( in french )

import java.util.Scanner;

public class combat_maths {
public static void main ( String args[] ){

Scanner input = new Scanner(System.in);
String type;
double calcul;
double damage;
double enemy_hp;
double hp;

enemy_hp = 100;
hp = 100;

do {

System.out.println ( "Choisissez une attaque : écrivez 'attaque' ou 'limit break'.");
type = input.nextLine();

if ( type.equals("attaque") | type.equals("Attaque") )

{ System.out.println("6*7 ?");
calcul = input.nextDouble();
if (calcul == 42) {
System.out.println ( "Vous frappez l'ennemi de toutes vos forces." );
enemy_hp = enemy_hp - 25; }
else { System.out.println ("Vous tentez une attaque, mais l'adversaire vous contre et vous mets un coup de boule !");
hp = hp - 25;
}
}

else if ( type.equals("limit break") | type.equals("Limit break") | type.equals("Limit Break") )
{ System.out.println ( "2*8*9*4*5 ?" );
calcul = input.nextDouble();
if ( calcul == 2880 ) {
System.out.println ("Vous projetez une boule de feu vers l'adversaire, puis vous bondissez sur lui et plantez votre épée dans son torse !");
enemy_hp = enemy_hp - 50;
}
else { System.out.println ("Vous préparez une boule de feu, mais le mercenaire adverse charge un éclair et vous le lance dessus. La charge vous frappe en pleine poitrine et vous sentez comme une odeur de grillé...");
hp = hp -50;
}
}

else {
System.out.println ("Alors que vous vous demandez que faire, le mercenaire vous frappe de son épée d'un coup vif et sournois.");
hp = hp - 10 ;
}
System.out.println (hp);
System.out.println (enemy_hp);
} while (hp > 0 && enemy_hp > 0) ;

if (hp <= 0) { System.out.println ("Alors que vous faiblissez, vous dérapez sur un caillou et l'ennemi saisi sa chance. L'épée sur la gorge, vous etes contraint d'abandonner... \n Vous n'etes pas fait pour etre un chevalier des maths.");
}

else if (enemy_hp <= 0){ System.out.println ("Vous bondissez sur l'ennemi chancelant et le frappez avec la garde de votre épée. Vainqueur, vous levez le bras. \n Vous etes accepter comme chevalier des maths !");
}

}
}

Thanks !
Oracion

Ralph Cook
Ranch Hand
Posts: 479
• 1
Welcome to the Ranch.

A couple of things: first, lurk a bit on a site before posting there. It helps give you the overall tone of things as they are normally done.

Second, we're all volunteers here, helping you out because that's what we want to do. It is a bit presumptuous of you to tell us to hurry, and makes it less likely that anyone will answer at all.

Third, if you had lurked, you would have seen repeated requests to put code within code tags, so that it is formatted to make it easier to read. Please look at some posts that do this and see how it is done, and how much easier it makes it.

I am not familiar enough with Scanner for numeric input to help you with your actual question. I can tell you that the scanner is passing your "input.nextLine()" statement with \r and \n characters as the input -- it appears to be returning a new token made up of a carriage return and a newline, though I would not have expected it to.

rc

Campbell Ritchie
Sheriff
Posts: 48404
56
Ralph Cook wrote: . . . code within code tags, so that it is formatted to make it easier to read. . . .
I would have added code tags, but the code is not indented, so it would have been pointless. Please go through your code and indent it with every { and } on a line by itself, like here. Then you can see which if matches which else. Also don't write such long lines; they are difficult to read.

Why are you asking for a double when the arithmetic is always in integer format?

Look at this post. I suspect what is happening is that you enter 42↩ and the Scanner reads the 42. Then it reads the next line. But the next line is whatever follows the 42 and precedes the ↩, i.e. "", so it cannot match "attaque" or "limit break". The solution, if that is in fact the problem, is to call input.nextLine() and discard the result at the very end of the loop.

Jean Fontaine
Greenhorn
Posts: 12
Hey !
Thank you for your answer, Ritchie !
I triple-checked all the curly braces and I'm sure the else corresponds with the right if, and I also really think you are right.
But , as I said, I'm really a beginner so if it isn't to much to ask how do I "call" input.nextLine() and how do I "discard the result at the very end" ? Do you mean removing the else ? But then nothing would happen if you type wrong, and that's not what I want to happen.
Anyways, thank you for your answer !
Oracion

Campbell Ritchie
Sheriff
Posts: 48404
56
You can simply write input.nextLine(); after the last nextInt() call. No = or anything, only input.nextLine();

fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
another few suggestions...

1) You may want to 'normalize' the user input. So instead of checking for "limit break" or "Limit break" or "Limit Break" (which would fail on "LIMIT BREAK"), a common technique would be to pass the input through a method that converts it to all lower-case, then you only have to check against "limit break" or "attaque".

2) If you are only dealing with integer values, I wouldn't use a double, but a long or short.

3) saying "that's not what I want to happen" doesn't make it easy for anyone to help you. instead, tell us what you DO want to happen.

Jean Fontaine
Greenhorn
Posts: 12
Hey guys !
Thanks for your answer ritchie, it completly solved the problem !
I will remember that !
Topic closed !
Oracion

Campbell Ritchie
Sheriff
Posts: 48404
56
You're welcome