所以我有这个类Food我想用另一个类(testFood)输出一个水果数组。我遇到的问题是在正确的句子结构中输出非空值。我已经弄清楚如何计算数组的长度,折扣数组中的空值(使用新方法" realLength"),但仍然有第54行的问题,其中有一个空值元素之间的值,但语句不按照我喜欢的方式处理它。如果有人知道如何改变这一点,我们将不胜感激!
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,
答案 0 :(得分:1)
有几种方法可以解决这个问题。您可以创建一个实际长度的新数组,其中只包含非空元素。虽然这不是最好的,因为每次你想要制作句子时你都会创建一个新数组。您可以考虑使用List字符串。列表只是一个数组,您可以添加元素并从中删除元素,并且所有顺序都会为您处理。因此,当你删除一个元素时,你不会留下null,但只是列表有点似乎会转移到一个地方。
最后,如果你想继续你目前的工作方式,我写了一个简单但有效的实现。
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
System.out.println(Food.makeSentence());
//Remove fruit from the favFruit array to test the removeFruit method
Food.removeFruit("Orange");
//Print the array to prove the array has changed
System.out.println(Food.makeSentence());
//Repeat last steps to test for multiple fruit
Food.addFruit("Banana");
Food.addFruit("Apple");
Food.addFruit("Pear");
Food.addFruit("Orange");
System.out.println(Food.makeSentence());
Food.removeFruit("Apple");
System.out.println(Food.makeSentence());
}
}
public class Food {
static final int MAX_SIZE = 10;
public static String[] favFruit = new String[MAX_SIZE];
/**
* Add's a fruit, if and only if there is a space for it.
*
* @param fruit Name of the fruit to be added.
*/
public static void addFruit(String fruit) {
for (int i = 0; i < MAX_SIZE; i++) {
if (favFruit[i] == null) {
favFruit[i] = fruit;
break;
}
}
}
/**
* Removes the specified fruit, if it does exist in the food.
*
* @param fruit Name of the fruit to be removed.
*/
public static void removeFruit(String fruit) {
for (int i = 0; i < MAX_SIZE; i++) {
//Note the use of the 'equals' method
if (fruit.equals(favFruit[i])) {
favFruit[i] = null;
break;
}
}
}
/**
* Computes the used length of the array in this class.
*
* @return The length, or count of elements, used in this class.
*/
public static int realLength() {
int length = 0;
for (int i = 0; i < MAX_SIZE; i++)
if (favFruit[i] != null)
length++;
return length;
}
public static String makeSentence() {
//Get the real length of the array
int length = realLength();
//Have a variable, used to tell how many more fruits are to be added.
int fruitsToAdd = length;
/*
The purpose of having the two variables will be seen later. But basically
the purpose is because of the appending of the word "and". If the real
length of the array is 1, the fruitsToAdd variable will be 1 too. When this
happens the word "and" will be appended even though there was only one fruit
in the first place.
*/
if (fruitsToAdd == 0)
return "There are no favourite fruits.";
//Make a StringBuilder to append everything to
StringBuilder builder = new StringBuilder();
//Append the start of the sentence to the StringBuilder, depending on how many elements there are
if (length == 1)
builder.append("The favourite fruit is: ");
else
builder.append("The favourite fruits are: ");
//Go through all the elements in the array
for (int position = 0; position < favFruit.length; position++) {
//Test if the current position of the favourite fruits is not null
if (favFruit[position] != null) {
//If this is the last fruit to add, append it with "and [fruitName]."
if (fruitsToAdd == 1)
//If the length was 1, no need to append "and"
if (length == 1)
builder.append(favFruit[position]).append(".");
else
//If there are more than 1 fruit, then append "and". Not you could easily make this one expression with a ternary statement
builder.append(" and ").append(favFruit[position]).append(".");
//Else, append the name of the fruit.
else
builder.append(favFruit[position]);
//If this is not the second last fruit (but is not the last element either), append a comma and a space for seperation.
if (fruitsToAdd > 2)
builder.append(", ");
//Decrement the amount of fruits to add.
fruitsToAdd--;
}
}
//Returns the String contents of the builder
return builder.toString();
}
}
这给了我输出:
The favourite fruit is: Orange.
There are no favourite fruits.
The favourite fruits are: Banana, Apple, Pear and Orange.
The favourite fruits are: Banana, Pear and Orange.