I'm working on a homework assignment for my Java class. I am using a scanner to ask for user input only once, but when I run the program, it continues to prompt the user for the data even after receiving the input. Finally after inputing the data three times, the rest of the program will run. Here is my code. It runs perfectly after the data has been input three times, I just can't figure out why it is asking three times instead of just running after the data has been entered the first time.
package CarlysEventPriceWithMethods;
import java.util.Scanner;
public class CarlysEventPriceWithMethods
{
public static void main(String[] args)
{
CarlysEventPriceWithMethods obj = new CarlysEventPriceWithMethods();
obj.getGuests(0);
obj.calcPrice(obj.getGuests(0));
obj.displayMotto();
}
public double getGuests(double guests)
{
Scanner input = new Scanner(System.in);
System.out.println("How many guests will be attending the event?");
guests = input.nextInt();
return guests;
}
public void calcPrice(double getGuests)
{
final int pricePerGuest = 35;
boolean isLarge;
double totalPrice;
double guests = getGuests(0);
isLarge = (guests >= 50);
totalPrice = guests * pricePerGuest;
System.out.println("Total guests attending: " guests);
System.out.println("Price per guest attending: $" pricePerGuest);
System.out.println("Total price for all guests: $" totalPrice);
System.out.println("Large Event? " isLarge);
}
public void displayMotto()
{
System.out.println("***********************************************\n"
"*Carly's makes the food that makes it a party!*\n"
"***********************************************");
}
}
CodePudding user response:
Because you are calling getGuests
3 times. Two times in main method and once inside the calcPrice
method.
public static void main(String[] args)
{
CarlysEventPriceWithMethods obj = new CarlysEventPriceWithMethods();
obj.getGuests(0); // <- first call to getGuests
obj.calcPrice(obj.getGuests(0)); // <- second call to getGuests, after which you go to calcPrice method where you call getGuests again
obj.displayMotto();
}
Here is your code with some minor tweaks.
public class CarlysEventPriceWithMethods {
public static void main(String[] args) {
CarlysEventPriceWithMethods obj = new CarlysEventPriceWithMethods();
int guests = obj.getGuests();
obj.calcPrice(guests);
obj.displayMotto();
}
public int getGuests() {
Scanner input = new Scanner(System.in);
System.out.println("How many guests will be attending the event?");
guests = input.nextInt();
return guests;
}
public void calcPrice(int guests) {
final int pricePerGuest = 35;
boolean isLarge;
double totalPrice;
isLarge = (guests >= 50);
totalPrice = guests * pricePerGuest;
System.out.println("Total guests attending: " guests);
System.out.println("Price per guest attending: $" pricePerGuest);
System.out.println("Total price for all guests: $" totalPrice);
System.out.println("Large Event? " isLarge);
}
public void displayMotto() {
System.out.println("***********************************************\n"
"*Carly's makes the food that makes it a party!*\n"
"***********************************************");
}
}
CodePudding user response:
You have put Scanner into method getGuests()
. Each time you call that method, nextInt()
method is invoked.
You call three times method getGuests()
and that's why you get prompted three times.