I´m new to Java and I have made the code down belowe. It works as intended except for the fact that it after the user have typed in the amount af gas it jumps right down to the else-statement and prints out "Wrong answer! Type either 'yes' or 'no' ". After that its all fine and it accepts yes or no and do the calculations after the answers. Pls help. I have searched the web for answers but I cannot find out what I´m doing wrong.
public static void gasPrice()
{
Scanner scan = new Scanner(System.in);
double totalPrice;
double pricePerLitre = 1.23;
double discountPercent = 5;
String userAnswer;
boolean loop = true;
System.out.println("Hi and welcome to High&Dry gas station. Please register how many litres you required.");
double recievedGas = scan.nextDouble();
System.out.println("Thank you. Are you a member of High&Dry and therefor recieving a 5% discount? Please answer 'yes' or 'no' ");
userAnswer = scan.nextLine();
do {
if (userAnswer.equalsIgnoreCase("ja")) {
totalPrice = recievedGas * pricePerLitre * (1 - 0.01 * 5);
System.out.printf("%.2fdollars is drawn from your account", totalPrice);
loop = false;
}
else if(userAnswer.equalsIgnoreCase("nej")) {
totalPrice = recievedGas * pricePerLitre ;
System.out.printf("%.2fdollars is drawn from your account",totalPrice);
loop = false;
}
else {
System.out.println("Wrong answer! Type either 'yes' or 'no' ");
userAnswer = scan.nextLine();
}
}while (loop);
}
Aucun commentaire:
Enregistrer un commentaire