gpt4 book ai didi

java - Do While 循环不想为应用程序工作

转载 作者:行者123 更新时间:2023-12-01 22:54:18 24 4
gpt4 key购买 nike

Assignment 是一个沙龙的 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);

关于java - Do While 循环不想为应用程序工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com