Java 是否可以在另一个方法中使用一个方法的返回值?

Java 是否可以在另一个方法中使用一个方法的返回值?,java,methods,Java,Methods,你好,我正在做这个家庭作业,我应该写一个方法,从我写的其他方法中获取一些信息。e、 g.汽车类型、日期、附加值等。所以我的问题是,我能在我的最终方法中使用这些方法的返回值而不再次调用它们吗?这是我的主要方法: public class Homework3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); char customer = '

你好,我正在做这个家庭作业,我应该写一个方法,从我写的其他方法中获取一些信息。e、 g.汽车类型、日期、附加值等。所以我的问题是,我能在我的最终方法中使用这些方法的返回值而不再次调用它们吗?这是我的主要方法:

public class Homework3 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        char customer = 'O';
        int numberOfCustomers = 0;
        int totalEarning= 0;
        while(customer != 'N'){
            System.out.println("Car Type is " + promptForCarType());
            System.out.println("Car rented for " + calculateDays() + " days.");
            promptForExtras();
            System.out.printf("\nTotal : %d TL",calculateTotal() );
            totalEarning += calculateTotal();
            numberOfCustomers ++;
            System.out.println("Number of customers : " + numberOfCustomers);
            System.out.println("Total amount earned : " + totalEarning);
            while(customer != 'N' || customer != 'Y'){
                System.out.println("A new customer? (Y/N)");
                customer = input.next().charAt(0);
                customer = Character.toUpperCase(customer);
            }
        }
    }
我应该在我的计算方法中使用车型、天数和附加值。我还尝试将其他方法的返回值指定为变量,并在我的最终方法中使用它们作为参数,但没有成功。例如,我尝试编写如下内容:

public static int calculateTotal(){
            int total=0;
            int time = calculateDays();
            int rate = (time / 7)*5    + (time % 7);

            if(promptForCarType().equals("Economy"))
                total += 50*rate;
            else if(promptForCarType().equals("Midsize"))
                total += 70*rate;
            else if(promptForCarType().equals("Fullsize"))
                total += 100*rate;


            total += promptForExtras() * time;
            return total;
        }

但当我在主方法中调用calculateTotal时,它会自动再次调用calculateDays

基本上,可以做的是将calculateDays中的值传递给calculateTotal,这样您就不需要再次计算它了

System.out.println("Car Type is " + promptForCarType());
in days = calculateDays();
System.out.println("Car rented for " + days + " days.");
promptForExtras();
System.out.printf("\nTotal : %d TL",calculateTotal(days) );
然后您需要更改calculateTotal以允许它接受您要传递的值

public static int calculateTotal(int time){
    int total=0;
    int rate = (time / 7)*5    + (time % 7);
    //...

例如…

那么您会得到什么错误?当然,您可以将数据从一个方法调用返回到另一个方法调用。你的例子没有任何方法,除了Main,那么你有什么问题呢?是的,你肯定能做到。您得到的错误是什么?您编写了任何方法吗?没有错误。例如,当我尝试使用天来计算总金额时,它再次调用该方法并再次要求用户提供提取和返回日期