This question already has an answer here:
- How do I compare strings in Java? 23 answers
I'm sorry, but my code is in german! (I hope that's not a problem!)
My problem is, that even if (at the bottom) the input is "Ja"("Yes") or "ja"("yes") Java just skips it and does the else-clause!
Thanks to everyone in advance!
import java.util.Scanner;
public class Pizza {
private double preis;
private int anzahl;
public Pizza() {
preis = 5.50;
anzahl = 0;
}
public void hinzufuegen(int tmp) {
anzahl = anzahl + tmp;
}
public void entfernen(int tmp) {
anzahl = anzahl - tmp;
}
public void drucken() {
System.out.println("Pizzen: " + anzahl + " Preis: " + preis * anzahl);
}
public static void main(String[] args) {
Pizza Margherita = new Pizza();
Scanner scan = new Scanner(System.in);
System.out.print("Wie viele Pizzen willst du? ");
Margherita.anzahl = scan.nextInt();
System.out.print("Bist du dir sicher, dass du " + Margherita.anzahl + " Pizzen willst? ");
String input = scan.next();
System.out.println(input);
if(input=="Ja" || input=="ja") {
Margherita.drucken();
}
else {
System.out.println("Schade!");
}
}
}
Aucun commentaire:
Enregistrer un commentaire