我试图找出为什么我的程序似乎没有进入我的一个方法: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 :(得分:0)
它确实运行assignKennel()
。如果你在那里放一个System.out.println()
语句来验证该方法是否运行,你会看到它。问题不在于那种方法;每个方法都在运行。
您的问题出在displayKennel()
。它没有做任何事情。 areKennelsFull()
评估为false
,inputDogID==9999
也是如此(假设输入了其他ID)。我猜测areKennelsFull()
在输入有效ID之后应该评估为true
,因此问题的根本原因很可能是areKennelsFull()
或{{1}错误地在assignKennels()
中的某处分配值。
答案 1 :(得分:0)
它确实输入了该例程,但我看到的一个问题是你超出了数组索引。对于应该读作“count&lt; 8”(删除“=”)的语句,3中的条件为“count&lt; = 8”。当它试图使用索引8时,它会发生轰炸,因此程序无法正常完成。数组的索引从0到7,因此8无效。