Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

help with the do-wihle loop

Hello, I have been having a very hard time with this do while loop...it doesn't seem to work...it says cannot find symbol variable on the last line...can anybody help please?

thank you

System.out.print("Please tell me how many people you are in your group: ");
double numberPeople=input.nextDouble();
System.out.print("Thank you, now tell me the total cost of your meal: ");
double totalCost=input.nextDouble();
System.out.print("Thank you, now please tell me discount you were allotted in % form: ");
double discount=input.nextDouble();


//Compute the given data

do{
double worthDiscount=totalCost*(discount/100);//Calculate the total cost after the discount is given
double amountGst=worthDiscount*GST;//Calculate the amount of Pst (cost after disc.*.05)
double amountQst=(worthDiscount+amountGst)*QST;//Calculate the amount of Qst, so I did (cost after discount+Gst)*.075
double totalPayableAmount=worthDiscount+amountGst+amountQst;
double amountPaidByEach=totalPayableAmount/numberPeople;

//Output the results to console
System.out.println("Your discount is "+discount+"%, which comes up to "+worthDiscount+"$.");
System.out.println("Your subtotal price after discount is "+(totalCost-worthDiscount));
System.out.println("You are "+numberPeople+ " people");
System.out.println("The GST comes to "+amountGst+" $");
System.out.println("The Qst comes to "+amountQst+"$");
System.out.println("Your total price is "+(((totalCost-worthDiscount)*1.05)*1.075)+"$");
System.out.println("The amount paid by each person is: "+(((((totalCost-worthDiscount)*1.05)*1.075))/numberPeople)+"$");

System.out.println("Would you like to repeat the calculation?");
double answer=input.nextDouble();
}
while (answer=1);

}
«1

Comments

  • usman riazusman riaz Posts: 7Member
    Use comparison operator '==' instead of assignment operator '=', it will solve your problem.
    thanks!
  • usman riazusman riaz Posts: 7Member
    Also declare answer if you haven't!
  • paradisojulioparadisojulio Posts: 27Member
    Thanks for the help, but when I declare the variable before that, it says that the answer is already defined in main (java.lang.String).

    import java.util.Scanner; //Import Scanner

    public class Restaurant_Ass1
    {

    public static void main (String[]args)
    {
    //Create Scanner
    Scanner input=new Scanner(System.in);

    //ennumerate my constants
    final double GST=0.05;
    final double QST=0.075;
    double answer;


    //Ask for the needed information



    System.out.print("Please tell me how many people you are in your group: ");
    double numberPeople=input.nextDouble();
    System.out.print("Thank you, now tell me the total cost of your meal: ");
    double totalCost=input.nextDouble();
    System.out.print("Thank you, now please tell me discount you were allotted in % form: ");
    double discount=input.nextDouble();


    //Compute the given data

    do{
    double worthDiscount=totalCost*(discount/100);//Calculate the total cost after the discount is given
    double amountGst=worthDiscount*GST;//Calculate the amount of Pst (cost after disc.*.05)
    double amountQst=(worthDiscount+amountGst)*QST;//Calculate the amount of Qst, so I did (cost after discount+Gst)*.075
    double totalPayableAmount=worthDiscount+amountGst+amountQst;
    double amountPaidByEach=totalPayableAmount/numberPeople;

    //Output the results to console
    System.out.println("Your discount is "+discount+"%, which comes up to "+worthDiscount+"$.");
    System.out.println("Your subtotal price after discount is "+(totalCost-worthDiscount));
    System.out.println("You are "+numberPeople+ " people");
    System.out.println("The GST comes to "+amountGst+" $");
    System.out.println("The Qst comes to "+amountQst+"$");
    System.out.println("Your total price is "+(((totalCost-worthDiscount)*1.05)*1.075)+"$");
    System.out.println("The amount paid by each person is: "+(((((totalCost-worthDiscount)*1.05)*1.075))/numberPeople)+"$");

    System.out.println("Would you like to repeat the calculation?");
    double answer=input.nextDouble();
    }
    while (answer==1);

    }

    }
  • usman riazusman riaz Posts: 7Member
    hmmmmm what are the exact outputs can you paste the error msgs?
  • usman riazusman riaz Posts: 7Member
    sorry! i haven't seen that you have already declared the answer before the closing '{' of do while ,just omit the double before the 'answer' variable inside while loop.
  • usman riazusman riaz Posts: 7Member
    i think its ok now take care!
  • paradisojulioparadisojulio Posts: 27Member
    1000 times, thank you!!
  • paradisojulioparadisojulio Posts: 27Member
    This post has been deleted.
  • paradisojulioparadisojulio Posts: 27Member
    This post has been deleted.
  • paradisojulioparadisojulio Posts: 27Member
    What you told me works, but now there is another problem...I am trying to make this work... It is a problem where the user must enter an answer in order to restart or finish the program...when I use numbers, it works fine, (1 or 2) but when I use "y" or "n", there are errors that come out:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at Restaurant_Ass1.main(Restaurant_Ass1.java:54)

    Process completed.

    I know this must be a really simple thing, but I have no idea what, I have been trying everything, but it only works with numbers




    import java.util.Scanner; //Import Scanner

    public class Restaurant_Ass1
    {

    public static void main (String[]args)
    {
    //Create Scanner
    Scanner input=new Scanner(System.in);

    //ennumerate my constants
    final double GST=0.05;
    final double QST=0.075;
    double answer;

    //Ask for the needed information


    do{ //loop in case answer==y
    System.out.print("Please tell me how many people you are in your group: ");
    double numberPeople=input.nextDouble();
    System.out.print("Thank you, now tell me the total cost of your meal: ");
    double totalCost=input.nextDouble();
    System.out.print("Thank you, now please tell me the discount you were allotted in % form: ");
    double discount=input.nextDouble();


    //Compute the given dataSystem.out.printf("Total spendings: %5.2f",total);


    double worthDiscount=totalCost*(discount/100);//Calculate the total cost after the discount is given
    double amountGst=worthDiscount*GST;//Calculate the amount of Pst (cost after disc.*.05)
    double amountQst=(worthDiscount+amountGst)*QST;//Calculate the amount of Qst, so I did (cost after discount+Gst)*.075
    double totalPayableAmount=worthDiscount+amountGst+amountQst;// Calculate amount payable in total
    double amountPaidByEach=totalPayableAmount/numberPeople;// Calculate amount paid by each member

    //Output the results to console
    System.out.printf("Your discount is $ %5.2f
    ",worthDiscount);
    System.out.printf("Your subtotal price after discount is $%5.2f
    ",(totalCost-worthDiscount));
    System.out.printf("The GST comes to $ %5.2f
    ",amountGst);
    System.out.printf("The Qst comes to $ %5.2f
    ",amountQst);
    System.out.printf("Your total price is $ %5.2f
    ",(((totalCost-worthDiscount)*1.05)*1.075));
    System.out.printf("The amount paid by each person is $ %5.2f
    ",(((((totalCost-worthDiscount)*1.05)*1.075))/numberPeople));

    do{ //loop in case answer!=y||n

    System.out.println("Would you like to repeat the calculation?");
    answer=input.nextDouble();
    if(answer=='n')
    {break;
    }
    else if (answer!='y')
    {System.out.printf("I am sorry, but you input"+answer+"
    This is not valid. Please try again.
    ");
    }




    }
    while (answer!='y');

    }
    while (answer=='y');
    }
    }


«1
Sign In or Register to comment.