我正在嘗試撰寫此代碼,以便將變數的值相加,但總和始終為 0.00。任何人都可以解釋原因,因為側邊欄上沒有顯示錯誤。如果這個問題很簡單,我很抱歉,我是編碼新手。我懷疑這個問題可能是由于變數沖突引起的,但我不確定,如果是這樣,我不知道如何解決它。
import java.util.Scanner;
public class TitanicAgeSubmissions {
public static void main(String[] args) {
// Write a program for four people to enter the Titanic Belfast.
// They all must have variable ages, which the user should be able to input.
// There are four age values:
// Adult: £19
// Child (5-16): £8.50
// Child under 5: Free
System.out.println("Please enter the age of the first Visitor:");
Scanner sc= new Scanner (System.in);
double age1 = sc.nextDouble();
System.out.println("Please enter the age of the second Visitor:");
Scanner sc2= new Scanner (System.in);
double age2 = sc.nextDouble() ;
System.out.println("Please enter the age of the third Visitor:");
Scanner sc3= new Scanner (System.in);
double age3 = sc.nextDouble();
System.out.println("Please enter the age of the fourth Visitor:");
Scanner sc4= new Scanner (System.in);
double age4 = sc.nextDouble();
if (age1>16) {;
double ticketprice1 = 19; }
else if (age1 <16 && age1>=5) {
double ticketprice1= 8.50; }
else if (age1 <5) {
}double ticketprice1 = 0.00 ;
// next values
if (age1>16) {;
double ticketprice2 = 19; }
else if (age1 <16 && age1>=5) {
double ticketprice2= 8.50; }
else if (age1 <5) {
}double ticketprice2 = 0.00;
// next values
if (age1>16) {;
double ticketprice3 = 19; }
else if (age1 <16 && age1>=5) {
double ticketprice3= 8.50; }
else if (age1 <5) {
}double ticketprice3= 0.00;
// next values
if (age1>16) {;
double ticketprice4 = 19; }
else if (age1 <16 && age1>=5) {
double ticketprice4= 8.50; }
else if (age1 <5) {
}double ticketprice4 = 0.00;
double grandtotal = ticketprice1 ticketprice2 ticketprice3 ticketprice4;
System.out.println("The grand total of the tickets in pounds is:" " " grandtotal);
}
}
uj5u.com熱心網友回復:
計算錯誤的根本原因是ticketprice#變數的宣告和初始化不正確。此外,在使用相同age1的定義所有ticketprice#變數的值時存在一個錯誤。
其他錯誤/缺陷是:
- 未使用
Scanner實體的宣告sc2, sc3, sc4——只使用一個Scanner就可以了 age == 16inif陳述句的間隙- 多次使用
double var1, var2, ...代替陣列double[] ages, ticketPrices;
應該使用單獨的方法來定義年齡的價格。
所以改進后的解決方案可能如下所示:
static double getPrice(double age) {
double price = 0.0; // default value
if (age > 16) { // for adults
price = 19;
} else if (age >= 5) { // for children
price = 8.5;
}
return price;
}
public static void main(String[] args) {
int n = 4;
double[] ages = new double[n];
double[] prices = new double[n];
String[] ids = {"first", "second", "third", "fourth"};
double sum = 0.0;
Scanner sc= new Scanner (System.in);
for (int i = 0; i < n; i ) {
System.out.println("Please enter the age of the " ids[i] " Visitor:");
ages[i] = sc.nextDouble();
prices[i] = getPrice(ages[i]);
sum = prices[i];
// or a chain may be used
// sum = prices[i] = getPrice(ages[i] = sc.nextDouble());
}
System.out.println("The grand total of the tickets in pounds is: £" sum);
}
測驗運行的輸出:
Please enter the age of the first Visitor:
4
Please enter the age of the second Visitor:
16
Please enter the age of the third Visitor:
21
Please enter the age of the fourth Visitor:
9
The grand total of the tickets in pounds is: £36.0
uj5u.com熱心網友回復:
您的問題是因為您必須在 if 之前宣告tickerprice 變數,并且您應該注意大括號,因為您將其中一些放在錯誤的位置。你應該學習正確使用分號 ( ; ) 你在 if(...) { ; ,但沒有必要在 if 之后寫它們。我建議你更仔細地寫代碼,并盡量讓你的代碼更具可讀性,最后,這不是javascript,多學習編程語言,下次一切都會好起來的。這是您的問題的解決方案:
import java.util.Scanner;
public class TitanicAgeSubmissions {
public static void main(String[] args) {
// Write a program for four people to enter the Titanic Belfast.
// They all must have variable ages, which the user should be able to input.
// There are four age values:
// Adult: £19
// Child (5-16): £8.50
// Child under 5: Free
System.out.println("Please enter the age of the first Visitor:");
Scanner sc= new Scanner (System.in);
double age1 = sc.nextDouble();
System.out.println("Please enter the age of the second Visitor:");
Scanner sc2= new Scanner (System.in);
double age2 = sc.nextDouble() ;
System.out.println("Please enter the age of the third Visitor:");
Scanner sc3= new Scanner (System.in);
double age3 = sc.nextDouble();
System.out.println("Please enter the age of the fourth Visitor:");
Scanner sc4= new Scanner (System.in);
double age4 = sc.nextDouble();
double ticketprice1 = 0.0, ticketprice2 = 0.0, ticketprice3 = 0.0, ticketprice4 = 0.0;
if (age1>16) {
ticketprice1 = 19;
}
else if (age1 <16 && age1>=5) {
ticketprice1= 8.50;
}
else if (age1 <5) {
ticketprice1 = 0.00 ;
}
// next values
if (age2>16) {
ticketprice2 = 19;
}
else if (age2 <16 && age2>=5) {
ticketprice2= 8.50;
}
else if (age2 <5) {
ticketprice2 = 0.00 ;
}
// next values
if (age3>16) {
ticketprice3 = 19;
}
else if (age3 <16 && age3 >=5) {
ticketprice3= 8.50;
}
else if (age3 <5) {
ticketprice3 = 0.00 ;
}
// next values
if (age4>16) {
ticketprice4 = 19;
}
else if (age4 <16 && age4 >=5) {
ticketprice4= 8.50;
}
else if (age4 <5) {
ticketprice4 = 0.00 ;
}
double grandtotal = ticketprice1 ticketprice2 ticketprice3 ticketprice4;
System.out.println("The grand total of the tickets in pounds is:" " " grandtotal);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/320744.html
上一篇:顫振無法訪問靜態變數
