所以我创建了一个程序来创建船只列表,然后创建一个菜单,您可以在其中插入船只的详细信息(例如名称,长度等)。
class testVessel
{
public static void main(String args[])
{
String name,idioktName,skafos,temp1;
double length,width,ektopisma,maxspeed,max,ektop;
int etos,epilogh,size=0,thesi,i=0,o,a,year;
boolean shmaia=false,flag;
Vessel[] skafoi=new Vessel[10];
for(;shmaia;)
{
System.out.println("lista epilogwn");
System.out.println("1.eisagwgh stoixeiwn skafous");
System.out.println("2.emfanish stoixeiwn skafous");
System.out.println("3.emfanish oloklhrou tou pinaka");
System.out.println("4.telos");
System.out.println("dwste epilogh(1-4):");
epilogh=UserInput.getInteger();
if(epilogh==4)shmaia=true;
switch(epilogh)
{
case 1 :
System.out.println("dwste onoma skafous:");
name=UserInput.getString();
System.out.println("dwste mhkos:");
flag=true;
length=0;
for(;flag;)
{
length=UserInput.getDouble();
if(length>=0 && length<=1000)flag=false;
else
System.out.println("mh apodekta oria xanadokimaste");
}
System.out.println("dwste platos:");
width=UserInput.getDouble();
System.out.println("dwste ektopisma:");
flag=true;
ektopisma=0;
for(;flag;)
{
ektopisma=UserInput.getDouble();
if(ektopisma>=0 && ektopisma<=150000)flag=false;
else
System.out.println("mh apodekta oria xanadokimaste");
}
System.out.println("dwste etos kataskeuhs:");
etos=UserInput.getInteger();
System.out.println("dwste megisth taxhtyta:");
maxspeed=UserInput.getDouble();
System.out.println("dwste onoma idioktith:");
idioktName=UserInput.getString();
skafoi[i]=new Vessel(name,length,width,ektopisma,etos,maxspeed,idioktName);
i++;
size++;
break;
case 2 :
if(size!=0)
System.out.println("epilogh pediou anazhthshs");
System.out.println("1.onoma skafous");
System.out.println("2.etos kataskeuhs");
System.out.println("3.ektopisma");
System.out.println("4.megisto mhkos skafous");
System.out.println("5.epistrodh sthn arxikh lista epilogwn");
System.out.println("dwste epilogh(1-5):");
epilogh=UserInput.getInteger();
switch(epilogh)
{
case 1:
MyUtils.epiloghMethodou();
epilogh=UserInput.getInteger();
switch(epilogh)
{
case 1:
System.out.println("dwste onoma skafous");
skafos=UserInput.getString();
thesi=MyUtils.seqSearch(skafoi,skafos);
System.out.println(skafoi[thesi].EmfanishOlwn());
break;
case 2:
System.out.println("dwste onoma skafous");
skafos=UserInput.getString();
thesi=MyUtils.binSearch(skafoi,skafos,size);
System.out.println(skafoi[thesi].EmfanishOlwn());
break;
case 3:
break;
default: System.out.print("\n\tLathos epilogi methodou ... Eisagete ari8mo 1-3\n");
}break;
case 2:
MyUtils.epiloghMethodou();
epilogh=UserInput.getInteger();
switch(epilogh)
{
case 1:
System.out.println("dwste etos kataskeuhs");
year=UserInput.getInteger();
MyUtils.seqSearch(skafoi,year,size);
break;
case 2:
System.out.println("dwste etos kataskeuhs");
year=UserInput.getInteger();
MyUtils.binSearch(skafoi,year,size);
break;
case 3:
break;
default: System.out.print("\n\tLathos epilogi methodou ... Eisagete ari8mo 1-3\n");
}break;
case 3:
MyUtils.epiloghMethodou();
epilogh=UserInput.getInteger();
switch(epilogh)
{
case 1:
System.out.println("dwste ektopisma");
ektop=UserInput.getDouble();
MyUtils.seqSearch(skafoi,ektop,size);
break;
case 2:
System.out.println("dwste ektopisma");
ektop=UserInput.getDouble();
MyUtils.binSearch(skafoi,ektop,size);
break;
case 3:
break;
default: System.out.print("\n\tLathos epilogi methodou ... Eisagete ari8mo 1-3\n");
}break;
case 4:
MyUtils.epiloghMethodou();
epilogh=UserInput.getInteger();
max=0;
a=0;
switch(epilogh)
{
case 1:
MyUtils.seqSearch(skafoi,max,size,a);
System.out.println("h max taxhthta einai:"+max);
break;
case 2:
MyUtils.binSearch(skafoi,max,size);
System.out.println("h max taxhthta einai:"+max);
break;
case 3:
break;
default: System.out.print("\n\tLathos epilogi methodou ... Eisagete ari8mo 1-3\n");
}
case 5:
break;
default: System.out.print("\n\tLathos epilogi methodou ... Eisagete ari8mo 1-5\n");
}
if(size==0)System.out.println("den uparxoun skafoi sth lista");
break;
case 3:
if(size!=0)
{for(i=0;i<=size;i++)
{
System.out.println(skafoi[i].EmfanishOlwn());
}
}
if(size==0)
{System.out.println("den uparxoun skafoi sth lista");}
break;
case 4:
System.out.println("telos programmatos");
break;
default: System.out.print("\n\tLathos epilogi methodou ... Eisagete ari8mo 1-4\n");
}
}
}
}
class MyUtils
{
public static void seqSearch(Vessel[] a,int key,int size)
{
int x=0;
for(int i=0;i<=size;i++)
if(a[i].getetos()==key)
{System.out.println(a[i].EmfanishOlwn());
x++;}
if(x==0)System.out.println("den uparxoun skafoi autou tou etous");
}
public static int seqSearch(Vessel[] a,String key)
{
for(int i=0;i<a.length;i++)
if(a[i].getname()==key)
return i;
return -1;
}
public static void seqSearch(Vessel[] a,double key,int size)
{
int x=0;
for(int i=0;i<=size;i++)
if(a[i].getektopisma()==key)
{System.out.println(a[i].EmfanishOlwn());
x++;}
if(x==0)System.out.println("den uparxoun skafoi me tetoio ektopisma");
}
public static void seqSearch(Vessel[] a,double max,int size,int l)
{
l=0;
max=0;
for(int i=0;i<=size;i++)
if(a[i].getmaxspeed()>max)max=a[i].getmaxspeed();
}
public static int binSearch(Vessel[] a,String key,int size)
{
int left=0,right=size-1;
return binarySearch(a,key,left,right);
}
private static int binarySearch(Vessel[] numbers,String key,int left,int right)
{
int mid,pos=-1;
while(pos==-1 && left<=right)
{
mid=(left+right)/2;
if(numbers[mid].getname().compareTo(key)>0)
right=mid-1;
else
if(numbers[mid].getname().compareTo(key)<0)
left=mid+1;
else
pos=mid;
}
return pos;
}
public static void binSearch(Vessel[] a,int key,int size)
{
int left=0,right=size-1;
for(int i=0;i<=size;i++)
if(a[i].getetos()==a[binarySearch(a,key,left,right)].getetos())System.out.println(a[i].EmfanishOlwn());
}
private static int binarySearch(Vessel[] numbers,int key,int left,int right)
{
int mid,pos=-1;
while(pos==-1 && left<=right)
{
mid=(left+right)/2;
if(key<numbers[mid].getetos())
right=mid-1;
else
if(key>numbers[mid].getetos())
left=mid+1;
else
pos=mid;
}
return pos;
}
public static void binSearch(Vessel[] a,double key,int size)
{
int left=0,right=size-1;
for(int i=0;i<=size;i++)
if(a[i].getektopisma()==a[binarySearch(a,key,left,right)].getektopisma())System.out.println(a[i].EmfanishOlwn());
}
private static int binarySearch(Vessel[] numbers,double key,int left,int right)
{
int mid,pos=-1;
while(pos==-1 && left<=right)
{
mid=(left+right)/2;
if(key<numbers[mid].getektopisma())
right=mid-1;
else
if(key>numbers[mid].getektopisma())
left=mid+1;
else
pos=mid;
}
return pos;
}
public static void epiloghMethodou()
{
System.out.println("epilogh methodou anazhthshs");
System.out.println("1.seiriakh anazhthsh");
System.out.println("2.duadiki anazhthsh");
System.out.println("3.epistrofh sthn epilogh pediou anazhthshs");
System.out.println("dwste epilogh(1-3):");
}
}
class Vessel
{
private String name;
private double length;
private double width;
private double ektopisma;
private int etos;
private double maxspeed;
private String idiokName;
Vessel(String n,double l,double w,double e,int et,double ms,String cn)
{
name=n;
length=l;
width=w;
ektopisma=e;
etos=et;
maxspeed=ms;
idiokName=cn;
}
public String getname()
{
return name;
}
public double getlength()
{
return length;
}
public double getwidth()
{
return width;
}
public double getektopisma()
{
return ektopisma;
}
public int getetos()
{
return etos;
}
public double getmaxspeed()
{
return maxspeed;
}
public String getidiokName()
{
return idiokName;
}
public String EmfanishOlwn()
{
String toString=("to onoma einai:"+name);
toString+=("to mhkos einai:"+length);
toString+=("to platos einai:"+width);
toString+=("to ektopisma einai:"+ektopisma);
toString+=("to etos einai:"+etos);
toString+=("h megisth taxhtyta einai:"+maxspeed);
toString+=("to onoma tou idiokthth einai:"+idiokName);
return toString;
}
}
所以...程序编译,但是当我尝试运行它时,它不能像我预期的那样工作或抛出任何异常。
答案 0 :(得分:3)
如果shmaia
为真,您只进入主循环。但是你最初把它设置为假。所以主循环永远不会执行,程序什么也不做。
答案 1 :(得分:0)
你的main方法有一个永远不会被执行的for循环,因此,你看不到任何输出。
答案 2 :(得分:0)
将boolean shmaia=false,flag;
替换为boolean shmaia=true,flag;
你检查shmaia是否属实,但它永远不会。