Java 无法访问代码,编译错误恶性循环

Java 无法访问代码,编译错误恶性循环,java,compilation,Java,Compilation,因此,我制作了一个程序,根据一些因素计算门票成本,我将在下面发布我得到的问题 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

因此,我制作了一个程序,根据一些因素计算门票成本,我将在下面发布我得到的问题

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;
}
}
}
}
}
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入票数(最多10张):”;
int numberOfTickets=input.nextInt();
如果(票数>10){
System.out.println(“请选择少于10张票”);
}否则{
双倍成本=路线();
双倍总成本=(成本*票数);
System.out.println(“”);
System.out.printf(“您的总成本为:”,totalCost);
}
}
//声明计算行驶距离的新方法
公共静态双路由(){
扫描仪输入=新扫描仪(System.in);
int[]路线={7,12,13,17,22,26};//每站之间的英里数
System.out.println(“输入第一站编号(0-5):”;
int firstStation=input.nextInt();
System.out.println(“输入最后一个站号(0-5):”;
int lastStation=input.nextInt();
整数总英里数=0;
对于(int i=firstStation;i0岁)){
成本=(成本*0.25);
}如果((年龄<16岁)和&(年龄>=10岁)){
成本=(成本*0.5);
}否则,如果(年龄>60岁){
成本=(成本*0.4);
}
System.out.println(“您是学生吗,如果是,请输入1,如果不是,请输入2”);
int studentPass=input.nextInt();
布尔学生=假;
如果(学生通行证==1)
{
学生=真;
}
如果(学生通行证==2){
成人++;
}
退货成本;
}
}
}
}
}
问题是最后一个花括号中有一个错误,所以当我删除它时,从return成人到down的所有内容都被认为是无法访问的代码


对问题中的大量文本表示歉意。顺便说一句,我用的是java。

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

public int function(){
value = 0
//do stuff
return value
//do more things
}
“执行更多操作:将永远无法运行,因为一旦遇到
返回值
,函数就会停止运行。此代码被称为无法访问。”。
将代码放入多个函数中,每个函数只返回一次,然后从主函数调用这些函数,或者在需要时从任何地方调用这些函数

如果您不有条件地返回成人,下面的代码将永远无法访问。编译器正试图在此帮助您…我怀疑您想将其分为两种方法。请重新会员选择一个最佳答案感谢非常好的清晰答案,我现在通过删除return成人声明对其进行排序。一种相关的问题,因为我在第一种方法中有数值numberOfTickets,是否仍然存在将数值输入公共静态双路径()的问题因为我想创建一个for循环,其中票证的数量是我能达到的高度的限制???是的,您将该值作为函数参数传入,如
public static double route(int numberOfTickets)
然后在函数中使用变量
numberOfTickets
。调用函数时必须传入此值,否则编译器会抱怨