问题描述
我必须制作这个程序,它要求家具类型、材料类型并在显示价格和材料类型的同时计算家具的成本。
// 亚当巴希尔公开课练习1 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
char furntype;
char fabtype;
int c =1000;
int C =1000;
int l =750;
int L =750;
int r =1200;
int R =1200;
String couch="";
System.out.println("Please enter the type of furniture you want! C for couch L for loveseats, and R for Recliner");
furntype = (char)System.in.read();
if (furntype == 'c' || furntype == 'C') {
System.out.println("You chose a couch! It sells for $ " + c);
} else if (furntype == 'l' || furntype == 'L') {
System.out.println("You chose a loveseat! It sells for $750");
} else if (furntype == 'r' || furntype == 'R') {
System.out.println("you chose recliner! It sells for $1200");
} else {
System.out.println("Invalid choice");
}
System.out.println("Would you like fabric or leather for the couch, leather costs 35% more.");
fabtype = (char)System.in.read();
if (fabtype == 'f' || fabtype =='F') {
System.out.println("you selected fabric! your total is $" + furntype);
} else if (fabtype == 'l' || fabtype =='L') {
System.out.println("you chose leather! your new total is $" + fabtype);
} else {
System.out.println("Invalid choice");
}
}
我不明白为什么我的第二组 if 语句不允许我输入结构选择
1楼
为什么不用扫描仪?
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
char furntype;
char fabtype;
int c =1000;
int C =1000;
int l =750;
int L =750;
int r =1200;
int R =1200;
String couch="";
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the type of furniture you want! "+
"C for couch L for loveseats, and R for Recliner");
//Take user input as a String, and get the first char.
furntype = scanner.nextLine().charAt(0);
if (furntype == 'c' || furntype == 'C')
System.out.println("You chose a couch! It sells for $ " + c);
else if (furntype == 'l' || furntype == 'L')
System.out.println("You chose a loveseat! It sells for $750");
else if (furntype == 'r' || furntype == 'R')
System.out.println("you chose recliner! It sells for $1200");
else
System.out.println("Invalid choice");
System.out.println("Would you like fabric or "+
"leather for the couch, leather costs 35% more.");
//Get the first char of input string
fabtype = scanner.nextLine().charAt(0);
if (fabtype == 'f' || fabtype =='F')
System.out.println("you selected fabric! your total is $" + furntype);
else if (fabtype == 'l' || fabtype =='L')
System.out.println("you chose leather! your new total is $" + fabtype);
else{
System.out.println("Invalid choice");
}
}
理想情况下,我建议使用String
而不是char
。
将您的输入视为字符串并使用new Scanner().readLine();
.
然后检查相等性,使用if(input.equalsIgnoreCase("c"))
。
它会让你的程序更容易编写。