JAVA,关于用LINKED LIST时碰到的问题!!!请高手帮下忙啊!!!很急!!!
importjava.util.*;publicclassFactory{privateStockstock;//privatePizza[]pizzas=newPizz...
import java.util.*;
public class Factory
{ private Stock stock;
//private Pizza[] pizzas = new Pizza[3];
//private int index = 0;
private Pizza made;
List<Pizza> pizzas = new LinkedList<Pizza>();
public Factory(Stock stock)
{ this.stock = stock;
LinkedList<String> pepTops = new LinkedList<String>();
pepTops.add(new String("pepperoni"));
pepTops.add(new String("mushroom"));
pepTops.add(new String("olive"));
add("pepperoni", pepTops, 4.50);
LinkedList<String> mariTops = new LinkedList<String>();
mariTops.add(new String("prawn"));
mariTops.add(new String("olive"));
add("marinara", mariTops, 6.70);
LinkedList<String> vegeTops = new LinkedList<String>();
vegeTops.add(new String("zucchini"));
vegeTops.add(new String("artichoke"));
add("vegetarian", vegeTops, 8.90); }
private void add(String name, LinkedList<String> toppings, double price)
{ pizzas.add( new Pizza(name, toppings, price));
for (String topping: toppings)
if (!stock.has(topping))
stock.add(topping); }
public void newPizza()
{
String newPizzaName = readPizzaName();
LinkedList<String> newTops = new LinkedList<String>();
do
{
newTops.add(new String(readNewTop()));
}while (!end(readNewTop()));
double price = readPrice();
add(newPizzaName, newTops, price);
}
private String readPizzaName()
{
System.out.print(" Pizza name: ");
return In.nextLine();
}
private double readPrice()
{
System.out.print(" Price: ");
return In.nextDouble();
}
private String readNewTop()
{
System.out.print(" Topping (or end): ");
return In.nextLine();
}
public boolean end(String s)
{ return s.equals("end"); }
=========================================
问题出在newPizza(),只有第一次输入的东西才能被加进add(newPizzaName, newTops, price);
比如说,pizza name: gold
Topping (or end): gold
Topping (or end): copper
Topping (or end): iron
只有gold被加进add(newPizzaName, newTops, price);
这是为什么呢?代码要怎么改呢?
求解啊~ 展开
public class Factory
{ private Stock stock;
//private Pizza[] pizzas = new Pizza[3];
//private int index = 0;
private Pizza made;
List<Pizza> pizzas = new LinkedList<Pizza>();
public Factory(Stock stock)
{ this.stock = stock;
LinkedList<String> pepTops = new LinkedList<String>();
pepTops.add(new String("pepperoni"));
pepTops.add(new String("mushroom"));
pepTops.add(new String("olive"));
add("pepperoni", pepTops, 4.50);
LinkedList<String> mariTops = new LinkedList<String>();
mariTops.add(new String("prawn"));
mariTops.add(new String("olive"));
add("marinara", mariTops, 6.70);
LinkedList<String> vegeTops = new LinkedList<String>();
vegeTops.add(new String("zucchini"));
vegeTops.add(new String("artichoke"));
add("vegetarian", vegeTops, 8.90); }
private void add(String name, LinkedList<String> toppings, double price)
{ pizzas.add( new Pizza(name, toppings, price));
for (String topping: toppings)
if (!stock.has(topping))
stock.add(topping); }
public void newPizza()
{
String newPizzaName = readPizzaName();
LinkedList<String> newTops = new LinkedList<String>();
do
{
newTops.add(new String(readNewTop()));
}while (!end(readNewTop()));
double price = readPrice();
add(newPizzaName, newTops, price);
}
private String readPizzaName()
{
System.out.print(" Pizza name: ");
return In.nextLine();
}
private double readPrice()
{
System.out.print(" Price: ");
return In.nextDouble();
}
private String readNewTop()
{
System.out.print(" Topping (or end): ");
return In.nextLine();
}
public boolean end(String s)
{ return s.equals("end"); }
=========================================
问题出在newPizza(),只有第一次输入的东西才能被加进add(newPizzaName, newTops, price);
比如说,pizza name: gold
Topping (or end): gold
Topping (or end): copper
Topping (or end): iron
只有gold被加进add(newPizzaName, newTops, price);
这是为什么呢?代码要怎么改呢?
求解啊~ 展开
展开全部
package cn.com.xxx; //根据实际情况调整
import java.io.*;
public class ReadWriteFile {
public static void printFile(String name) throws Exception {
FileReader fin = new FileReader(name);
int temp;
while ((temp = fin.read()) != -1)
System.out.print((char) temp);
System.out.println();
fin.close();
}
public static void mergeFile(String one, String two, String result)
throws Exception {
FileReader fin1 = new FileReader(one);
FileReader fin2 = new FileReader(two);
PrintWriter out = new PrintWriter(result);
int temp1, temp2;
while ((temp1 = fin1.read()) != -1)
out.print((char) temp1);
out.println();
while ((temp2 = fin2.read()) != -1)
out.append((char) temp2);
out.close();
fin1.close();
fin2.close();
}
public static void main(String[] args) {
try {
printFile("D:\\Test\\1.txt"); //文件路径根据实际情况调整下同
printFile("D:\\Test\\2.txt");
mergeFile("D:\\Test\\1.txt", "D:\\Test\\2.txt", "D:\\Test\\3.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
import java.io.*;
public class ReadWriteFile {
public static void printFile(String name) throws Exception {
FileReader fin = new FileReader(name);
int temp;
while ((temp = fin.read()) != -1)
System.out.print((char) temp);
System.out.println();
fin.close();
}
public static void mergeFile(String one, String two, String result)
throws Exception {
FileReader fin1 = new FileReader(one);
FileReader fin2 = new FileReader(two);
PrintWriter out = new PrintWriter(result);
int temp1, temp2;
while ((temp1 = fin1.read()) != -1)
out.print((char) temp1);
out.println();
while ((temp2 = fin2.read()) != -1)
out.append((char) temp2);
out.close();
fin1.close();
fin2.close();
}
public static void main(String[] args) {
try {
printFile("D:\\Test\\1.txt"); //文件路径根据实际情况调整下同
printFile("D:\\Test\\2.txt");
mergeFile("D:\\Test\\1.txt", "D:\\Test\\2.txt", "D:\\Test\\3.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询