Do While Loop不想为app工作

时间:2014-06-20 04:26:16

标签: java arrays methods do-while

Assignment是沙龙的6个对象数组,需要具有菜单循环,因此用户可以使用所有排序,直到他们选择“0”退出app.Errors在代码下方发布。 我知道我尝试了另一种方法而没有使用3种方法并且可以使用它但我需要能够使用方法和做任何人有任何建议或输入,所以我可以使这项工作。感谢

import java.util.*;

public class SalonReport
{
   public static void main(String[]args)
   {
      int x =0;
  Service s1 = new Service();
  Service s2 = new Service();
  Service s3 = new Service();
  Service s4 = new Service();
  Service s5 = new Service();
  Service s6 = new Service();


  s1.setService("Cut");
  s1.setPrice(5.00);
  s1.setTime(15);

  s2.setService("Shampoo");
  s2.setPrice(5.00);
  s2.setTime(10);

  s3.setService("Manicure");
  s3.setPrice(20.00);
  s3.setTime(30);

  s4.setService("Style");
  s4.setPrice(60.00);
  s4.setTime(55);

  s5.setService("Permanent");
  s5.setPrice(28.00);
  s5.setTime(35);

  s6.setService("Trim");
  s6.setPrice(8.00);
  s6.setTime(5);


  Service[] services = {s1, s2, s3, s4, s5,s6};


 //Menu
  System.out.println("Choose your sort");
  System.out.println("1: Service");
  System.out.println("2: Price");
  System.out.println("3: Time");
  System.out.println("0: Exit");


   Scanner input = new Scanner(System.in);

do{  
  x = Integer.parseInt(input.next());  



  switch(x)
  {
     case 1:
        sortByService(services);

        break;
     case 2:
        sortByPrice(services);
        break;
     case 3:
        sortByTime(services);
        break;
     case 0:
         break;
     default:
        System.out.println("Invalid Entry");
  }while(x!=0); 











          }

           }

      public static void sortByTime(Service[] array)
       {
          Service temp;
          int highSubscript = array.length - 1;
          for(int a = 0; a < highSubscript; ++a)
          {
              for(int b = 0; b < highSubscript; ++b)
              {
                if(array[b].getTime() > array[b+1].getTime())
                   {
                      temp = array[b];
                      array[b] = array[b + 1];
                      array[b + 1] = temp;
                   }
             }
          }

          for(int i = 0; i < array.length; ++i)
          {

             System.out.println("Service: "+array[i].getService()+", "+"Price: "            +array[i].getPrice()+", "+"Time: "+ array[i].getTime()); 
      }

   }

   public static void sortByService(Service[] array)
   {
      Service temp;
      int highSubscript = array.length - 1;
      for(int a = 0; a < highSubscript; ++a)
      {
         for(int b = 0; b < highSubscript; ++b)
         {
            if((array[b].getService().compareToIgnoreCase(array[b+1].getService())) >=     0)
               {
                  temp = array[b];
                  array[b] = array[b + 1];
                  array[b + 1] = temp;
               }
         }
      }

      for(int i = 0; i < array.length; ++i)
      {

         System.out.println("Service: "+array[i].getService()+", "+"Price: "     +array[i].getPrice()+", "+"Time: "+ array[i].getTime());        
      }

   }

   public static void sortByPrice(Service[] array)
   {
    Service temp;
      int highSubscript = array.length - 1;
      for(int a = 0; a < highSubscript; ++a)
      {
         for(int b = 0; b < highSubscript; ++b)
         {
            if(array[b].getPrice() > array[b+1].getPrice())
               {
                  temp = array[b];
                  array[b] = array[b + 1];
                  array[b + 1] = temp;
               }
         }
      }

      for(int i = 0; i < array.length; ++i)
      {
         System.out.println("Service: "+array[i].getService()+", "+"Price:  "+array[i].getPrice()+", "+"Time: "+ array[i].getTime());        
      }

   }


}


SalonReport.java:87: error: while expected
      }
       ^
SalonReport.java:91: error: illegal start of expression
  public static void sortByTime(Service[] array)
  ^
SalonReport.java:91: error: ')' expected
  public static void sortByTime(Service[] array)
        ^
SalonReport.java:91: error: ';' expected
  public static void sortByTime(Service[] array)
           ^
SalonReport.java:91: error: '.class' expected
  public static void sortByTime(Service[] array)
                                      ^
SalonReport.java:91: error: ';' expected
  public static void sortByTime(Service[] array)
                                           ^
SalonReport.java:116: error: illegal start of expression
   public static void sortByService(Service[] array)
   ^
SalonReport.java:116: error: illegal start of expression
   public static void sortByService(Service[] array)
          ^
SalonReport.java:116: error: ';' expected
   public static void sortByService(Service[] array)
            ^
SalonReport.java:116: error: '.class' expected
   public static void sortByService(Service[] array)
                                          ^
SalonReport.java:116: error: ';' expected
   public static void sortByService(Service[] array)
                                               ^
SalonReport.java:141: error: illegal start of expression
   public static void sortByPrice(Service[] array)
  ^
SalonReport.java:141: error: illegal start of expression
   public static void sortByPrice(Service[] array)
          ^
SalonReport.java:141: error: ';' expected
   public static void sortByPrice(Service[] array)
            ^
SalonReport.java:141: error: '.class' expected
   public static void sortByPrice(Service[] array)
                                        ^
SalonReport.java:141: error: ';' expected
   public static void sortByPrice(Service[] array)
                                             ^
SalonReport.java:166: error: reached end of file while parsing
}
 ^
17 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

3 个答案:

答案 0 :(得分:2)

您错过了}

应该是

do{  
  x = Integer.parseInt(input.next());  
  switch(x)
  {
     case 1:
        sortByService(services);

        break;
     case 2:
        sortByPrice(services);
        break;
     case 3:
        sortByTime(services);
        break;
     case 0:
         break;
     default:
        System.out.println("Invalid Entry");
  }
}while(x!=0); 

答案 1 :(得分:1)

您在切换案例时缺少}

答案 2 :(得分:0)

应该是:

做{
      x = Integer.parseInt(input.next());

  switch(x)
  {
     case 1:
        sortByService(services);

        break;
     case 2:
        sortByPrice(services);
        break;
     case 3:
        sortByTime(services);
        break;
     case 0:
         break;
     default:
        System.out.println("Invalid Entry");
  }











          }while(x!=0);