2016-04-29 19 views
0

我想弄清楚爲什麼我的程序似乎沒有進入我的方法之一:assignKennel()。相反,它始終要求更多的輸入,即使while循環中要做的更多。任何意見將不勝感激。我不確定要包括什麼,所以這裏是我的全部。程序不進入方法

package finalbarking; 

    import java.util.InputMismatchException; 
    import java.util.Scanner; 


    public class Finalbarking { 

    //global int numberOfDogs, numberOfKennels; 
    static int numberOfDogs, numberOfKennels=8; 
     //int dogID 
     static int [] dogIDs= new int [15]; 
      //add provided ID's 

     //String dogNames 
     static String [] dogNames= new String [15]; 

      //add provided dogs to array 


     //int dogWeights 
     static int [] dogWeights= new int [15]; 
      //add provided weights to array 
     //at start, this is empty. 
     //array size = 8 elements myArray = new int[7] 
     //int KennelNumber 
     static int [] kennelNumbers= new int [8]; 
     //String kennelWeight(SMALL/MEDIUM/LARGE) 
     static String [] kennelWeight= new String [3]; 

     //int dogInKennelID 
     static int [] dogInKennelID= new int [8]; 
     static int inputDogID=0; 

    public static void main(String[] args) throws InterruptedException { 
    //vars 
    //Scanner 
    Scanner response= new Scanner(System.in); 
    //int inputDogID 

    dogIDs[0]=1001; 
    dogIDs[1]=1003; 
    dogIDs[2]=1007; 
    dogIDs[3]=1008; 
    dogIDs[4]=1012; 
    dogIDs[5]=1034; 
    dogIDs[6]=1038; 
    dogIDs[7]=1087; 
    dogIDs[8]=1088; 
    dogIDs[9]=1120; 
    dogIDs[10]=1129; 
    dogIDs[11]=1145; 
    dogIDs[12]=1200; 
    dogIDs[13]=1211; 
    dogIDs[14]=1222; 
    dogNames[0]= "Bowser"; 
    dogNames[1]= "Ginger"; 
    dogNames[2]= "Molly"; 
    dogNames[3]= "Murphy"; 
    dogNames[4]= "Roxy"; 
    dogNames[5]= "Samantha"; 
    dogNames[6]= "Duke"; 
    dogNames[7]= "Pookie"; 
    dogNames[8]= "Abby"; 
    dogNames[9]= "Barney"; 
    dogNames[10]= "Autumn"; 
    dogNames[11]= "Hershey"; 
    dogNames[12]= "King"; 
    dogNames[13]= "Bosco"; 
    dogNames[14]= "Daisy"; 
    dogWeights[0]=130; 
    dogWeights[1]=80; 
    dogWeights[2]=45; 
    dogWeights[3]=18; 
    dogWeights[4]=70; 
    dogWeights[5]=12; 
    dogWeights[6]=90; 
    dogWeights[7]=16; 
    dogWeights[8]=35; 
    dogWeights[9]=65; 
    dogWeights[10]=20; 
    dogWeights[11]=100; 
    dogWeights[12]=110; 
    dogWeights[13]=70; 
    dogWeights[14]=55; 
    kennelWeight[0] = "SMALL"; 
    kennelWeight[1] = "MEDIUM"; 
    kennelWeight[2]= "LARGE"; 

       //reset at end 

    //Greeting for employee 
     System.out.println("Hello Barking Lot Employee!"); 
    //begin loop 
    while(inputDogID!=9999){ 
    //try 
    try{ 
    //prompt for dog id, 9999 as sentinel value 
     System.out.println("Please enter the dog's ID number, or '9999' to quit)"); 
     //capture 
     inputDogID= response.nextInt(); 
    //end try start catch exception 
    }catch(InputMismatchException error){ 
     //Invalid 
       System.err.println("You've entered an invalid Dog ID. Try again(Please refer to our Dog Client List, they are all 4 digits long)."); 
       //pause program 
       Thread.sleep(10); 
       //reset scanner 
       response = new Scanner(System.in); 
    }//end catch 

    int foundDogID = 0; 

    for(int count=0; count<15; count++) 
    { 
     if(dogIDs[count] == inputDogID){ 
      foundDogID = count; 
     }//end if 
    }//end for 

    currentClients(dogIDs,dogNames,dogWeights,inputDogID); 
    //assignKennel() 
    assignKennel(foundDogID); 
    //displayKennel(); 
    displayKennel(); 


    }//endwhile 

    }//end main 
    //User Defined Methods 
    //CurrentClients- Identifies dog based on list of current clients 
    public static void currentClients(int[]dogIDs,String[]dogNames,int[]dogWeights,int inputDogID){ 
     //Takes 3 arrays(ID, NAME, WEIGHT) 
     //Takes inputDogID 
     //Iterate the array and check if inputDogID == ID[count] 
     //for(int count = 0; count < numberOfDogs; count++) 
     for(int count = 0; count <= numberOfDogs; count ++){ 



      //if(inputDogID == ID) then return NAME + WEIGHT; 
      if(inputDogID==dogIDs[count]){ 
       System.out.println("Dog Name : "+dogNames[count]+"\tDog Weight: "+ dogWeights[count]); 
      }//end if 
      //else then return 0 and display an error 
      else 
       System.out.println("Did not find a dog matching this ID Number."); 

     }//end for 
    }//endCurrentClients 
    //assignKennel-assigns dogs to kennels 
    public static void assignKennel(int dogIDs){ 


     //takes 1 int dogID 
     //Iterate the array and check: 
     //var 
     boolean didDogGetKennel=false; 
     for(int count = 0; count < numberOfKennels; count++){ 
     //if(getDogWeightClass(dogWeights[dogID]).equals(kennelWeight[count])) 
     String weightClass = getDogWeightClass(dogWeights[dogIDs]); 
     for(int counter = 0; counter < 3; counter++) 
     { 
      if(weightClass.equals(kennelWeight[counter])) 
      { 
       if(dogInKennelID[count] == 0){ 
         //then EMPTY, assign 
          didDogGetKennel=true; 
          dogInKennelID[count]=dogIDs; 
        }//end if 
       } 
     } 

     }//end for loop 
      //if didDogGetKennel=false display message: "I'm sorry all ken 
      if(didDogGetKennel==false) 
       System.out.println("I'm sorry, all of our kennels for that weight limit are filled."); 
    }//end assignKennel 
//displayKennel- displays the eight kennel numbers &the dog assigned to each 
    public static void displayKennel(){ 
    // print if all kennels are filled 
    if(areKennelsFull()){ 
      for(int count =0; count<=8; count ++) 
      System.out.println(kennelNumbers[count] + getDogName(dogInKennelID[count])); 
    }//end if 
//else if print if sentinel value is enter 
    else if(inputDogID==9999){ 
      for(int count =0; count<=8; count ++) 
      System.out.println(kennelNumbers[count] + getDogName(dogInKennelID[count])); 
    }//end else if 
    }//end displayKennel 
//getDogWeightClass() 
    public static String getDogWeightClass(int dogWeight){ 
     //take int dog weight 
     //if dog weight < 50, then SMALL 
     if(dogWeight<50) 
      return "SMALL"; 
     //else if dog weight >50 &&<100 then MEDIUM 
     else if(dogWeight>50 &&dogWeight <100) 
      return "MEDIUM"; 
     //else then LARGE 
     else 
      return "LARGE"; 
     //return String S/M/L 


    }//end getDogWeightClass 
    //getDogName() 
    public static String getDogName(int dogInKennelID){ 
     //take dogInKennelID 
     return dogNames[dogInKennelID]; 

    }//end getDogName 
    //areKennelsFull() 
    public static Boolean areKennelsFull(){ 
    for(int count =0; count<=8; count ++){ 
     if (dogInKennelID[count] ==0) 
      return false; 
     //return boolean false 
     //else return boolean true 
     else 
      return true; 

    }//end for 
      return false; 
    }//end areKennelsFull 

}//end class 

回答

0

它實際上運行assignKennel()。如果您在其中放入System.out.println()語句來驗證該方法是否運行,您會看到它的確存在。問題不在於這種方法;每種方法都按照它應該運行。

你的問題是在displayKennel()。它沒有做任何事情。 areKennelsFull()評估爲falseinputDogID==9999(假設輸入了其他ID)。我猜areKennelsFull()在輸入一個有效的ID後應該計算爲true,所以這個問題的根本原因很可能是在areKennelsFull()或者assignKennels()錯誤地分配了dogInKennelID某處的某個值。

+0

看,它確實打印和結束,但它看起來像它沒有分配到正確的狗窩。它只分配一個,爲0.如果輸入了另一個ID,它會出現毛刺。 –

+0

好的,我想我已經找到你的問題了。這實際上是主要方法。這是在for循環中的語句:'foundDogID = count'。如果你輸入1001,那麼'foundDogID'爲0,因爲'dogIDs [0]'等於1001.這個值然後被傳遞給'assignKennel()',然後在'dogInKennelID'中的某處分配0。 'areKennelsFull()'看到這個值是0,因此返回'false',這導致你遇到的問題。 –

+0

我注意到的另一個問題是'ArrayIndexOutOfBoundsException'。這發生在'displayKennel()'這裏:'for(int count = 0; count <= 8; count ++)'。因爲'kennelNumbers [8]'和'dogInKennelID [8]'不存在,for循環的終止條件應該是'count <8'。請記住,在Java中計數是基於零的,所以一個大小爲8的數組的索引將爲0-7,而不是1-8。 –

0

它確實進入該例程,但我看到的一個問題是您超出了數組索引。對於應讀爲「count < 8」(刪除「=」)的語句,您的條件爲「count < = 8」。當它試圖使用索引8時,它會炸彈,所以程序無法正常完成。數組索引從0到7,所以8是無效的。