添加我的购买价格输出

Adding my outputs for for Purchase Price

我的代码有点吃力,我刚开始学习 java 4 周前,我希望我能得到你们的帮助.. 第一的。这是我的JAVA代码

import java.util.Scanner;
public class CoffeeBot
{
   public static void main(String[] args)
   {

    Scanner keyboard = new Scanner(System.in);
    System.out.println ("Hello, what's your name?");
    String name = keyboard.nextLine();
    System.out.println("Would you like to order some coffee, " + name + "? (y/n)");
String goon=keyboard.next();
char answer=goon.charAt(0);
if ((answer!= 'y') && (answer!='n'))
System.out.println("no valid");
else if (answer== 'n')
System.out.println("OK BYE");
else{
System.out.println("Great, Let's get started.");
System.out.println("Order selection");
System.out.println("----------------");
System.out.println("There are 90 coffee cups in stock and each costs .00");
System.out.println("There are 100 coffee shots in stock and each costs .00");
System.out.println("How many cups of coffee would you like?");
int cupsOfCoffee = keyboard.nextInt();
if (cupsOfCoffee ==0)
System.out.println("No cups, no coffee, Goodbye");
else if (cupsOfCoffee < 0)
System.err.println("Doesn't compute, system terminating");
else if (cupsOfCoffee >90)
System.out.println("Not enogh stock,come back later");
else {

  int countd;
  int[] shots = new int[cupsOfCoffee];
  for (countd = 0; countd < cupsOfCoffee; countd++)
  {
      System.out.println("How many coffee shots in cup " + (countd+1 ));

      shots[countd] = keyboard.nextInt();
      while ((shots[countd] <0 || (shots[countd]>100)) ) {
        System.err.println("Try again");
        System.out.println("How many cups of coffee would you like in" + (countd+1));
        shots[countd] = keyboard.nextInt();

      }
  }
  System.out.println("Order Suammery\n----------------");
  for (countd = 0; countd < cupsOfCoffee; countd++)

  {System.out.println("cup " + (countd + 1) + " has " + shots[countd] +  " shots and will cost" + (2+(shots[countd]))) ;

}
System.out.println(cupsOfCoffee + "To purchase");
System.out.println("Purchase price= "  ) ;


System.out.println("Proceed to payment? (y/n)");
String gon = keyboard.next();
char answers=gon.charAt(0);
if ((answers!= 'y') && (answers!='n'))
System.out.println("no valid");
else if (answers== 'n')
System.out.println("OK BYE");

else {
  System.out.println("ORDER PAYMENT\n-----------------");
}


}
}
}
}

我的程序还没有完成,但我已经到了计算总价的部分。为此,我想要我所有输入的总和,或者我输出总和的答案(这个等式 (2+(shots[countd]));

例如,如果我得到 3 个输出......并且将花费 (4,5,6),所以我需要 4+5+6 的总和,..我尝试了 sum+= 东西,但我认为它不起作用,或者我可能是初学者所以我无法正确使用它。所以我需要在 (PURCHASE PRICE= ) 语句

之后的一些输出

我希望你们明白我的意思,希望你们能提供一些帮助,我将不胜感激。

我刚刚在您的代码中添加了几行并尝试回答您的问题,请告诉我,以防这是您想要的。

int sum=0; // **new addition**
                for (countd = 0; countd < cupsOfCoffee; countd++)
                {
                    System.out.println("cup " + (countd + 1) + " has " + shots[countd] +  " shots and will cost" + (2+(shots[countd]))) ;
                    sum+=2+(shots[countd]); // **new addition**

                }
                System.out.println(cupsOfCoffee + "To purchase");
                System.out.println("Purchase price= " +sum ) ;// **new addition**