经过许多更改和改进,我的代码似乎运行良好。但是它仍然没有打印出我所有的输出。如何解决?
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。 (已打印)
成人数量是:
孩子的数量是:
您需要支付的总价:
答案 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,您将看到下一个打印结果。
希望有帮助