我的总数没有加起来,而是保持为0

时间:2019-02-10 03:58:05

标签: loops totals

每当我运行我的代码时,我所有的总数最终都为零。我想念什么?

while (theaterNumber != -999)

childTicketInOneTotal += childTicketInOne;
adultTicketInOneTotal += adultTicketInOne;
childTicketInTwoTotal += childTicketInTwo;
adultTicketInTwoTotal += adultTicketInTwo;

if (theaterNumber == 1)

  {System.out.print("How many child tickets were sold?");
  childTicketInOne = keyboard.nextInt();

  System.out.print("How many adult tickets were sold?");
  adultTicketInOne = keyboard.nextInt();

        System.out.print("Which theater was used (1 or 2)? Enter -999 to complete inputs.");
        theaterNumber = keyboard.nextInt();}

else if (theaterNumber == 2)

{System.out.print("How many child tickets were sold?");
  childTicketInTwo = keyboard.nextInt();

  System.out.print("How many adult tickets were sold?");
  adultTicketInTwo = keyboard.nextInt();

        System.out.print("Which theater was used (1 or 2)? Enter -999 to complete inputs.");
        theaterNumber = keyboard.nextInt();}}

//Display totals
System.out.println();
System.out.println("Theater 1 Totals:");
System.out.println("Adult Tickets Sold: "+ adultTicketInOneTotal +"");    
System.out.println("Child Tickets Sold: "+ childTicketInOneTotal +"");
System.out.printf("Total Amount Made: $%.2f \n\n",  totalAmountTheaterOne);
System.out.println();
System.out.println("Theater 2 Totals:");
System.out.println("Adult Tickets Sold: "+ adultTicketInTwoTotal +"");
System.out.println("Child Tickets Sold: "+ childTicketInTwoTotal +"");
System.out.printf("Total Amount Made: $%.2f \n\n",  totalAmountTheaterTwo);

}}

1 个答案:

答案 0 :(得分:0)

这是什么语言?您可能想在循环开始之前初始化变量,并在输出之前添加输入,如下所示:

childTicketInOneTotal = 0;
adultTicketInOneTotal = 0;
childTicketInTwoTotal = 0;
adultTicketInTwoTotal = 0;

while (theaterNumber != -999){

  if (theaterNumber == 1)

   {System.out.print("How many child tickets were sold?");
   childTicketInOne = keyboard.nextInt();

   System.out.print("How many adult tickets were sold?");
   adultTicketInOne = keyboard.nextInt();

    System.out.print("Which theater was used (1 or 2)? Enter -999 to complete inputs.");
    theaterNumber = keyboard.nextInt();}

  else if (theaterNumber == 2)

   {System.out.print("How many child tickets were sold?");

    childTicketInTwo = keyboard.nextInt();

    System.out.print("How many adult tickets were sold?");
    adultTicketInTwo = keyboard.nextInt();
  # end if not needed?

    System.out.print("Which theater was used (1 or 2)? Enter -999 to complete inputs.");
    theaterNumber = keyboard.nextInt();}}

   childTicketInOneTotal += childTicketInOne;
  adultTicketInOneTotal += adultTicketInOne;
  childTicketInTwoTotal += childTicketInTwo;
  adultTicketInTwoTotal += adultTicketInTwo;

  //Display totals
  System.out.println();
  System.out.println("Theater 1 Totals:");
  System.out.println("Adult Tickets Sold: "+ adultTicketInOneTotal +"");    
  System.out.println("Child Tickets Sold: "+ childTicketInOneTotal +"");
  System.out.printf("Total Amount Made: $%.2f \n\n",  totalAmountTheaterOne);
  System.out.println();
  System.out.println("Theater 2 Totals:");
  System.out.println("Adult Tickets Sold: "+ adultTicketInTwoTotal +"");
  System.out.println("Child Tickets Sold: "+ childTicketInTwoTotal +"");
  System.out.printf("Total Amount Made: $%.2f \n\n",  totalAmountTheaterTwo);

}}