2014-04-06 91 views
-5

所以我創建了一個程序,創建一個船舶列表,然後創建一個菜單,您可以在其中插入船舶的詳細信息(例如名稱,長度等)。爲什麼java編譯但不運行?

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; 
} 
} 

所以...程序編譯,但是當我嘗試運行它,如我所料不工作或拋出任何異常。

+1

到目前爲止,您曾嘗試對您的程序進行故障排除/調試? – ElGavilan

+0

當你說它不運行時,你的意思是什麼? –

+0

大衛華萊士回答了我的問題,因爲它似乎是非常愚蠢的任何方式我應該設置我的布爾shmaia真實,而不是假 – user3194888

回答

0

你的主要方法有一個永遠不會執行的for循環,因此你看不到任何輸出。

3

如果shmaia爲真,則只進入主循環。但你最初設置爲false。所以主循環從不執行,程序什麼也不做。

+1

調試器會立即告訴你這一點。學習使用調試器非常重要。 –

0

更換boolean shmaia=false,flag;boolean shmaia=true,flag;

您是否shmaia是真實的,但它永遠不會。