Im new to java and this is my problem. Why did the code went straight to the else statement and print "Invalid item entered" instead of going to the if statement and print "Enter quantity" after i typed in Milo, Salt and Brown Sugar? Here is the coding that I did.
package Practice;
import java.util.Scanner;
public class SirT{
public static void main (String[]args) {
Scanner in = new Scanner (System.in);
String M = "Milo";
String S = "Salt";
String B = "Brown Sugar";
int i = 0;
double sum = 0;
double price = 0;
while (i < 3) {
System.out.print("Enter item name: ");
String item = in.next();
if (item == M) {
System.out.print("Enter quantity: ");
int quan = in.nextInt();
price = quan * 8.50;
}
else if (item == S) {
System.out.print("Enter quantity: ");
int quan = in.nextInt();
price = quan * 0.50;
}
else if (item == B) {
System.out.print("Enter quantity: ");
int quan = in.nextInt();
price = quan * 3.20;
}
else {
System.out.println("Invalid item entered.");
}
sum = sum + price;
i++;
}
System.out.println("Total price: " + sum);
}
}
Aucun commentaire:
Enregistrer un commentaire