我的某些输出似乎无法打印。怎么解决呢?

时间:2019-03-27 15:15:37

标签: java oop

经过许多更改和改进,我的代码似乎运行良好。但是它仍然没有打印出我所有的输出。如何解决?

    import java.util.Scanner;
    public class BusPayment {
    public int number_of_adults;
    public int number_of_children;
    public double adult_price;
    public double children_price;
    public double totalprice;

    public BusPayment(){

        }

    public int getnumber_of_adults(){
     return number_of_adults;
       }

    public int getnumber_of_children(){
      return number_of_children;
       }

    public double getadult_price(){
     return adult_price;
       }

    public double getchildren_price(){
      return children_price;
        }

     public double gettotal_price(){
       return totalprice;
        }

      public void setadult_price(double adult_p){
         adult_price=adult_p; 
        }

      public void setchildren_price(double children_p){
          children_price=children_p;
        }

      public void settotalprice(double total_price){
          totalprice=total_price;
         }

      public BusPayment(double adult_p, double children_p){
          adult_price=adult_p;
          children_price=children_p;
          }

       public void BusPaymentPart(){
         number_of_adults=0;
         number_of_children=0;
         adult_price=15.00;
         children_price=10.00;
         totalprice=0.00;
          } 

       public double calculate_totalprice(){
         number_of_adults= 0;
          number_of_children= 0;
         totalprice= (number_of_adults*adult_price)+ 
         (number_of_children*children_price);
         return totalprice;
          }

       public static void main (String []args){
        BusPayment test = new BusPayment();

          Scanner input=new Scanner(System.in);
      System.out.println("Payment");
       System.out.println("The price of an adult ticket is 
         15.00.");
           System.out.println("The price of a children's ticket is 
         10.00.");

           int number_of_adults=input.nextInt();
           System.out.println("The number of adults are: " 
           +number_of_adults);
            number_of_adults=input.nextInt();

             int number_of_children=input.nextInt();
            System.out.println("The number of children are: 
       "+number_of_children);
          number_of_children=input.nextInt();

           double totalpri = test.calculate_totalprice();
          System.out.println("The total price you need to pay: " 
        +totalpri);

         }

       }

我要打印:

成人票的价格为15.00。 (已打印)

儿童票的价格为10.00。 (已打印)

成人数量是:

孩子的数量是:

您需要支付的总价:

2 个答案:

答案 0 :(得分:0)

您的main方法中还有一些额外的input.nextInt()调用。如果删除它们,则打印的值很好:

public static void main (String []args){
    BusPayment test = new BusPayment();

    Scanner input=new Scanner(System.in);
    System.out.println("Payment");
    System.out.println("The price of an adult ticket is 15.00.");
    System.out.println("The price of a children's ticket is 10.00.");

    System.out.println("Enter number of adults:");
    test.number_of_adults=input.nextInt();
    System.out.println("The number of adults are: "
            +test.number_of_adults);

    System.out.println("Enter number of children:");
    test.number_of_children=input.nextInt();
    System.out.println("The number of children are:"+test.number_of_children);

    double totalpri = test.calculate_totalprice();
    System.out.println("The total price you need to pay: "
            +totalpri);

}

其他问题:

  • 您要混合使用局部变量和实例变量,以便您 caldulate_totalprice无法正常运作-从 stdin不会传播到该类。我已将其修复 片段-您可能还想为此创建一个setter /传递构造函数参数
  • 从您的number_of_adults=0;方法中删除number_of_children=0;calculate_totalprice
  • 在构造函数中初始化机票价格,或手动调用BusPaymentPart方法

答案 1 :(得分:0)

关于您的问题,首先出现两次打印是因为它们在“ input.nextInt()”之前

当您要要求用户提供程序数据时,将使用Input.nextInt()。

因此,这里正在等待控制台中的一些输入,请尝试输入5,您将看到下一个打印结果。

希望有帮助