我正在尝试使用嵌套的if / else结构和while循环完成分配。但是,以某种方式,我的第二种方法导致了错误。让我知道您是否可以帮助我,因为这是我关于SO的第一个问题。
我尝试用方括号关闭第一个方法,但这只会引起更多错误。我不知道从这里还能去哪里,我在Google各处寻找帮助。
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); }}
答案 0 :(得分:0)
好吧,这是一个问题:
while( (senitel == 'y') || (senitel == 'Y') );
{
看到最后的分号了吗?可能不是您想要的。
使用IDE自动缩进代码。然后遍历所有错误并修复语法错误。认真地。
答案 1 :(得分:0)
您忘记了花括号。
这是您的固定示例:
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。