【发布时间】:2016-01-07 13:27:12
【问题描述】:
所以我遇到了一个问题,我必须在来自队列的单词中找到特定数量的字母,例如一个苹果有 2 个字母 p,所以我必须打印出例如“总金额单词中以 p:2" 开头的字母。在那之后,我不知道如何打印和计算特定的字母。我的老师建议我使用 arraylist 方法来计算字母,但我仍然不知道该怎么做。如果我的代码看起来很乱,请原谅我,我对编程还是很陌生。 到目前为止,这是我的一些代码:
package fruits;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
public class Fruits {
public static void main(String[] args) {
Queue myQueue = new Queue(80);
for (int j = 0; j < num; j++) {
System.out.println("Enter the fruits you like to eat" + j + ":");
String input = sc.nextLine();
myQueue.enqueue(input);//enqueue the fruits
}
System.out.println("List of Fruits i like to eat: ");
for (int x = 0; x < num; x++) {
System.out.print(x);
if (!myQueue.isEmpty()) {
String input = (String) myQueue.dequeue();
System.out.println(input);
}
}
ArrayList<Queue> list = new ArrayList<Queue>();
list.add(myQueue);
System.out.println("Fruits that start with a letter p:");
}
}
【问题讨论】: