그래서이 클래스 Food가 있고 다른 클래스 (testFood)를 사용하여 과일 배열을 출력하고 싶습니다. 내가 겪고있는 문제는 적절한 문장 구조로 null이 아닌 값을 출력하는 것입니다. 배열의 null 값을 할인하는 방법을 알아 냈습니다 (새로운 메서드 "realLength"사용). 그러나 요소 사이에 null 값이 있지만 명령문은 여전히 54 행에 문제가 있습니다. 내가 원한 방식대로 처리하지 못한다. 누군가가 이것을 바꿀 수있는 방법을 알고 있다면, 크게 감사하겠습니다!null 값을 갖는 문장으로 배열 서식 지정
public class Food{
static final int MAX_SIZE=10;
public static String[] favFruit=new String[MAX_SIZE]; //array of favourite fruit
//Set member function used to set a new favourite fruit in the array of favourite fruit
public static void addFruit(String fruit){
for(int i=0;i<MAX_SIZE;i++){
if(favFruit[i]==null){
favFruit[i]=fruit;
break;
}
}
}
//Set member function used to set a favourite fruit in the array to null, thereby removing it
public static void removeFruit(String fruit){
for(int i=0;i<MAX_SIZE;i++){
if(fruit==favFruit[i]){
favFruit[i]=null;
break;
}
}
}
//Returns the length of an array minus the amount of null values
public static int realLength(String[] arr){
int num=0;
for(int i=0;i<MAX_SIZE;i++){
if(arr[i]==null){
num++;
}
}
return MAX_SIZE-num;
}
//Prints the list of fruit in order to prove what is in the array of favFruit
public static void printFruit(String[] fruits){
//Prints no fruits and returns a statement saying why
int length=realLength(fruits);
if(length==0){
System.out.println("There are no favourite fruits.");
}
else{
System.out.print("The favourite fruits are: ");
for(int i=0; i<MAX_SIZE; i++){
//Prints the fruit without ','/'.'/'and' if and only if there is one valid fruit in the array
if(fruits[i]!=null && length==1){
System.out.print(fruits[i]+".");
}
//Prints the fruit in successive order
else if(fruits[i]!=null && fruits[i]!=fruits[length-1]){
System.out.print(fruits[i]+", ");
}
//On the last favourite fruit, this prints 'and' and '.' instead to complete the sentence
else if(fruits[i]!=null && fruits[i]==fruits[length-1]){ //Issue: doesnt work if null is between elements
System.out.print("and "+fruits[i]+".");
}
}
System.out.println();
}
}
}
public class testFood{
public static void main(String[] args){
//Add fruit to the favFruit array to test addFruit method
Food.addFruit("Orange");
//Print the array to prove the array has changed
Food.printFruit(Food.favFruit);
//Remove fruit from the favFruit array to test the removeFruit method
Food.removeFruit("Orange");
//Print the array to prove the array has changed
Food.printFruit(Food.favFruit);
//Repeat last steps to test for multiple fruit
Food.addFruit("Banana");
Food.addFruit("Apple");
Food.addFruit("Pear");
Food.addFruit("Orange");
Food.printFruit(Food.favFruit);
Food.removeFruit("Apple");
Food.printFruit(Food.favFruit);
}
}
예 출력 :
The favourite fruits are: Orange.
There are no favourite fruits.
The favourite fruits are: Banana, Apple, Pear, and Orange.
The favourite fruits are: Banana, and Pear.Orange,
없는 문제를하지만,'realLength()'반복 호출 중지합니다. 그것을 한 번 불러서 결과를 저장하십시오! – John3136
이것은 문제가 아니지만'removeFruit (String)'메소드에서 변경해야하는 것은'if (fruit == favFruit [i])'테스트입니다. 현재이 기능이 작동하는 유일한 이유는 컴파일하기 전에 모든 문자열을 선언했기 때문입니다. 모두 동일합니다. 그러나 사용자 입력을 위해 프로그램을 확장했다고 가정 해 보겠습니다. 이 메소드는 각 입력이 * new * String이며 동일하지 않기 때문에 더 이상 작동하지 않습니다. 대신에'String.equals (String)'메쏘드를 사용하십시오. 이 잠재적 인 문제점을 제거합니다. –