Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法由于某种奇怪的原因被调用了两次?_Java_Class_Loops_Methods_Constructor - Fatal编程技术网

Java 方法由于某种奇怪的原因被调用了两次?

Java 方法由于某种奇怪的原因被调用了两次?,java,class,loops,methods,constructor,Java,Class,Loops,Methods,Constructor,出于某种原因,在我添加了一个名为“Oliver”的宠物后,主菜单会打印两次,同时显示“无效选择”行。我只需要用另一双眼睛去看它,因为我已经连续看了好几个小时,一直在修正一些小错误,但都没有用 运行时的代码如下所示: /*Welcome to the pet store.Type the letter to make your selection A. List the pets in the store. B. Age up the pets C

出于某种原因,在我添加了一个名为“Oliver”的宠物后,主菜单会打印两次,同时显示“无效选择”行。我只需要用另一双眼睛去看它,因为我已经连续看了好几个小时,一直在修正一些小错误,但都没有用

运行时的代码如下所示:

     /*Welcome to the pet store.Type the letter to make your selection
      A.  List the pets in the store.
      B.  Age up the pets
      C.  Add a new pet
      D.  Adopt a pet
      E.  Quit
      C
      Please type in a name
      Oliver
      Please type in an age
      22
      Oliver has just been added to the store!
      Welcome to the pet store.Type the letter to make your selection
      A.  List the pets in the store.
      B.  Age up the pets
      C.  Add a new pet
      D.  Adopt a pet
      E.  Quit
      Invalid choice
       Welcome to the pet store.Type the letter to make your selection
       A.  List the pets in the store.
       B.  Age up the pets
       C.  Add a new pet
       D.  Adopt a pet
       E.  Quit*/
这是我的主要课程代码:

    private static void mainmenu(){
    System.out.println("Welcome to the pet store.Type the letter to make 
    your selection");
    System.out.println("A."+"  " + "List the pets in the store.");
    System.out.println("B."+"  " + "Age up the pets");
    System.out.println("C."+"  " + "Add a new pet");
    System.out.println("D."+"  " + "Adopt a pet");
    System.out.println("E."+"  " + "Quit");

    MainPets.Getuserinput();

}

public static String Getuserinput(){

    userinput=scan.nextLine();

    return userinput; 

}

   public static void main (String [] args){
    int pet3age;
    String pet3name;
    Pet Pet1=new Pet("Fido",3); 
    Pet Pet2=new Pet("Furball",1);
    Pet Pet3=null;
    int userinputint;

    MainPets.mainmenu();


     while(userinput.equals("A")||userinput.equals("B")||userinput.equals("C")||userinput.equals("D")||userinput.equals("E")){

         switch(userinput){
         case "C":

            if (Pet3!=null&&userinput.equals("C")){
                System.out.println("Sorry the store is full");
            }

            if(Pet3==null){ 
                System.out.println("Please type in a name");
                pet3name=scan.nextLine();
                System.out.println("Please type in an age");
                pet3age=scan.nextInt();
                Pet3=new Pet(pet3name,pet3age);
                System.out.println(pet3name + " has just been added to the store!");
            }
            MainPets.mainmenu();
            break;
            }
            }
        while(!userinput.equals("A")||!userinput.equals("B")||!userinput.equals("C")||!userinput.equals("D")||!userinput.equals("E")){
      System.out.println("Invalid choice");
      MainPets.mainmenu();
   }
下面是包含所有方法的类:

public class Pet {
String Name; 
String AdoptionStatus; 
int Age;

public Pet() {}

public Pet(String Name, int Age) {
    this.Name = Name;
    this.Age = Age;
}

public void SetName(String namesetup) {
    Name = namesetup;
}

public String GetName() {
    return Name;
}

public int GetAge() {
    return Age;
}

public int ageincrease() {
    return Age++;
}

public String Getadoptionstatus() {
    return AdoptionStatus;
}

 public void Setadoptionstatustonotadopted(int petnumber) {
    AdoptionStatus="not adopted";
}

public void Setadoptionstatustoadopted(int petnumber){
    AdoptionStatus="adopted";
}

}

看起来您正在尽可能多地使用
static
来练习它的功能

无论如何,请参见下面的一个最小示例,您可以在此基础上进行构建(即,如果您想“添加”新宠物,它将允许您输入“C”的次数)


它绝对不是完美的,只是很快地将它组合起来,给你一个处理它的机会。

看起来你正试图尽可能多地使用
静态
来练习它的功能

无论如何,请参见下面的一个最小示例,您可以在此基础上进行构建(即,如果您想“添加”新宠物,它将允许您输入“C”的次数)


它绝对不是完美的,只是很快将它组装起来,让您有机会使用它。

请阅读java命名公约。这几乎让人觉得你是故意违反他们的。Serioulsy:你编写代码的方式让我转过身去做其他事情……你使用的是没有开关的case吗?你应该先仔细检查编译错误……我不认为代码是编译的:缺少开关,缺少a}等等……请阅读java命名协议。这几乎让人觉得你是故意违反他们的。Serioulsy:你编写代码的方式让我转过身去做其他事情……你使用的是没有开关的case吗?你是否应该先仔细检查编译错误…我不认为代码编译:缺少开关,缺少a}等等…注意:我在
Pet
类中将
age
作为字符串,但这很容易更改。注意:我在
Pet
类中将
age
作为字符串,但这很容易更改。
static String petname, petage;
    public static void main(String[] args) {
        initialText();
        String userinput = userInput();
        while (userinput.equals("A") || userinput.equals("B") || userinput.equals("C") || userinput.equals("D") || userinput.equals("E")) {
            if(userinput.equals("C")){
                System.out.println("Please type in a name");
                petname = userInput();
                System.out.println("Please type in an age");
                petage = userInput();
                Pet p = new Pet(petname, petage);
                System.out.println(petname + " has been added to the store.");
            }
            else{
                System.out.println("Option not configured yet");
                //TODO - the rest of the options
            }
            initialText();
            userinput = userInput();
        }
    }

    public static void initialText() {
        System.out.println("Welcome to the pet store.Type the letter to make your selection");
        System.out.println("A." + "  " + "List the pets in the store.");
        System.out.println("B." + "  " + "Age up the pets");
        System.out.println("C." + "  " + "Add a new pet");
        System.out.println("D." + "  " + "Adopt a pet");
        System.out.println("E." + "  " + "Quit");
    }

    public static String userInput(){
        Scanner s = new Scanner(System.in);
        return s.nextLine();
    }