我無法理解如何僅呼叫具有多個物件的函式一次(在 for 回圈中)。
我正在嘗試撰寫一個代碼,其中物件的資訊應該傳遞給另一個函式。
以下面的程式為例,該函式不是一次,而是多次呼叫。
import java.util.Scanner;
class Attraction {
String name;
int open;
}
public class attractionGuide {
public static void main(String[] args) {
attractionDetails();
System.exit(0);
}
public static Attraction createAttraction(String attractionName, int openingTime) {
Attraction a = new Attraction();
a.name = attractionName;
a.open = openingTime;
return a;
}
public static void attractionDetails() {
Attraction TheEdenProject = createAttraction("The Eden Project", 9);
Attraction LondonZoo = createAttraction("London Zoo", 10);
Attraction TateModern = createAttraction("Tate Modern", 10);
attractionInfo(TheEdenProject);
attractionInfo(LondonZoo);
attractionInfo(TateModern);
// This is where the problem is^
}
public static Attraction attractionInfo(Attraction a) {
Scanner scanner1 = new Scanner(System.in);
System.out.print("Welcome. How many attractions do you need to know about? ");
final int howMany = scanner1.nextInt();
for (int i = 1; i <= howMany; i ) {
System.out.print("\nName of attraction number number " i "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
if (attraction_name.equalsIgnoreCase(a.name)) {
System.out.println(a.name " opens at " a.open "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
return a;
}
}
示例輸出:
Welcome. How many attractions do you need to know about? 3
Name of attraction number number 1?: the eden project
The Eden Project opens at 9am.
Name of attraction number number 2?: tate modern
I have no information about that attraction.
Name of attraction number number 3?: london zoo
I have no information about that attraction.
Welcome. How many attractions do you need to know about?
似乎因為該函式在回圈中被呼叫了 3 次,它一次只接受一個引數,而所需的輸出應該是這樣的:
預期輸出:
Welcome. How many attractions do you need to know about? 3
Name of attraction number number 1?: the eden project
The Eden Project opens at 9am.
Name of attraction number number 2?: tate modern
Tate Modern opens at 10am.
Name of attraction number number 3?: london zoo
London Zoo opens at 10am.
那么我如何去呼叫一次函式,以便一次獲取所有引數?任何幫助表示贊賞。謝謝。
uj5u.com熱心網友回復:
創建一個List景點
List<Attraction> attractions = new ArrayList<>();
將每個景點添加到串列中:
attractions.add(TheEdenProject);
attractions.add(LondonZoo);
attractions.add(TateModern);
傳遞List給attractionInfo
attractionInfo(attractions);
定義attractionInfo為
// Didn't see why you needed to return Attraction
public static void attractionInfo(List<Attraction> allKnown)
并使用它:
Scanner scanner1 = new Scanner(System.in);
System.out.print("Welcome. How many attractions do you need to know about? ");
final int howMany = scanner1.nextInt();
for (int i = 1; i <= howMany; i ) {
System.out.print("\nName of attraction number number " i "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
Attraction lookup = createAttraction(attraction_name,0);
if (allKnown.contains(lookup)) {
Attraction ofInterest = allKnown.get(allKnown.indexOf(lookup));
System.out.println(ofInterest.name " opens at " ofInterest.open "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
該contains假定equals的Attraction作為所需的名稱比較實作的。如果您需要幫助,請直接詢問。或[java] equals在 SO 中搜索。
uj5u.com熱心網友回復:
你需要一些集合來迭代,就像一個串列。
List<Attraction> attractions = Arrays.asList(
createAttraction("The Eden Project", 9),
createAttraction("London Zoo", 10),
createAttraction("Tate Modern", 10)
);
for (Attraction attraction : attractions) {
attractionInfo(attraction);
}
或者,更高級
Stream.of(
createAttraction("The Eden Project", 9),
createAttraction("London Zoo", 10),
createAttraction("Tate Modern", 10)
)
.forEach(attractionGuide::attractionInfo);
uj5u.com熱心網友回復:
問題是你要呼叫你的回圈三遍。
attractionInfo(TheEdenProject); // This creates a loop
attractionInfo(LondonZoo); // Ditto
attractionInfo(TateModern); // Ditto
如果你想回圈一次,你必須只呼叫一次回圈。嘗試創建回圈并讓它呼叫檢查吸引力的方法。
// Closer to main()
Attraction a;
for (int i = 1; i <= howMany; i ) {
System.out.print("\nName of attraction number number " i "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
// Add this
if ( (a = attractionInfo(a.name)) != null ) {
System.out.println(a.name " opens at " a.open "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
uj5u.com熱心網友回復:
傳遞陣列或集合以進行迭代。最簡單的更改如下,但您也可以使用 Map 按名稱獲取資訊,而不是遍歷整個集合。
import java.util.Scanner;
public class AttractionGuide {
static class Attraction {
String name;
int open;
Attraction(String name, int open) {
this.name = name;
this.open = open;
}
}
public static void main(String[] args) {
attractionDetails();
System.exit(0);
}
public static void attractionDetails() {
Attraction TheEdenProject = new Attraction("The Eden Project", 9);
Attraction LondonZoo = new Attraction("London Zoo", 10);
Attraction TateModern = new Attraction("Tate Modern", 10);
queryAttractionInfo(TheEdenProject, LondonZoo, TateModern);
}
public static void queryAttractionInfo(Attraction ... attractions) {
Scanner scanner1 = new Scanner(System.in);
System.out.print("Welcome. How many attractions do you need to know about? ");
final int howMany = scanner1.nextInt();
for (int i = 1; i <= howMany; i ) {
System.out.print("\nName of attraction number number " i "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
boolean found = false;
for (Attraction a : attractions) {
if (attraction_name.equalsIgnoreCase(a.name)) {
System.out.println(a.name " opens at " a.open "am.");
break;
}
}
if (!found) {
System.out.println("I have no information about that attraction.");
}
}
}
}
uj5u.com熱心網友回復:
嗯,有這么多答案。
我認為這是你的意思:
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Welcome. Enter attractions count:");
int count = scanner.nextInt();
System.out.println("count: " count);
attractionInfo(count, "Jim", 10);
attractionInfo(count, "Tom", 2);
attractionInfo(count, "Jack", 4);
scanner.close();
}
public static void attractionInfo(int count, String name, int open) {
for (int i = 1; i <= count; i ) {
System.out.print("Enter a name:");
Scanner scanner = new Scanner(System.in);
String attraction_name = scanner.nextLine();
if (attraction_name.equalsIgnoreCase(name)) {
System.out.println( name " opens at " open "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
System.out.println("=================");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/376519.html
上一篇:為什么我在reactjs中的功能組件映射一個空陣列?
下一篇:什么時候參考鑄造切片物件?
