Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 编辑:我重做我的编码。它现在运行正常,但我的一些输出没有打印_Java_Oop - Fatal编程技术网

Java 编辑:我重做我的编码。它现在运行正常,但我的一些输出没有打印

Java 编辑:我重做我的编码。它现在运行正常,但我的一些输出没有打印,java,oop,Java,Oop,我正在做一个支付代码。但它不起作用。我怎样才能让它工作 编辑:现在我已经改变了我的代码一点。它现在可以运行了,但仍然没有打印出我想要的输出。它只打印出我想要的所有输出中的2行。我不知道问题出在哪里,但我认为这是主要问题。有什么建议吗 import java.util.Scanner; public class BusPayment { public int number_of_adults; public int number_of

我正在做一个支付代码。但它不起作用。我怎样才能让它工作

编辑:现在我已经改变了我的代码一点。它现在可以运行了,但仍然没有打印出我想要的输出。它只打印出我想要的所有输出中的2行。我不知道问题出在哪里,但我认为这是主要问题。有什么建议吗

        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 RM15.00.");
    System.out.println("The price of a children's ticket is RM10.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元。这是印刷的

成人人数如下:

儿童人数如下:

您需要支付的总价格:

付款成功


在尝试打印值后,您似乎正在计算这些值:

System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);
calculate_totalprice();
另外,您正在从静态范围内调用非静态方法calculate_totalprice:

public static void main (String []args){
    ...
    calculate_totalprice(); 
}

我想你可能需要先进行计算

calculate_totalprice();

System.out.println("The number of adults are: " + number_of_adults);
System.out.println("The number of children are: " + number_of_adults);
System.out.println("The total price you need to pay: " + totalprice);

请添加更多代码如何从静态方法调用实例方法?我没有包含的代码是声明、访问器、变异器和构造函数,因为它们太长了。我应该创建另一种方法吗?因为要打印它,它必须在main方法中,对吗?如果使用System.out.*,您可以从代码中的任何位置打印。您正在创建扫描仪,但没有使用它从用户检索输入。我怀疑还有更多的代码,我们需要看到,以解决您的问题。请提供一份说明您的问题的报告。目前,您似乎正在尝试在计算/分配变量值之前使用变量。另外,您正在从静态范围调用非静态方法。您需要一个对象的实例来调用该对象上的非静态方法。我是否应该将整个计算价格移到主方法中?但它仍然不起作用。或者我应该把总价格计算放在主方法中?