if-eingaben nur einmal anzeigen

iliasdoui

Mitglied
Java:
import java.util.Scanner;

public class ilia {
   
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("How can i help you?");
        String eingabe = sc.next();
        if(eingabe == "How are you?"); {
            System.out.println("Im good what about you?");
        }
       
        if(eingabe == "How was your day?"); {
            System.out.println("Mine was good what about yours?");
            }
       
        if(eingabe == "What time is it?"); {
            System.out.println("I dont know go look yourself");
        }
   
        if(eingabe == "Can you see me?"); {
            System.out.println("Yeah i can see you trough your cameras");
        }
   
        if(eingabe == "How old are you?"); {
            System.out.println("I am 666 years old");
        }
       
        if(eingabe == "Where are you from?"); {
            System.out.println("You dont want to know that");
        }
       
        if(eingabe == "Can you play music?"); {
            System.out.println("Of course what you wanna hear");
        }
       
        if(eingabe == "Whats your name?"); {
            System.out.println("I dont have a name. I was made before the time started");
        }

Wenn ich eine Eingabe mache dann kommen alle antworten auf einmal und das würde ich gerne ändern
freue mich über eure antworten🙂
LG ilias
ps: ist für privaten gebrauch und ja ich programmiere noch nicht so lange🙂
 
Zuletzt bearbeitet von einem Moderator:
Zwei Dinge:
A) nach dem if (....) hast du ;. Damit beendet du das if komplett und der Code der nachfolgt ist ein Block unabhängig vom if. Also: die ; bei den if entfernen!
B) Strings vergleicht man mit equals und nicht mit ==. Mit == würden nur die String Referenzen verglichen und nicht die String Werte.
 
Zwei Dinge:
A) nach dem if (....) hast du ;. Damit beendet du das if komplett und der Code der nachfolgt ist ein Block unabhängig vom if. Also: die ; bei den if entfernen!
B) Strings vergleicht man mit equals und nicht mit ==. Mit == würden nur die String Referenzen verglichen und nicht die String Werte.
Danke für deine schnelle antwort ich habe die == durch equals vertauscht und der code funktioniert nun wunderbar😉jedoch musste ich die ; da lassen da der code ohne diese hinter den ifs nicht funktioniert
tzd danke🙂
LG ilias
 
Java:
import java.util.Scanner;

public class ilia {
   
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("How can i help you?");
        String eingabe = sc.next();
        if(eingabe.equals("How are you?") ) {
            System.out.println("Im good what about you?");
        }
       
        if(eingabe.equals("How was your day?") ) {
            System.out.println("Mine was good what about yours?");
            }
       
        if(eingabe.equals("What time is it?") ) {
            System.out.println("I dont know go look yourself");
        }
   
        if(eingabe.equals("Can you see me?") ) {
            System.out.println("Yeah i can see you trough your cameras");
        }
   
        if(eingabe.equals("How old are you?") ) {
            System.out.println("I am 666 years old");
        }
       
        if(eingabe.equals("Where are you from?") ) {
            System.out.println("You dont want to know that");
        }
       
        if(eingabe.equals("Can you play music?") ) {
            System.out.println("Of course what you wanna hear");
        }
       
        if(eingabe.equals("Whats your name?") ) {
            System.out.println("I dont have a name. I was made before the time started");
        }
       
        if(eingabe.equals("Do you belive in God") ) {
            System.out.println("Yes i believe in Allah");
        }
       
        if(eingabe.equals("What are you?") ) {
            System.out.println("you dont want to know that");
        }
       
        if(eingabe.equals("Tell me a joke") ) {
            System.out.println("You");
        }
       
        if(eingabe.equals("") ) {
            System.out.println("");
        }
       
        if(eingabe.equals("") ) {
            System.out.println("");
        }
       
       
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
    }
   
}
das ist der jetzige code🙂
jedoch bekomme ich jtz gar kein output mehr auf der console
 
Ok, dann kommen wir zum Punkt c):

Ich sehe hier zwei Ansätze:
a) Nach dem einlesen der Eingabe in die Variable Eingabe: Gib diese doch einfach einmal aus. Dann würdest Du erst einmal sehen, was überhaupt eingegeben wurde. Also sowas wie:
Code:
String eingabe = sc.next();
System.out.println("Eingabe: \"" + eingabe + "\"");
Was fällt Dir auf?
b) Schau dir alternativ einmal an, was die Methode next macht? https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#next--
Evtl. wirst Du daraus klug und verstehst das Problem? Zusammen mit a) solltest Du es aber schnell begreifen...

Du erhältst keine Ausgabe, da eingabe nie einer Deiner Zeichenketten enthält. Es kann kein "How was your day?" enthalten. Es kann auch kein "" sein. Das gilt auch für alle anderen Checks, die Du derzeit machst.

Eine Lösungsmöglichkeit wäre ggf. "eingabe = sc.nextLine();" - aber das nur am Rande. Du solltest auf jeden Fall erst einmal verstehen, was next() macht um die Problematik zu verstehen.
 
Kleiner Tipp:
Java:
import java.util.Scanner;

public class QA {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		String[][] qa = { 
				{ "How are you?", "Im good what about you?" }, 
				{ "How was your day?", "Mine was good what about yours?" }, 
				{ "What time is it?", "I dont know go look yourself" },
				{ "Can you see me?", "Yeah i can see you trough your cameras" }, 
				{ "How old are you?", "I am 666 years old" }, 
				{ "Where are you from?", "You dont want to know that" }, 
				{ "Can you play music?", "Of course what you wanna hear" },
				{ "Whats your name?", "I dont have a name. I was made before the time started" }, 
				{ "Do you belive in God", "Yes i believe in Allah" }, 
				{ "What are you?", "you dont want to know that" }, 
				{ "Tell me a joke", "You" } 
		};

		System.out.println("How can i help you?");
		String eingabe = new Scanner(System.in).nextLine();
		String ausgabe = null;
		for (String[] strings : qa) {
			if (strings[0].toLowerCase().startsWith(eingabe.toLowerCase())) {
				ausgabe = strings[1];
				break;
			}
		}
		if (ausgabe != null) {
			System.out.println(ausgabe);
		} else {
			System.out.println("sorry, i don't understand you");
		}
	}
}
 

Zurück
Oben