Java 如何使用用户输入比较列表中的两种不同类型?
我目前正在做一个程序的一部分,该程序提示用户将新的巡航添加到现有的巡航列表中。 当前的列表如下所示:Java 如何使用用户输入比较列表中的两种不同类型?,java,arrays,list,Java,Arrays,List,我目前正在做一个程序的一部分,该程序提示用户将新的巡航添加到现有的巡航列表中。 当前的列表如下所示: public static void initializeCruiseList() { Cruise newCruise = new Cruise("Southern Swirl", "Candy Cane", "Miami", "Cuba", "Miami"); cruiseList.add(newCruise); Cruise newCruiseTwo = new
public static void initializeCruiseList() {
Cruise newCruise = new Cruise("Southern Swirl", "Candy Cane", "Miami", "Cuba", "Miami");
cruiseList.add(newCruise);
Cruise newCruiseTwo = new Cruise("Hippy Train", "Ole Bertha", "Baltimore", "The 70s", "Baltimore");
cruiseList.add(newCruiseTwo);
}
public static void initializeShipList() {
add("Candy Cane", 20, 40, 10, 60, true);
add("Peppermint Stick", 10, 20, 5, 40, true);
add("Bon Bon", 12, 18, 2, 24, false);
add("Candy Corn", 12, 18, 2, 24, false);
}
我遇到的问题是“addCruise”中“Ship name”部分的输入需要检查以确保该船已经存在,并且还需要检查该船是否正在使用,该船是否作为布尔值存在于shipList中。船舶列表如下所示:
public static void initializeCruiseList() {
Cruise newCruise = new Cruise("Southern Swirl", "Candy Cane", "Miami", "Cuba", "Miami");
cruiseList.add(newCruise);
Cruise newCruiseTwo = new Cruise("Hippy Train", "Ole Bertha", "Baltimore", "The 70s", "Baltimore");
cruiseList.add(newCruiseTwo);
}
public static void initializeShipList() {
add("Candy Cane", 20, 40, 10, 60, true);
add("Peppermint Stick", 10, 20, 5, 40, true);
add("Bon Bon", 12, 18, 2, 24, false);
add("Candy Corn", 12, 18, 2, 24, false);
}
我遇到的问题是,将“addCruise”方法中的用户输入与船名和布尔变量进行比较,因为用户只输入了一个字符串。目前我拥有的是:
System.out.println("Enter ship name: ");
String newShipName = newCruiseInput.nextLine();
for(Ship eachShip: shipList) {
if((eachShip.getShipName() != newShipName) && (eachShip.getInService() == false)) {
System.out.println("This ship doesn't exist or isn't in service.");
return;
}
}
我敢肯定,当前设置的问题在于,它会在整个列表中进行迭代,无论发生什么,它都会找到一个“false”条目,使得代码看起来就像用户输入了无效条目一样,即使用户没有输入。那么,我如何在条目中查找船舶名称,然后检查以确保所选船舶当前正在使用?如果您需要更多信息,请询问,我会尽力提供更多信息。用类似的内容替换for循环中的If条件怎么样
for(Ship eachShip: shipList) {
if( (!eachShip.getShipName().equals(newShipName)) || (eachShip.getShipName().equals(newShipName) && eachShip.getInService() != false) ) {
System.out.println("This ship doesn't exist or isn't in service.");
return;
}else {
//do add ship
}
}
尝试使用标志
boolean shipFound = false;
for(Ship eachShip: shipList) {
if((eachShip.getShipName().equals(newShipName)) && (eachShip.getInService() == true)) {
shipFound=true;
break;
}
}
if(shipFound)
// Do what you want
for循环中的逻辑不正确。对于您的需求,您需要首先确定循环中的列表中是否存在该船。如果你没有找到它,你需要处理这种情况。但是如果你发现了它,你需要检查它是否在使用中。不能在一个if条件中同时合并它们。此外,请注意,对于字符串比较,您需要使用字符串的equals()方法,而不是=问题在于
for
循环中的if
条件。因为它不会迭代整个列表,只要找到第一个不匹配的结果,就会返回void
您可以使用用户输入的船名筛选与您的船名匹配的所有元素,然后检查列表是否为空
如果它为空,则表示它不存在,反之亦然
您的代码应该如下所示:
if(shipList
.stream()
.filter(!eachShip -> eachShip.getShipName().equals(newShipName)
&& !eachShip.getInService)
.collect(Collectors.toList())
.isEmpty()) {
// Not found
} else {
// Found the ship
}
我希望这是有意义的。
eachShip.getInService()==true
这不能解决您的问题吗?很遗憾,它不能。根据您的要求,您想检查是否已经存在同名的活动船舶吗?是吗?是的@trishulsinghchoudhary参考我给出的解决方案。如果该船已经存在于列表中并且处于活动状态,您将得到一个为true的标志。如果不是,你将得到false标志。在你迭代之前,你如何获得每个ship值?@Trishul Singh Choudhary,这里他已经有了一个shiplist,他需要检查现有shiplist中的newShipName。根据我的理解,没有必要使用forloop。还有第二个条件,他需要检查飞船是否处于活动状态。@Trishul Singh Choudhary,哦,对不起,我的错,编辑我的answer@Trishul辛格·乔杜里,我已经编辑了我的答案,你能改变你的否决票吗?那么就一系列的if-else语句?为什么不能合并这两个条件???只有当&&之前的第一部分为true时,才会检查&&之后的第二个条件。没有问题。@TrishulSinghChoudhary-(eachShip.getShipName()!=newShipName)和&(eachShip.getInService()==false)=>当名称不匹配时,检查船舶是否在服务中,它将使用equals()方法出现,解决了我的问题哈哈。伙计,我觉得自己很傻。只是出于好奇,列表如何直观地知道如何检查连接到飞船的正确布尔值?这仅仅是因为它在从左到右-从上到下的列表中移动的方式吗?@b我们正在对列表进行迭代。我们正在选取列表中的每个对象,并将其放入每个ship变量中。现在每个飞船都有船名和布尔值,我们检查这些值。它有意义吗?@b如果它对你有帮助,你可以点击我答案旁边的勾号来接受答案。它有意义。我似乎确实有问题。当我输入一个不是现有飞船的名字时,它会接受这个输入,而它应该拒绝这个输入。你的名字似乎起作用了。您添加的shipFound布尔值似乎是必需的。我想我可以反转if要求,不需要额外的布尔值。