我想写一个程序Pizza.java,该程序使用户最多可以输入15个比萨的浇头,然后按字母顺序打印出浇头。另外,浇头应该用数字列出。
示例输出将是这样
我写的代码如下:
import java.util.*;
public class Pizza {
public static final int numbers=15;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String []toppings;
System.out.println("Enter a toping (or type quit): ");
String a= input.nextLine();
// how do I add String a to the array toppings?
int count=1;
while (!a.equals("quit")&&count<numbers){
System.out.println("Enter a topping (or type quit): ");
a= input.nextLine();
if(!a.equals("quit"))
// how do I add String a to the array toppings?
count++;
}
if(count==numbers)
System.out.println("No more toppings allowed.");
int i=1;
Arrays.sort(toppings); //sorts the array in alphabetical order
while (int i<=count){
System.out.println(i+". "+Arrays.toString(toppings));
}
if(a.equals("quit")){
Arrays.sort(toppings); //sorts the array in alphabetical order
while (int j<=count){
System.out.println(j+". "+Arrays.toString(toppings));
}
}
}
}
如何完成此代码? 任何帮助将不胜感激
答案 0 :(得分:1)
您可以使用列表而不是数组来使其更简单:
import java.util.*;
public class Pizza {
public static final int numbers = 15;
public static void main(String[] args) {
List<String> toppings = new ArrayList<>();
Scanner input = new Scanner(System.in);
int attempt;
for (attempt = 0; attempt < numbers; attempt++) {
System.out.print("Enter topping topping (or type quit): ");
String topping = input.nextLine();
if (topping.equals("quit")) {
break;
}
toppings.add(topping);
}
if (attempt == numbers) {
System.out.println("No more toppings allowed.");
}
Collections.sort(toppings);
for (int position = 0; position < toppings.size(); position++) {
System.out.println((position + 1) + ". " + element);
}
}
}
或使用数组:
import java.util.*;
public class Pizza {
public static final int numbers = 15;
public static void main(String[] args) {
String[] toppings = new String[numbers];
Scanner input = new Scanner(System.in);
int attempt;
for (attempt = 0; attempt < numbers; attempt++) {
System.out.print("Enter topping topping (or type quit): ");
String topping = input.nextLine();
if (topping.equals("quit")) {
break;
}
toppings[attempt] = topping;
}
if (attempt == numbers - 1) {
System.out.println("No more toppings allowed.");
} else {
// Remove "null" elements from "toppings" array
String[] temp = new String[attempt];
for (int position = 0; position < attempt; position++) {
temp[position] = toppings[position];
}
toppings = temp;
}
Arrays.sort(toppings);
for (int position = 0; position < toppings.length; position++) {
String element = toppings[position];
System.out.println((position + 1) + ". " + element);
}
}
}
答案 1 :(得分:0)
如您所说,不允许使用ArrayList
。这是我使用String数组的方法。对您来说最有趣的部分应该是Arrays.copyOfRange
方法,您也可以用System.arraycopy(...)
调用来代替。
import java.util.*;
public class Pizza {
private static final int MAX_TOPINGS = 15;
private final String QUIT_KEYWORD = "quit";
public static void main(String[] args) {
new Pizza().printToppings(MAX_TOPINGS);
}
public void printToppings(int maxTopings){
Scanner input = new Scanner(System.in);
String[] toppings = new String[maxTopings];
int count;
for (count = 0; count < maxTopings; count++) {
System.out.printf("Enter topping topping (or type %s): ", QUIT_KEYWORD);
String topping = input.nextLine();
if (topping.toLowerCase().equals(QUIT_KEYWORD)) {
break;
}
toppings[count] = topping;
}
if (count+1 == maxTopings) {
System.out.println("No more toppings allowed.");
} else {
toppings = Arrays.copyOfRange(toppings, 0, count);
}
Arrays.sort(toppings);
for (int i = 0; i < count; i++) {
System.out.println(i+1 + ". " + toppings[i]);
}
}
}
对于以下输入:
Enter topping topping (or type quit): Cheese
Enter topping topping (or type quit): Onions
Enter topping topping (or type quit): Tuna
Enter topping topping (or type quit): quit
您将收到以下输出:
1. Cheese
2. Onions
3. Tuna
答案 2 :(得分:-2)
不要打扰while循环。如果要处理数组,则应使用for
循环。
就将字符串添加到数组而言,在尝试使用它们之前,您确实应该learn about arrays。在使用它之前,您甚至都没有初始化数组。
当用户输入“ quit”时,可以使用break
语句退出循环。
import java.util.Arrays;
import java.util.Scanner;
public class Pizza {
//If actually want the user to be able to enter 15 toppings, then set numbers to 16.
public static final int numbers = 16;
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
//Initialize the array
String[] toppings = new String[numbers];
int count;
//Use a for loop
for (count = 0; count < numbers; count++) {
System.out.println("Enter a toping (or type quit):");
toppings[count] = input.nextLine();
if (toppings[count].equalsIgnoreCase("quit")) {
//If they enter quit, break from the loop
break;
}
}
if (count == numbers)
System.out.println("No more toppings allowed.");
//If they do not fill all 15 indices of the array, trim out the empty indices.
if (count < numbers)
toppings = Arrays.copyOfRange(toppings, 0, count);
Arrays.sort(toppings);
//Use another for to print them
for (int i = 0; i < count; i++) {
System.out.println(i + ". " + toppings[i]);
}
}
}