錯誤:如果我將<identifier> expected for line 72
其
public static double calculatePrice(choice, t)
保留為 calculatePrice (int, int) 我仍然會收到相同的錯誤,任何幫助或想法如何解決此問題
我嘗試將方法更改為 if 我將其保留為 calculatePrice (int choice, int t) 但這只會產生更多錯誤。
import java.util.Scanner;
public class KosakowskiTickets3
{
public static void main(String[] args )
{
Scanner key = new Scanner (System.in);
double price;
double total;
int choice;
int t;
displayPrices();
getStadium(choice);
getNumTickets(t);
calculatePrice(choice, t);
displayTotal();
}
//Prices
public static void displayPrices()
{
System.out.println("Welcome to the US Open");
System.out.println("New York is where dreams come true\n");
System.out.println("Ticket prices are: ");
System.out.println("\t1. Arthur Ashe: \t\t$450.25");
System.out.println("\t2. Louis Armstron: \t\t$225.75");
System.out.println("\t3. Grandstand: \t\t$175.50\n");
}
// user choice
public static int getStadium();
{
Scanner key = new Scanner(System.in);
int choice;
System.out.println("Please choice 1, 2, or 3: ");
choice = key.nextInt();
while (choice <= 3)
{
System.out.println("Please enter a valid choice.");
choice = key.nextInt();
}
return choice;
}
// Tickets
public static int getNumTickets()
{
Scanner key = new Scanner (System.in);
int t;
System.out.print("Please note that tickets are limited to 10 per stadium. \nEnter number of tickets you would like to purchase: ");
t = key.nextInt();
while (t <=10)
{
System.out.println("Please enter a valid number of tickets.");
t = key.nextInt();
return t;
}
}
// Price math
public static double calculatePrice(int, int)
{
int choice;
int t;
switch (choice)
{
case 1:
price = 225.25;
total = price * t;
break;
case 2:
price = 117.75;
total = price * t;
break;
case 3:
price = 145.50;
total = price * t;
break;
default:
System.out.println("That is not a choice.");
double price;
double total;
return total;
}
}
// Price Total
public static void displayTotal(double total)
{
System.out.println("Your total ticket price is $" total);
}
}
uj5u.com熱心網友回復:
因為您的源代碼中有拼寫錯誤。
public static int getStadium(); {
結尾的那個分號是非法的。擺脫它。
public static double calculatePrice(int, int)
這不是一個有效的方法宣告。引數的形式為Type name
,因此:
public static double calculatePrice(int choice, int t) {
... code here
}
例如。你是說:任何呼叫這個calculatePrice
方法的人都必須通過寫calculatePrice(a, b)
where a 和 b 是運算式來做到這一點;a 和 b 都是 int 型別。好的,這些運算式決議為的值將傳遞給您的calculatePrice
方法 - 您希望如何在代碼中參考它們calculatePrice
?你必須選擇一個名字。這就是choice
和t
關于。然后,您在calculatePrice
. 我敢打賭你不想要那個。
引數完全是區域區域變數,除了由呼叫者設定而不是您設定:
public static int example1(int a, int b) {
return a b;
}
public static int example2() {
int a = 5;
int b = 10;
return a b;
}
這些方法幾乎相同,但是一個添加了給定的 2 個引數,另一個沒有引數。它們在其他方面的行為相同 - 兩者a
都是b
這兩種方法中的區域變數名。
uj5u.com熱心網友回復:
您的代碼中有幾個錯誤。
錯誤一:
在“calculatePrice”方法的引數中,您沒有給出任何名稱。命名這兩個引數將解決問題。
您需要提供名稱及其資料型別。
錯誤2:
在將這些變數作為引數發送到相關方法之前,您沒有為變數 'choice'、't' 設定任何值。
在使用它們之前用一個值初始化這些變數
錯誤 3:
從主方法你呼叫方法' getStadium
'像這樣=>
getStadium(choice);
但是根據這個方法的定義,它不帶任何引數。
此外,從實作來看,這個方法似乎不應該接受任何引數。
我猜你試圖在這個方法中接受輸入,然后將它回傳到 main 方法以存盤為變數'choice'。
為此,您需要這樣寫 =>
choice = getStadium();
getNumTickets
從主方法呼叫方法“ ”時,您犯了同樣的錯誤。
像這樣稱呼它=>
t = getNumTickets();
錯誤 4:
在方法 ' getStadium
' 的定義中存在語法錯誤。
您不應該在方法名稱的末尾添加分號 (;)。
public static int getStadium();
洗掉最后添加的分號。
讓我們看看一些邏輯錯誤。
錯誤 5:方法 getStadium
在這里,只要輸入從 1 到 3,您就可以從用戶那里獲取輸入。但是在回圈的條件下,您正在檢查輸入是否在 3 以內。
所以應該是這樣的=>
public static int getStadium() {
Scanner key = new Scanner(System.in);
int choice;
System.out.println("Please choice 1, 2, or 3: ");
choice = key.nextInt();
while (choice <1 || choice > 3) {
System.out.println("Please enter a valid choice.");
choice = key.nextInt();
}
return choice;
}
錯誤 6:方法 getNumTickets
在這種方法中,您犯了類似錯誤5。
有效輸入是從 1 到 10。
只要輸入 <= 10,您就嘗試運行 while 回圈。
您在這里犯的另一個錯誤是,您從 while 回圈中回傳了值。
這個方法應該是這樣的=>
public static int getNumTickets() {
Scanner key = new Scanner(System.in);
int t;
System.out.print("Please note that tickets are limited to 10 per stadium. \nEnter number of tickets you would like to purchase: ");
t = key.nextInt();
while (t < 1 && t > 10) {
System.out.println("Please enter a valid number of tickets.");
t = key.nextInt();
}
return t;
}
uj5u.com熱心網友回復:
首先,您必須為方法引數型別命名,然后如果方法是 return(double) 型別,則如果要宣告變數并在方法中使用它們,則最終不能在其中列印訊息,您應該首先這樣做。所以代碼是:
package stackoverflow;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
double result = calculatePrice(4,1);
if (result != -1)
System.out.println(result);
else
System.out.println("That is not a choice.");
}
public static double calculatePrice(int choice, int t)
{
double price = 0;
double total = 0;
switch (choice)
{
case 1:
price = 225.25;
total = price * t;
return total;
case 2:
price = 117.75;
total = price * t;
return total;
case 3:
price = 145.50;
total = price * t;
return total;
default:
return -1;
}
}
}
對不起我的英語
uj5u.com熱心網友回復:
自從我使用 Java 以來已經有一段時間了,但你不應該初始化你的變數嗎?例如“int 選擇 = t”。
這可能會對您有所幫助:https ://rollbar.com/blog/how-to-handle-the-identifier-expected-error-in-java/#
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/521544.html
標籤:爪哇
上一篇:輸入錯誤的布爾方法