[英]Do While Loop doesn't want to work for app
分配是沙龙的6对象数组,需要具有菜单循环,以便用户可以使用所有排序方式,直到他们选择``0''退出应用为止。错误发布在代码下方。 我知道我尝试了另一种方法而不使用这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.
您错过了一个}
它应该是
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);
您缺少}
的开关情况。
应该:
做{
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.