Java 当用户输入“错误”时,告诉它
这是我的密码。我真的一直在查看整个过程,但为了我的生命,我无法找出导致代码对我不利的问题在哪里:Java 当用户输入“错误”时,告诉它,java,Java,这是我的密码。我真的一直在查看整个过程,但为了我的生命,我无法找出导致代码对我不利的问题在哪里: import java.util.ArrayList; import java.util.Scanner; public class DogGompundTest { public static void main(String[] args) { boolean dogFound = false; boolean toEnd = false;
import java.util.ArrayList;
import java.util.Scanner;
public class DogGompundTest {
public static void main(String[] args) {
boolean dogFound = false;
boolean toEnd = false;
Scanner keyboard = new Scanner(System.in);
ArrayList<Dog> dogRegister = new ArrayList<Dog>();
//Other dogs in program
Dog d2 = new Dog("Mira", "Miniature Schnauzer", 1, 8.0, 0.8);
dogRegister.add(d2);
Dog d3 = new Dog("Jack", "Jack Russell", 3, 6.0, 1.8);
dogRegister.add(d3);
Dog d4 = new Dog("Charlie", "Pug", 5, 5.0, 2.5);
dogRegister.add(d4);
Dog d5 = new Dog("Max", "Dachshund", 9, 5.0, 3.7);
dogRegister.add(d5);
Dog d6 = new Dog("Bingo", "Golden Retriever", 5, 12.0, 6.0);
dogRegister.add(d6);
System.out.println("Name - Race - Age - Weight - Taillength: " + dogRegister);
while (toEnd == false) {
System.out.println(
"\nWhat would you like to do? \n Press 1 to register a dog. \n Press 2 to get a " +
"look at the taillengths of the dogs. \n Press 3 to delete a dog from " +
"the register.\n Press 4 to quit.");
int command = keyboard.nextInt(); //Alternatives stored in "command"
keyboard.nextLine();
switch (command) { //Execute chosen command in switch-statement
case 1: //User registers a dog
Dog d1 = new Dog("", "", 0, 0.0, 0.0);
System.out.println("\nPlease enter the dogs name:");
String Name = keyboard.next();
d1.setName(Name);
System.out.println("\nPlease enter the dogs race (in English):");
String Race = keyboard.next();
d1.setRace(Race);
System.out.println("\nPlease enter the dogs age (years):");
int age = keyboard.nextInt();
d1.setage(age);
System.out.println("\nPlease enter the dogs weight in kg:");
double weight = keyboard.nextDouble();
d1.setweight(weight);
dogRegister.add(d1);
System.out.println("\n");
System.out.println(
"\nComplete dog information: " + "\nName: " + Name + "\nRace: " + Race
+ "\nAge: " + age + "\nWeight: " + weight + " kg");
System.out.println(dogRegister);
break;
case 2: //User gets to see the different taillengths of the dogs in the register
System.out.println(
"\nEnter taillength and all dogs with a greater taillength will be displayed: ");
double taillength = keyboard.nextDouble();
for (int index = 0; index < dogRegister.size(); index++) {
if (taillength <= (dogRegister.get(index).gettaillength())) {
System.out.println(dogRegister.get(index));
}
}
break;
case 3: //User deletes a dog from the register
System.out.println("State the name of the dog you wish to delete from the register: ");
String delete = keyboard.nextLine();
for (int del = 0; del < dogRegister.size(); del++) {
if (delete.equalsIgnoreCase(dogRegister.get(del).getName())) {
System.out.println("\nThe dog with the given name has been deleted from the register.");
dogFound = true;
dogRegister.remove(del);
System.out.print(dogRegister + "\n");
}
}
{
if (!dogFound)
;
System.out.println("A dog by that name is not registered in our system.");
}
break;
case 4:
System.out.println("\nThe program has now ended.");
toEnd = true;
}
}
}
}
您需要将else{}块移到循环之外,并使用第二个变量确定是否找到了现有的dog,然后将其设置为true。然后您的else{}块改为读取,if!狗发现{}
否则,您的代码将打印未找到的行,每当它遇到每一只不是被寻找的狗时,我相信您会看到。e、 g
dogs = ["Jack","Pooch","Fido","Fluffy"]
如果您尝试删除Fluffy,则执行过程如下所示:
"Jack" isn't "Fluffy", print "not found."
"Pooch" isn't "Fluffy", print "not found."
"Fido" isn't "Fluffy", print "not found."
"Fluffy" is "Fluffy", remove and print "removed."
您应该重新考虑您的算法,特别是检查用于dogRegister的类的javadoc——我想它是从java.util.Collection继承的。你需要的东西已经有了。您还应该提供所有相关信息、必要的类型定义和示例数据,以重现您的问题。也许有人会调查它。dogFound=false的计算结果为false,因此println不会打印。相反,你想要!dogfund或dogfund==false。啊,现在你只是有一个语法问题。你有一个else{}不应该存在。没有匹配的if,空块,你有一个围绕赋值的if检查。不要这样做,你的空白使代码易于阅读。谢谢你的帮助,我的输出越来越好。当我写下一只狗的名字时,它会删除它,但现在当我写下一只不存在的狗的名字时,它不会打印任何东西。我再次编辑了我的第一篇文章。我已经修复了你代码的格式问题。这将帮助您找出哪里出了问题,因为for循环中仍然有一些不应该出现的问题,我说您必须移动。如果dogFound=false仍然在循环中,并且还在使用赋值而不是比较。@Caggen为什么要添加;你认为他的影响是什么?