![](/img/trans.png)
[英]how to fix “ illegal start of expression” error while declare method in java
[英]How to fix 'illegal start of expression' error for the second method?
我正在尝试使用嵌套的 if/else 结构和 while 循环来完成分配。 但是,不知何故,我的第二种方法会导致错误。 如果你能帮助我,请告诉我,因为这是我关于 SO 的第一个问题。
我试过用括号关闭第一种方法,但这只会导致更多错误。 我不知道从这里到 go 的其他地方,我已经在谷歌上寻找帮助。
import java.util.*;
public class PA1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while((senitel == 'y')||(senitel == 'Y')); {
menu();
System.out.printf("Enter your choice: ");
choice = in.nextInt();
redo = in.nextLine();
while(choose ==true) {
if(choice==1) {
choice = false;
computer = "HP Envy 13\n";
System.out.printf("Enter the quantity for the %s", computer);
quantity = in.nextInt();
subtotal = subtotal +799.99; }
else if (choice==2) {
choose = false;
computer = "Asus ZenBook 13 UX333FA\n";
System.out.printf("Enter qunatity for the %s", computer);
quantity = in.nextInt();
subtotal = subtotal + 849.99; }
else {
choose = false;
System.out.println("In Else");
System.out.printf("Invalid choice! Try again.\n");
System.out.printf("Enter 'Y' to add a laptop to your purchase or 'N' to exit: ");
redo = in.nextLine(); }
if((choice>=1)&&(choice<=5)) {
choice = true;}
else {
choice = false;
menu();
choice = in.nextInt();}}
redo = in.nextLine();
System.out.printf("Enter 'Y' to add a laptop to your purchase or 'N' to exit: ");
reply = in.nextLine();
senitel = reply.charAt(0);}
System.out.println("Out");
choice = 3;
if(choice>0) {
if(choice<6) {
System.out.printf("LAPTOP ORDER SUMMARY\n\n");
System.out.printf("%s",orderSummary);
System.out.printf("Date:\n");
System.out.printf("Time:\n\n");
tax = subtotal + .0825;
total = subtotal + tax;
int trigger =1;
String laptop = "";
int lineItem = 0;
if(trigger == 1) {
orderSummary += String.format("%n%,-9d %-30s %8s $%,17.2f",
qty, laptop, " ", lineItem);
trigger = 0; }
else {
orderSummary += String.format("%n%,-9d %-30s %9s %,17.2f",
qty, laptop, " ", lineItem);}
orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
+ "%n%31s Tax @ 8.25%% %6s %,17.2f"
+ "%n%n%37s TOTAL %5s $%,17.2f%n",
" ", " ", subtotal,
" ", " ", tax,
" ", " ", total);
System.out.printf("%s", orderSummary); }}
public static void menu(){
Calendar dateTime = Calendar.getInstance();
System.out.printf("%nTOP LAPTOPS OF %tY"
+ "%n%n1. %-23s %7s $%,9.2f"
+ "%n2. %-23s %8s %,9.2f"
+ "%n3. %-23s %8s %,9.2f"
+ "%n4. %-23s %8s %,9.2f"
+ "%n5. %-23s %8s %,9.2f"
+ "%n%nEnter your choice: ",
dateTime,
"HP Envy 13", " ", 799.99,
"Asus ZenBook 13 UX333FA", " ", 849.99,
"Dell XPS 13", " ", 989.99,
"Alienware Area 51-m", " ", 1999.99,
"Razer Blade Stealth", " ", 1299.00); }
public static void trig() {
int trigger = 1;
String orderSummary = "";
int quantity = 0;
String laptop = "";
int lineItem = 0;
if(trigger==1) {
orderSummary += String.format("%n%, -9d %-30s %8%s $%, 17.2f",
quantity, laptop, " ", lineItem);
trigger =0; }
else {
orderSummary += String.format("%n%, -9d %-30s %9s %, 17.2f",
quantity, laptop, lineItem); }
orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
+ "%n%31s Tax @ 8.25%% %6s %,17.2f"
+ "%n%n%37s TOTAL %5s $%,17.2f%n");
System.out.printf("%s", orderSummary); }}
那么这里有一个问题:
while( (senitel == 'y') || (senitel == 'Y') );
{
看到最后那个分号了吗? 可能不是你想要的。
使用 IDE 自动缩进您的代码。 然后 go 通过所有这些并修复语法错误。 严重地。
你忘记了大括号。
这是您的固定示例:
import java.util.*;
public class PA1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while ((senitel == 'y') || (senitel == 'Y')) ;
{
menu();
System.out.printf("Enter your choice: ");
choice = in.nextInt();
redo = in.nextLine();
while (choose == true) {
if (choice == 1) {
choice = false;
computer = "HP Envy 13\n";
System.out.printf("Enter the quantity for the %s", computer);
quantity = in.nextInt();
subtotal = subtotal + 799.99;
}
else if (choice == 2) {
choose = false;
computer = "Asus ZenBook 13 UX333FA\n";
System.out.printf("Enter qunatity for the %s", computer);
quantity = in.nextInt();
subtotal = subtotal + 849.99;
}
else {
choose = false;
System.out.println("In Else");
System.out.printf("Invalid choice! Try again.\n");
System.out
.printf("Enter 'Y' to add a laptop to your purchase or 'N' to exit: ");
redo = in.nextLine();
}
if ((choice >= 1) && (choice <= 5)) {
choice = true;
}
else {
choice = false;
menu();
choice = in.nextInt();
}
}
redo = in.nextLine();
System.out.printf("Enter 'Y' to add a laptop to your purchase or 'N' to exit: ");
reply = in.nextLine();
senitel = reply.charAt(0);
}
System.out.println("Out");
choice = 3;
if (choice > 0) {
if (choice < 6) {
System.out.printf("LAPTOP ORDER SUMMARY\n\n");
System.out.printf("%s", orderSummary);
System.out.printf("Date:\n");
System.out.printf("Time:\n\n");
tax = subtotal + .0825;
total = subtotal + tax;
int trigger = 1;
String laptop = "";
int lineItem = 0;
if (trigger == 1) {
orderSummary += String.format("%n%,-9d %-30s %8s $%,17.2f",
qty, laptop, " ", lineItem);
trigger = 0;
}
else {
orderSummary += String.format("%n%,-9d %-30s %9s %,17.2f",
qty, laptop, " ", lineItem);
}
orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
+ "%n%31s Tax @ 8.25%% %6s %,17.2f"
+ "%n%n%37s TOTAL %5s $%,17.2f%n",
" ", " ", subtotal,
" ", " ", tax,
" ", " ", total);
System.out.printf("%s", orderSummary);
}
}
}
public static void menu() {
Calendar dateTime = Calendar.getInstance();
System.out.printf("%nTOP LAPTOPS OF %tY"
+ "%n%n1. %-23s %7s $%,9.2f"
+ "%n2. %-23s %8s %,9.2f"
+ "%n3. %-23s %8s %,9.2f"
+ "%n4. %-23s %8s %,9.2f"
+ "%n5. %-23s %8s %,9.2f"
+ "%n%nEnter your choice: ",
dateTime,
"HP Envy 13", " ", 799.99,
"Asus ZenBook 13 UX333FA", " ", 849.99,
"Dell XPS 13", " ", 989.99,
"Alienware Area 51-m", " ", 1999.99,
"Razer Blade Stealth", " ", 1299.00);
}
public static void trig() {
int trigger = 1;
String orderSummary = "";
int quantity = 0;
String laptop = "";
int lineItem = 0;
if (trigger == 1) {
orderSummary += String.format("%n%, -9d %-30s %8%s $%, 17.2f",
quantity, laptop, " ", lineItem);
trigger = 0;
}
else {
orderSummary += String.format("%n%, -9d %-30s %9s %, 17.2f",
quantity, laptop, lineItem);
}
orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
+ "%n%31s Tax @ 8.25%% %6s %,17.2f"
+ "%n%n%37s TOTAL %5s $%,17.2f%n");
System.out.printf("%s", orderSummary);
}
}
使用一些指向错误的 IDE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.