gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:16:32 24 4
gpt4 key购买 nike

所以我制作了一个程序,根据多种因素计算机票费用我将在下面发布我遇到的问题

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;
}
}
}
}
}

问题是最后一个花括号有错误,所以当我删除它时,从 return adults 到 down 的所有内容都被认为是无法访问的代码。

对于问题中的大量文字表示歉意。顺便说一句,我在 Java 中。

最佳答案

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

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

“do more things: 将永远无法运行,因为函数一遇到返回值就停止运行。这段代码被称为不可访问。 将你的代码放在多个函数中,每个函数只有一个返回值,然后从你的主函数或任何你想在必要时使用它们的地方调用这些函数

关于java - 无法访问的代码,编译错误恶性循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27049059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com