私のプログラムが私のメソッドの1つにステップインしていないように見える理由を理解しようとしています: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
参照してください。印刷して終了しますが、正しい犬舎に割り当てられていないように見えます。 1つだけを0に割り当てます。別のIDが入力された場合は、それが不具合になります。 –
さて、私はあなたの問題を見つけたと思います。それは実際にはメインの方法です。これはforループの 'foundDogID = count'にこの文があります。 1001を入力すると 'dogIDs [0]'が1001であるため 'foundDogID'は0になります。この値は' assignKennel() 'に渡され、' dogInKennelID'のどこかに0が代入されます。 'areKennelsFull()'はこの値が0であることを知り、結果的に 'false'を返します。これは問題を引き起こしています。 –
これに気付いたもう一つの問題は 'ArrayIndexOutOfBoundsException'です。これは 'displayKennel()'で 'for(int count = 0; count <= 8; count ++)'です。 'kennelNumbers [8]'と 'dogInKennelID [8]'は存在しないので、forループの終了条件は 'count <8'でなければなりません。 Javaでのカウントはゼロベースであることに注意してください。したがって、サイズ8の配列はインデックス1〜7ではなくインデックス0〜7を持ちます。 –