+ 1
What is wrong with my code? Where is the mistake that I made?
import java.util.Scanner; public class Program { //Someone please tell me why doesn’t my code work. If you type alma in the scanner,it should print the eredmeny. But it outputs elbasztad all the time. public static void main(String[] args) { Scanner muvelet = new Scanner(System.in); int egyik = 734; int masik = 1000; int eredmeny = egyik + masik; String beirt = muvelet.nextLine(); if (beirt=="alma") {System.out.println(eredmeny); } else { System.out.println("elbasztad"); } } }
2 ответов
+ 4
You did everything correct, except the part where you compare the String, because with strings you should use .equals()
change:
if(beirt=="alma")
into:
if(beirt.equals("alma"))
That should make it do what you want.
+ 1
Thank you very much, it works now :)