+ 1
So I am making a dumb AI code and whenever the person inputs hi, hello, or hey, it doesn't work
import java.util.Scanner; public class dumbAI_V2 { public static void main(String[] args) { System.out.println("DumbAi: Hello, I am the updated version of GingyFlake's C# dumb AI."); Scanner sc=new Scanner(System.in); String input=sc.nextLine(); String response=input.toLowerCase(); System.out.println("You: "+input+"\n"); if (response=="hello" || response=="hi" || response=="hey") { System.out.println("DumbAi: Hello!"); } } }
2 Réponses
+ 6
♬Astro-GingyFlake🇹🇷
Use == cautiously as it isn't generally recommended for comparing string content, especially when dealing with string objects created using new String().
Prefer Equals() ( c# ) or equals() ( java) For accurate content comparison, always use the Equals() ( c# ) or equals() ( java) method. It ensures that the strings have the same characters in the same order, regardless of their memory location.
By understanding this distinction, you can write more reliable and efficient Java code with equals() and c sharp with Equals() involving string comparisons.
+ 4
♬Astro-GingyFlake🇹🇷
Change the if to
if ((response.equals("hello"))||(response.equals("hi"))||(response.equals("hey"))) {
System.out.println("DumbAi: Hello!");