项目必须做好几个与公园出勤的事情。我遇到的问题是当我要求用户输入并设置它时 "进入= kb.nextInt();"或者对于退出选项来说,似乎程序似乎没有像我认为的那样逐步通过条件逻辑。它会遇到一个条件,然后一些它不应该。任何帮助将不胜感激。我已经通过电子邮件发送了我的教授,我的项目已经过了几天,我只是想早点完成它。 Screenshot of code running
import java.util.Scanner;//import Scanner
import java.text.DecimalFormat;//import formatter
public class ParkAttendance
{
public static void main(String[] args)
{
//used for user input
Scanner kb = new Scanner(System.in);
//used to format currency
DecimalFormat currency = new DecimalFormat("$#,##0.00");
//used to format average
DecimalFormat decimal = new DecimalFormat("#.0");
//----------------User Input----------------------------------
int menuChoice = 0;//menu choice
int subMenuChoice = 0;//menu choice
//-----------------Calculations-------------------------------
int exitCounter = 0;//number of times exits were entered
int enterCounter = 0;//number of times entries were entered
int highestAttendance = 0;//holds highest attendance
int currentAttendance = 0;//holds current attendance
int entering ;// people entering the park
int exiting ;//people exiting the park
boolean black = false;//boolean for in the black or red check
double avgEnter = 0;//average number of people entering the park
double avgExit = 0;//average number of people exiting the park
double income = 0;// income made for the day
//-----------------------Constants----------------------------
final int CAPACAITY = 250;//capacity of the park
final double TICKET = 7.5;//ticket cost
final int OP_COST = 2250;//operating costs for the park
//prompt for user menu
System.out.println("1. Display Park Attendance"
+"\n2. Update Attendance"
+"\n3. End Attendance Tracker\n");
//The user menu
do
{
menuChoice = kb.nextInt();
//Display currentAttendance
if(menuChoice == 1)
{
System.out.println("Current Attendance:" + currentAttendance );
}
//Attendance updater
if(menuChoice == 2)
{
System.out.println("1. People have entered the park"
+"\n2. People have left the park\n");
subMenuChoice = kb.nextInt();
if(subMenuChoice ==1)
{ //Prompt for input
System.out.println("Enter the number of people entering the park: ");
entering = kb.nextInt();
//change attendance as long as it's less than 250 and adjust currentAttendance
if(entering > 0)
{
currentAttendance += entering;
if(currentAttendance > 250)
{
System.out.println("Please wait until some people have left the park!");
currentAttendance -= entering;
}
else if (currentAttendance <= 250);
{
System.out.println(entering + " People have entered the park!");
enterCounter += 1;
income += (entering * TICKET);
}
}
if(entering < 0);
{
System.out.println("Please enter a positive number!");
entering = 0;
}
}
if(subMenuChoice == 2)
{ //Prompt for input
System.out.println("Enter the number of people exiting the park: ");
exiting = kb.nextInt();
//check to see if user entered a positive value
if(exiting < 0);
{
System.out.println("Please enter a positive number!");
currentAttendance = 0;
exitCounter = 0;
}
if(exiting > 0)
{
System.out.println(exiting + " people have left the park!");
currentAttendance -= exiting;
exitCounter += 1;
}
}
}//end of attendance updater
//re-prompt for menu
System.out.println("1. Display Park Attendance"
+"\n2. Update Attendance"
+"\n3. End Attendance Tracker\n");
}while(menuChoice != 3);//end of do while
//calculate average entries and exits
if(enterCounter >0 && exitCounter >0)
{
avgEnter = currentAttendance/enterCounter;
avgExit = currentAttendance/exitCounter;
}
//calculate if operating costs were met
if(income > OP_COST)
{
black = true;
}
//display calculations and end program
System.out.print("The highest attendance was: " + highestAttendance
+"\n Average number of people entering:" + decimal.format(avgEnter)
+"\n Average number of people exiting: " + decimal.format(avgExit)
+"\n Income earned: " + currency.format(income)
+"\n In the black: " + black);
}//end of main
}//end of Project2
答案 0 :(得分:0)
首先,你有一个分号在&#34; if(输入&lt; 0);&#34;所以总会显示出来。你还有一个分号在&#34; if(退出&lt; 0);&#34;所以它也会一直显示出来。您还应该检查以确保离开的人数不超过当前在公园内的人数。此外,您永远不会使用highestAttendance做任何事情。由于整数除法,您的avgEnter和avgExit总是为0。它截断了值(int)/(int)。您可以通过将一个或两个整数类型转换为双精度来轻松解决此问题,例如&#34; avgEnter = currentAttendance /(double)enterCounter;&#34;希望这有帮助,祝你好运!