Java在第一个for循环结束后添加字符串
我有一个列表,它根据用户输入附加,例如,如果我键入 “LMO”将把大比萨饼、马苏里拉干酪和橄榄添加到列表中。如果我输入“MHMO”,它会在列表中添加中等比萨、火腿、马苏里拉和橄榄 现在我想使用for循环,它将以如下格式打印列表Java在第一个for循环结束后添加字符串,java,list,for-loop,Java,List,For Loop,我有一个列表,它根据用户输入附加,例如,如果我键入 “LMO”将把大比萨饼、马苏里拉干酪和橄榄添加到列表中。如果我输入“MHMO”,它会在列表中添加中等比萨、火腿、马苏里拉和橄榄 现在我想使用for循环,它将以如下格式打印列表 大比萨配马苏里拉、橄榄,8.90英镑 for循环可以打印列表的内容,但是如何在第一个循环结束后添加with字符串 目前我的代码是: for(int l = 0; l < words.size(); l++) { if (words.size
大比萨配马苏里拉、橄榄,8.90英镑
for循环可以打印列表的内容,但是如何在第一个循环结束后添加with
字符串
目前我的代码是:
for(int l = 0; l < words.size(); l++) {
if (words.size() == 1) {
System.out.println(words.get(l) + " with no toppings "+ "£"+String.format("%.2f", total));
}
else {
System.out.println(words.get(0) + " with " + words.get(1) + ", " + words.get(2) + ", "+ "£" + String.format("%.2f", total));
}
}
}
for(int l=0;l
目前,假设列表中只有3项,但可能会有所不同。我的方法是什么,这样我就可以使用for循环增量+使用带字符串的
。首先,在您的示例中,循环是非常无用的
为了回答您的问题,如果您有X个浇头,并且数组的第一个元素始终是比萨饼大小,那么您可以简单地将索引1循环到表的末尾:
if(words.size()>1){
StringBuffer buf = new StringBuffer(words.get(0));
buf.append(" with ");
for(int i=1;i<words.size();i++){
buf.append(words(i)).append(", ");
}
buf.append(", £").append(String.format("%.2f", total));
}
if(words.size()>1){
StringBuffer buf=新的StringBuffer(words.get(0));
buf.附加(“随附”);
对于(inti=1;i您可以在重写toString
方法上创建pizza类
但为了简单起见,我会根据你提供给我的回答。
这不是最好的解决方案,最好使用StringBuilder,但让它保持简单。您要寻找的只是一个简单的循环;)
我将使用地图为比萨饼尺寸和配料创建一个键值对。这使您可以更灵活地从菜单中添加/删除配料。我将类似地将价格映射到比萨饼尺寸和配料,但我将在原始海报中保留这一练习
public static void main(String[] args) throws IOException
{
HashMap<Character, String> sizeMap = new HashMap<>();
sizeMap.put('S', "Small");
sizeMap.put('M', "Medium");
sizeMap.put('L', "Large");
sizeMap.put('X', "Extra Large");
HashMap<Character, String> toppingsMap = new HashMap<>();
toppingsMap.put('H', "Ham");
toppingsMap.put('P', "Pepperoni");
toppingsMap.put('M', "Mozzarella");
toppingsMap.put('O', "Olives");
String words = "LHMO";
char pizzaSize = words.charAt(0);
String pizzaToppings = words.substring(1);
StringBuilder sb = new StringBuilder();
sb.append(sizeMap.get(pizzaSize));
if (pizzaToppings.length() == 0) {
sb.append(" with no toppings ");
} else {
sb.append(" with ");
for (int i = 0; i < pizzaToppings.length(); i++) {
sb.append(toppingsMap.get(pizzaToppings.charAt(i)) + " ");
}
}
System.out.println(sb.toString());
}
publicstaticvoidmain(字符串[]args)引发IOException
{
HashMap sizeMap=新HashMap();
sizeMap.put('S','Small');
sizeMap.put('M','Medium');
sizeMap.put('L','Large');
sizeMap.put('X',“超大”);
HashMap toppingsMap=新HashMap();
toppingmap.put('H','Ham');
toppingmap.put('P','Pepperoni');
toppingsMap.put('M','Mozzarella');
toppingmap.put('O','Olives”);
String words=“LHMO”;
char pizzaSize=words.charAt(0);
字符串pizzaToppings=words.substring(1);
StringBuilder sb=新的StringBuilder();
某人追加(sizeMap.get(pizzaSize));
如果(pizzaToppings.length()==0){
某人添加(“无浇头”);
}否则{
某人附加(以“);
对于(int i=0;i
您可以为此使用StringBuilder
public static void print(ArrayList words, double total){
StringBuilder str = new StringBuilder();
if (words.size() == 1) {
System.out.println(words.get(0) + " with no toppings "+ "£"+String.format("%.2f", total));
}else if(words.size() > 1){
for(int l = 0; l < words.size(); l++) {
str.append(words.get(l));
if(l==0){
str.append(" with ");
}else{
str.append(", ");
}
}
str.append("£");
str.append(String.format("%.2f", total));
}
System.out.println(str.toString());
}
公共静态无效打印(ArrayList字,双倍总数){
StringBuilder str=新的StringBuilder();
if(words.size()==1){
System.out.println(words.get(0)+“不带首字母”+“£”+String.format(“%.2f”,总计));
}else if(words.size()>1){
for(int l=0;l
您应该看一看,虽然这很简单,但唯一修改的部分(我的主代码中缺少)是;如果用户只输入“M”或“L”(中号或大号),则应打印“无配料的中号比萨饼”;该部分已被编辑掉。编辑:无需担心,只需添加,否则将声明是的。我没有对尺寸进行M/L。您发布的不是原始代码。我希望它能帮助您解决问题;)
public static void main(String[] args) throws IOException
{
HashMap<Character, String> sizeMap = new HashMap<>();
sizeMap.put('S', "Small");
sizeMap.put('M', "Medium");
sizeMap.put('L', "Large");
sizeMap.put('X', "Extra Large");
HashMap<Character, String> toppingsMap = new HashMap<>();
toppingsMap.put('H', "Ham");
toppingsMap.put('P', "Pepperoni");
toppingsMap.put('M', "Mozzarella");
toppingsMap.put('O', "Olives");
String words = "LHMO";
char pizzaSize = words.charAt(0);
String pizzaToppings = words.substring(1);
StringBuilder sb = new StringBuilder();
sb.append(sizeMap.get(pizzaSize));
if (pizzaToppings.length() == 0) {
sb.append(" with no toppings ");
} else {
sb.append(" with ");
for (int i = 0; i < pizzaToppings.length(); i++) {
sb.append(toppingsMap.get(pizzaToppings.charAt(i)) + " ");
}
}
System.out.println(sb.toString());
}
public static void print(ArrayList words, double total){
StringBuilder str = new StringBuilder();
if (words.size() == 1) {
System.out.println(words.get(0) + " with no toppings "+ "£"+String.format("%.2f", total));
}else if(words.size() > 1){
for(int l = 0; l < words.size(); l++) {
str.append(words.get(l));
if(l==0){
str.append(" with ");
}else{
str.append(", ");
}
}
str.append("£");
str.append(String.format("%.2f", total));
}
System.out.println(str.toString());
}