因此,我制作了一个程序,该程序根据多种因素来计算票务的成本,我将在下面发布我提出的问题

Create a program that given a number of tickets (maximum of 10),
        the type of ticket (return, one way), the passenger type (under 10, under 16,
        student, over 60, other), the selected route, and the starting and finishing
        stops (in the form of a number where n denotes stopn ),
        calculates the total cost for the journey.
        The cost of each ticket should be calculated as follows:
                • The cost per mile is 50p;
                • Under 10 travel free when accompanied by an adult;
                  otherwise, a discount of 75% is applied;
                • Under 16 get a 50% discount;
                • Students get a 25% discount;
                • Over 60’s get a 60% discount.
                Train routes should be expressed in the format:
                int [n] route = {stop2 , stop3 , ... stopNPlusOne};
                Example:
                int [4] route1 = {3, 5, 2, 6};

                 denotes a route with 5 stops:
                 the distance between stop one and two is 3 miles,
                 between stop two and three is 5 miles,
                 between stop three and four is 2,
                 and between stop four and five is 6.


我的代码如下

Scanner input = new Scanner(System.in);
        System.out.println("Enter the number of Tickets  (Max 10): ");
        int numberOfTickets = input.nextInt();

    if (numberOfTickets > 10) {
        System.out.println("Please choose less than 10 tickets");
    } else {
        double cost = route();
        double totalCost = (cost * numberOfTickets);
        System.out.println("");
        System.out.printf("Your total cost is:", totalCost);
    }
}

// DECLARE A NEW METHOD THAT CALCULATES THE DISTANCES TRAVELLED
public static double route() {
    Scanner input = new Scanner(System.in);

    int[] route = { 7, 12, 13, 17, 22, 26 }; // miles between each stop

    System.out.println("Enter the first station number(0 - 5): ");
    int firstStation = input.nextInt();

    System.out.println("Enter the last station number(0 - 5): ");
    int lastStation = input.nextInt();

    int totalMiles = 0;
    for (int i = firstStation; i < lastStation; i++) {
        totalMiles = totalMiles + route[i]; // Total miles
    }
    System.out.println(totalMiles);
    double cost = totalMiles * 0.5; // (* 0.5) because it's 50p per mile.
    System.out.println("The initial cost is £" + cost);

    System.out.println("Please enter your age");
    int age = input.nextInt();
    double totalCost = 0;
    int adults = 0;
    return adults;
    {
        {

    if ((age < 10) && (age > 0)) {
        cost = (cost * 0.25);
    } else if ((age < 16) && (age >= 10)) {
        cost = (cost * 0.5);
    } else if (age > 60) {
        cost = (cost * 0.4);
    }

    System.out.println("Are you a student, if yes enter 1 if not enter 2");
    int studentPass = input.nextInt();
    boolean Student = false;
    if (studentPass == 1)


    {
        Student = true;
    }
    if (studentPass == 2) {
        adults++;
    }

    return cost;
}
}
}
}
}


问题是最后一个花括号有一个错误,因此当我删除它时,返回的成年人和向下的所有东西都被称为无法访问的代码。

对于问题中的大量文字表示歉意。我在Java中。

最佳答案

问题是您在同一函数中有多个返回值。当执行返回时,函数将退出,这意味着以下代码将永远无法运行,因此无法访问。
因此在此:

public int function(){
value = 0
//do stuff
return value
//do more things
}


“做更多的事情:将永远无法运行,因为一旦遇到return value函数就会立即停止运行。据说此代码不可访问。
 将您的代码放入多个函数中,每个函数只有一个返回,然后从主函数或需要使用它们的任何地方调用这些函数

10-07 19:32
查看更多