我很难理解如何只调用一次具有多个对象的函数(在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?
因为函数在循环中被调用了三次,所以一次只接受一个参数,而期望的输出应该是这样的:
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.
那么,我如何一次调用函数,让它一次接受所有参数呢?感谢您的帮助。谢谢
创建一个景点列表
List<Attraction> attractions = new ArrayList<>();
将每个景点添加到列表中:
attractions.add(TheEdenProject);
attractions.add(LondonZoo);
attractions.add(TateModern);
将列表传递给
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();
if (allKnown.contains(attraction_name)) {
Attraction ofInterest = allKnown.get(attraction_name);
System.out.println(ofInterest.name + " opens at " + ofInterest.open + "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
contains
假设吸引力的
被实现为所需的名称比较。如果你需要帮助,尽管问。或者在SO中搜索等于
,[java]equals
。
问题内容: 是否有可能在Java中获得对对象的所有引用。 我需要检查的是对象是否删除了所有的回调订阅。 谢谢 问题答案: 这可以通过JVMTI实现,并且通常由堆分析器完成。但是,它不能在Java内部完成。
问题内容: 我有一个在CPython实现上运行的python程序,并且在其中必须调用在Java程序中定义的函数。我怎样才能做到这一点? 能够使用一些Java对象也将是一件很棒的事。 Jython不是一个选择。我必须在CPython中运行python部分。 问题答案: 最简单的事情是 为您的Java“函数”编写一个简单的CLI。(没有这样的东西,所以我假设您实际上是指Java类的方法功能。) 以此为
当尝试将Mockito与spring一起使用时,通过bean声明创建Mock对象... ...我在调用mockito时发现了一些奇怪的行为。当多次没有重置Mock对象时,例如: 在测试过程中(在同一个模拟上)多次调用此代码(“mockito.when”)时,测试就会失败并出现错误(BadSqlGrammerException即使该异常实际上是预期的--如果不抛出异常,我会得到一个失败,并且手动抛出
我有一个模型,在几个模型中设置为外键。现在从模型中删除任何对象时,如果该对象在这些模型中被引用,则会抛出ProtectedError。我想让用户在一次操作中删除包含所有受保护对象的对象。 我只需调用 但是当有自己的受保护对象时,操作失败并抛出另一层。我想要实现的是,删除所有受保护的对象,而不区分它存在于哪一层。我知道这可能是一个危险的操作。但我能在没有复杂解决方案的情况下实现这一点吗。提前谢谢。
问题内容: 当尝试通过Spring使用Mockito时,通过bean声明创建Mock对象… …我多次调用Mockito。时发现一些奇怪的行为,而没有重置Mock对象,例如: 一旦在测试期间(在同一模拟上)多次调用此代码(“ Mockito.when”),测试就会失败并显示错误(BadSqlGrammerException,即使实际上是预期的异常,我也会失败- 如果我不抛出异常,则手动抛出该异常就可
我在尝试测试对象检测api model\u builder\u测试时遇到以下错误。皮耶。 运行model_builder_test.py后出现以下错误 ...0220 03:22:35.097244 140099951081344deprecation.py:323]从 /content/models/research/object_detection/anchor_generators/grid