i don't get the output for 'total' which was at the very last segment of the coding. i think there must be something wrong with the if else statement but i am not sure which one.
import java.util.Scanner; // Scanner is in the java.util package public class discount {
public static void main(String[] args) {
// Create a scanner object
Scanner sc = new Scanner (System.in);
//declare variables
double age=0;
String government_servant,room;
double price = 0;
double day = 0;
double total=0;
//Prompt user to enter the input
System.out.print("Type of room (Single | Double | Family ): ");
room = sc.next();
System.out.print("Enter your age : ");
age = sc.nextInt ();
System.out.print("Are you a government servant (yes or no): ");
government_servant = sc.next();
System.out.print("How many days you want to stay: ");
day = sc.nextDouble();
System.out.print(room);
//selection method
if (room == "Single" || room == "single") {
if (age > 60.0) {
price = 185.0 * 0.85;
}
else if (government_servant == "yes" || government_servant == "Yes") {
price = 185.0 * 0.9;
}
else {
price = 185.0;
}
}
else if (room == "Double" || room == "double") {
if (age > 60.0 ) {
price = 219.0 * 0.85;
}
else if (government_servant == "yes" || government_servant == "Yes") {
price = 210.0 * 0.9;
}
else {
price = 210.0;
}
}
else if (room == "family" || room == "Family") {
if (age > 60.0 ) {
price = 285.0 * 0.85;
}
else if (government_servant == "yes" || government_servant == "Yes") {
price = 285.0 * 0.9;
}
else {
price = 285.0;
}
}
//mathematical operation
total = price * day;
//display results
System.out.println("Total payment : RM " + total );
}}
Aucun commentaire:
Enregistrer un commentaire