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

时间:2014-11-20 20:35:22

标签: 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 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中。

1 个答案:

答案 0 :(得分:5)

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

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

&#34;执行更多操作:永远无法运行,因为遇到return value时函数会立即停止运行。据说这段代码无法访问。  将代码放在多个函数中,每个函数只有一个返回值,然后从主函数或者根据需要在任何地方调用这些函数