,我的问题是:为什么我的程序在处理文本文件中的行时不重复自己。
我正在编写一个管理车库的类--车库中的汽车被放入一个不超过7的堆栈中,如果已经满了,它们会进入一个不超过5的队列。有一种方法可以手动添加cars,还有一种用于读取文本文件输入的方法。我还得到了一个stackInt类,我在创建堆栈对象时被要求使用这个类。它只有4种方法,这使得事情变得很麻烦:
public interface StackInt<E> {
E push(E obj);
E peek();
E pop();
boolean empty();
}因此,我不得不用避免没有其他有用的堆栈方法的方法来编写代码。我的大多数方法都是按预期工作的:它读取文本文件并对其进行处理,但是它不会重复进程的循环,例如,如果从车库中移走一辆汽车,从队列中插入另一辆汽车。这是我的代码:
import java.util.*;
import java.util.LinkedList;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
public class Garage{
public String line;
public String stackToString;
public int position;
public int numAdded;
LinkedList<String> queue = new LinkedList<>();
StackInt<String> stack = new LinkedStack<>();
public Garage(){
}
public Garage(String fileName){
try{
File file = new File(fileName);
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
line = scan.nextLine();
String[] data = line.split(" ");
if (data[0].equals("a"))
arrival(data[1]);
else if (data[0].equals("d"))
departure(data[1]);
}
}
catch(FileNotFoundException e) {
System.out.println("File not found");
}
}
public boolean arrival(String license){
boolean added = false;
if(numAdded < 7){
stack.push(license);
added = true;
} else if (queue.size() < 5) {
added = queue.add(license);
numAdded--;
}
if(added){
numAdded++;
}
return true;
}
public int departure(String license){
Stack<String> temp = new Stack();
while(!stack.empty()){
temp.push(stack.pop());
}
position = temp.indexOf(license);
temp.remove(license);
while(!temp.isEmpty()){
stack.push(temp.pop());
}
return position;
}
public int numberParked(){
return numAdded;
}
public int numberWaiting(){
return queue.size();
}
public String toString(){
Stack<String> tempStack = new Stack();
while (!stack.empty()){
tempStack.push(stack.pop());
stackToString = tempStack.toString().replace("[", "").replace("]", "");
}
while (!tempStack.empty()){
stack.push(tempStack.pop());
}
return "Cars in Garage: " + stackToString + "\n" + "Cars waiting: " + (queue.toString().replace("[", "").replace("]", ""));
}
}我要测试的是:
public class GarageTest
{
public static void main (String [] args)
{
Garage g1 = new Garage("parking.txt");
System.out.println("Number parked: " + g1.numberParked());
System.out.println("Number waiting: " + g1.numberWaiting());
System.out.println("Parking WEB445 ... " + g1.arrival("WEB445"));
System.out.println("Parking BEA345 ... " + g1.arrival("BEA345"));
System.out.println(g1);
System.out.println("Z23YTU departs after " + g1.departure("B12GFT") + " car(s) moved");
System.out.println(g1);
}
}产出如下:
Number parked: 7
Number waiting: 5
Parking WEB445 ... true
Parking BEA345 ... true
Cars in Garage: Y23456, X12345, B12GFT, Z23YTU
Cars waiting: W321RE, CVBNMK, DFGHJK, ERTYUI, FGHJKL
Z23YTU departs after 2 car(s) moved
Cars in Garage: Y23456, X12345, Z23YTU
Cars waiting: W321RE, CVBNMK, DFGHJK, ERTYUI, FGHJKL人们对“车库里的汽车”的预期是:
DFGHJK CVBNMK R23EWQ W321RE Y23456 X12345 B12GFT Z23YTU
with the queue on the next line. It's removing a few that have "d", but not filling the garage
afterward正在处理的文本文件(a表示到达,d表示离开):
a A123TR
a Z23YTU
a Z23YTU
a ERW345
d ERW345
a B12GFT
d a23TR
a X12345
a Y23456
a W321RE
d R23EWQ
a CVBNMK
a DFGHJK
a ERTYUI
a FGHJKL
a GHJKL9
a HJKL98发布于 2020-10-30 23:25:05
当你说“如果一辆车被从车库里移走,队列中的另一辆车应该被插入”--这个逻辑在你的离开()方法中是在哪里?该方法中没有从队列中删除汽车的代码吗?
您期望的行为在代码中丢失了。
发布于 2020-10-30 23:25:31
您的departure()不调用其体内任何位置的arrival()方法,这将将下一辆汽车添加到车库中。当departure()成功时(返回值是有效的(而不是-1),您知道汽车已经离开车库了。然后,您可以简单地使用队列中的下一个car并通过arrival()方法添加它。它将适合在车库,因为现在有空间为下一辆车。
https://stackoverflow.com/questions/64616853
复制相似问题