import java.util.Scanner;
public class Menu {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int choice, quantity;
double price = 0;
double studentprice = 0;
double totalamt = 0;
String option,student;
char option1, studentChar;
do{
System.out.println("menu");
System.out.println("====");
System.out.println("1. Hamburger");
System.out.println("2. CheeseBurger");
System.out.println("3. FrenchFries");
System.out.println("4. SoftDrinks");
System.out.print("Enter your choice (1,2,3 or 4):");
choice = sc.nextInt();
if(choice ==1)
price = 1.5;
else if (choice ==2)
price = 2;
else if (choice ==3)
price = 2.4;
else if (choice ==4)
price= 1.95;
System.out.print("Enter the quantity of your order:");
quantity = sc.nextInt();
totalamt += price * quantity ;
sc.nextLine();
System.out.print("Do you want to continue? (yes/no)?");
option = sc.next();
option1 = option.charAt(0);
System.out.println("");
}
while(option1 == 'y');
System.out.print("Are you a student? (yes/no?)");
student = sc.next();
studentChar = student.charAt(0);
if(studentChar == 'y')
studentprice = totalamt * 0.9;
System.out.print("$ " + studentprice + " is the price you have to pay " );
**else**(studentChar == 'n')
studentprice = price;
System.out.print("$ " + price + " is the price you have to pay " );
}
}`
其他最后一个错误。它在else处声明语法错误,删除此令牌。请帮忙!当我添加do while时,它打印为零。 -Filler填料填料填料填料 -
三江源! :)
答案 0 :(得分:2)
语法不正确。如果您if-block
下有多个陈述,则应使用括号,否则只会在if-block
& {其他声明不属于if-block
的一部分。
在这种情况下,else
语句在if
行后if-block
完成studentprice = totalamt * 0.9;
时没有if(studentChar == 'y'){
studentprice = totalamt * 0.9;
System.out.print("$ " + studentprice + " is the price you have to pay " );
}else(studentChar == 'n'){
studentprice = price;
System.out.print("$ " + price + " is the price you have to pay " );
}
。
{{1}}
答案 1 :(得分:0)
错误就在这里,它应该是否则,如果
**else**(studentChar == 'n')
studentprice = price;
应该是
if(studentChar == 'y'){
studentprice = totalamt * 0.9;
System.out.print("$ " + studentprice + " is the price you have to pay " );
}
else if(studentChar == 'n'){
studentprice = price;
System.out.print("$ " + price + " is the price you have to pay " );
}else{
}
答案 2 :(得分:0)
studentChar = 0;
while(studentChar != 'y' && studentChar != 'n')
{
System.out.print("Are you a student? (yes/no?)");
student = sc.next();
studentChar = student.charAt(0);
}
if(studentChar == 'y')
{
studentprice = totalamt * 0.9;
System.out.print("$ " + studentprice + " is the price you have to pay " );
}
else if(studentChar == 'n')
{
studentprice = price;
System.out.print("$ " + price + " is the price you have to pay " );
}
答案 3 :(得分:0)
我对你的代码感到困惑。你是如何用
编译这段代码的?
else(studentChar == 'n')
您无法使用else检查条件,您必须使用if
import java.util.Scanner;
公共类菜单{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int choice, quantity;
double price = 0;
double studentprice = 0;
double totalamt = 0;
String option,student;
char option1, studentChar;
do{
System.out.println("menu");
System.out.println("====");
System.out.println("1. Hamburger");
System.out.println("2. CheeseBurger");
System.out.println("3. FrenchFries");
System.out.println("4. SoftDrinks");
System.out.print("Enter your choice (1,2,3 or 4):");
choice = sc.nextInt();
if(choice ==1)
price = 1.5;
else if (choice ==2)
price = 2;
else if (choice ==3)
price = 2.4;
else if (choice ==4)
price= 1.95;
System.out.print("Enter the quantity of your order:");
quantity = sc.nextInt();
totalamt += price * quantity ;
sc.nextLine();
System.out.print("Do you want to continue? (yes/no)?");
option = sc.next();
option1 = option.charAt(0);
System.out.println("");
}
while(option1 == 'y');
System.out.print("Are you a student? (yes/no?)");
student = sc.next();
studentChar = student.charAt(0);
if(studentChar == 'y')
{
studentprice = totalamt * 0.9;
System.out.print("$ " + studentprice + " is the price you have to pay " );
}
else if(studentChar == 'n')
{
studentprice = price;
System.out.print("$ " + studentprice + " is the price you have to pay " );
}
}
}
现在可以使用
答案 4 :(得分:0)
试试这个
do
{
System.out.print("Are you a student? (yes/no?)");
student = sc.next();
studentChar = student.charAt(0);
} while(studentChar != 'y' && studentChar != 'n');
if(studentChar == 'y')
{
//do something here
}
else if(studentChar == 'n')
{
//other statement
}