我正在进行一项任务,目前运行良好。但有几个方面不起作用。对于初学者来说,我的int total和int counter的计数器不起作用。此外,我的if语句似乎不工作。现在我已经挠了脑袋好几天了。计数器将无法工作结束循环
该分配需要一个程序输入订单号,并根据客户有多少订单进行循环。它还要求客户姓名,标志类型(木头或塑料),字符数和字符颜色。
一些详细信息:
- 底价为所有迹象为$ 20
- 如果标志是木头,则加10美元。如果是塑料加5美元。
- 前5个字母/数字包含在基准价格中,每增加一个字符,则包含2美元。
- 黑色或白色字符包含在基本价格中,彩色字母额外收取8美元。
- 如果总费用超过100美元,可享受总价格25%的折扣。
这里是我的代码现在:
import java.util.Scanner;
public class Carpenter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int orderNumber;
String custName;
String signType;
int numOfCharacters;
String color;
int i = 20;
double total;
int counter;
System.out.println("Enter your order number");
orderNumber = sc.nextInt();
counter=orderNumber;
counter--;
sc.nextLine();
System.out.println("Enter customer name");
custName = sc.next();
do{
System.out.println("Enter the sign type (wood or plastic)");
signType = sc.next();
if(signType == "wood") {
i+=10;
}
if(signType == "plastic") {
i+=5;
}
System.out.println("Enter the number of characters");
numOfCharacters = sc.nextInt();
if(numOfCharacters > 5) {
i += 2*(numOfCharacters-5);
}
System.out.println("Enter the color of characters");
color = sc.next();
if(color != "white" || color != "black") {
i += 8;
}
total= i;
System.out.println("Total is: $" + total);
if(total > 100) {
total = (total * 0.25);
System.out.println("The total is " + total);
}
}
while(counter <= orderNumber);
}
}
'......几个方面都不起作用......' - 请更具体一些。你的程序如何不起作用? – azurefrog 2014-10-09 22:12:47
请了解如何调试代码(或了解调试的内容)。这是一个很好的教程:http://www.vogella.com/tutorials/EclipseDebugging/article.html或者如果你不使用eclipse:http://www.javaworld.com/article/2077445/testing-debugging /debug-with-jdb.html – gerrytan 2014-10-09 22:13:02
您不应该将字符串与'=='和'!='进行比较,因为这会检查对象身份。改用'.equals'。 – 2014-10-09 22:13:12