我的程序在第一个元素进入数组后停止。你可以帮帮我吗?这是代码: 对不起 我现在做了一些更改ArrayQueue是我的所有方法实现的类 QueueElement是另一个保存元素类型的类。 我认为现在的问题是在ArrayQueue类
import java.io.*;
import java.util.Scanner;
public class QueueTest
{ private static Scanner scan =new Scanner(System.in);
public static void main(String[] args)
{
ArrayQueue queue = new ArrayQueue();
System.out.println("insert :" +
" P: to print the array "+
"S: to sort "+
"E: to empty");
do
{
if (userInput.startsWith("Y")) {
System.out.println("insert the value of element");
int vl = scan.nextInt();
System.out.println("insert the description");
String p = scan.next();
System.out.println("insert true or false");
boolean vlf = scan.nextBoolean();
queue.shtoElement(new QueueElement(vl, p, vlf));
} else if (userInput.startsWith("P")) {
queue.shfaqArray();//display the array
} else if (userInput.startsWith("S")) {
queue.rendit();//sort the array
} else if (userInput.startsWith("E")) {
queue.Empty();//empty array
}
System.out.println("Insert:" + " P: to display the array "
+ " S: to sort array "
+ " E: to empty");
userInput = scan.next();
} while (!userInput.equalsIgnoreCase("exit"));
}
}
这是类ArrayQueue
public class ArrayQueue implements Queue
{
//build the queue
QueueElement[] tabela= new QueueElement[MADHESIA];
QueueElement element= new QueueElement();
//test if queue is empty
public boolean isEmpty()
{
boolean empty = true;
for(int k =0;k<tabela.length;k++)
{
if(tabela[k] !=null)
empty =false;
break;
}
return empty;
}
public boolean isFull()
{
boolean full=false;
for(int k=0;k<tabela.length;k++)
{
if(tabela[k]!=null)
full=true;
break;
}
return full;
}
//sort the array
public void rendit()
{
QueueElement temp=tabela[0];
for(int i=0;i<tabela.length;i++)
{
for(int j=i+1;j<tabela.length;j++)
if(tabela[j]!=null)
{
if(tabela[j].compareTo(tabela[i])==1)
{
tabela[j]=temp;
tabela[i]=tabela[j];
temp=tabela[i];
}
else
{
nrElementeve++;
}
}
if(tabela[i].getVlefshmeria()==false)
{
hiqElement(i,temp);
}
}
}
// add element into the array
public void shtoElement(QueueElement el)
{
if(isEmpty())
{
tabela[0]=el;
}
else
{
for(int i=0;i<tabela.length;i++)
{
if(tabela[i]!= null && (el.compareTo(tabela[i])==0))
{
System.out.println("element can't be added into array cause it exists !");
}
{
if(isFull())
{
int index=tabela.length;
tabela=rritMadhesine(tabela);
tabela[index]=el;
}
else
{
for(int j=0;j<tabela.length;j++)
{
if(tabela[j]==null)
tabela[j]=el;
break;
}
}
}
}
}
}
//find max of array
public QueueElement gjejMax()
{
QueueElement max = tabela[0];
for (QueueElement element :tabela)
{
if(element.getValue() > max.getValue())
{
max =element;
}
return max;
}
return max;
}
//find min of array
public QueueElement gjejMin()
{
QueueElement min= tabela[0];
for(QueueElement element : tabela)
{
if(element.getValue()< min.getValue())
{
min=element;
}
return min;
}
return min;
}
//remove element from array
public void hiqElement(int indeksi, QueueElement temp)
{
if(tabela.length > 0)
{
if(gjejMax().compareTo(temp)==0 || gjejMin().compareTo(temp)==0)
{
System.out.println("element can't be removed!");
}
else
{
for(int i=indeksi;i<tabela.length;i++)
{
tabela[i]=tabela[i+1];
}
}
nrElementeve--;
}
}
//empty array
public void Empty()
{
tabela= new QueueElement[MADHESIA];
}
//display the array
public void shfaqArray()
{
for(int i=0;i< tabela.length;i++)
{
if(tabela[i]!=null)
{
System.out.println(tabela[i]);
}
else
break;
}
}
//increase the length of array
public static QueueElement [] rritMadhesine(QueueElement[] array){
QueueElement[] tab=new QueueElement[array.length+2];
return tab;
}
private int nrElementeve;
private static final int MADHESIA = 10;
}
答案 0 :(得分:2)
不知道ArrayQueue
和QueueElement
是什么,但是......
看起来您正在阅读用户输入,然后通过决策树来决定基于该输入做什么。但是在每一点上,您都在重新阅读用户输入:
if(scan.next().startsWith("Y"))
{
System.out.println("Jepni vleren e elem");
vl=scan.nextInt();
System.out.println("Jepni pershkrimin e elem");
p=scan.next();
System.out.println("Jepni vlefshmerine e elem");
vlf=scan.nextBoolean();
queue.shtoElement(new QueueElement(vl,p,vlf));
}
else
{
if(scan.next().startsWith("P"))
queue.shfaqArray();
}
if(scan.next().startsWith("E"))
queue.Empty();
if(scan.next().startsWith("S"))
queue.rendit();
else
{
break;
}
我想你想要更像的东西:
String userInput = scan.next();
if(userInput.startsWith("Y"))
{
System.out.println("Jepni vleren e elem");
vl=scan.nextInt();
System.out.println("Jepni pershkrimin e elem");
p=scan.next();
System.out.println("Jepni vlefshmerine e elem");
vlf=scan.nextBoolean();
queue.shtoElement(new QueueElement(vl,p,vlf));
}
else if(userInput.startsWith("P"))
queue.shfaqArray();
else if(userInput.startsWith("E"))
queue.Empty();
else if(userInput.startsWith("S"))
queue.rendit();
else
{
break;
}
您在哪里阅读用户输入一次(String userInput = scan.next();
),然后根据userInput
变量决定做什么,而不是每次都重新扫描。此外,您可能会在中间的某处找到一些else
语句。