计数器不会结束循环

时间:2014-10-09 22:06:32

标签: java

我正在完成一项任务,到目前为止它运作良好。但是有几个方面是行不通的。首先,我的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);

    }

}

2 个答案:

答案 0 :(得分:1)

您应该将counter设置为正确的起始值(在您的情况下大概为1):

    orderNumber = sc.nextInt();
    counter=1;
   //counter=orderNumber;
   //counter--;

然后在循环结束时,您应该增加counter

do{
    //code
    counter++;
}
while(counter <= orderNumber);

答案 1 :(得分:1)

我添加了评论,引导您完成我所做的更改。另外,请记住在获得用户输入后调用sc.NextLine()函数,以便下次可以输入不同的内容(这称为“刷新”缓冲区)。

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;

//I changed the phrasing just because it is a little confusing
        System.out.println("Enter your number of orders");

        orderNumber = sc.nextInt();

        counter = orderNumber;

        sc.nextLine();

        System.out.println("Enter customer name");

        custName = sc.next();
        sc.nextLine();
//When you know how many times you want to repeat something (like when a user tells you how many) I prefer using a for-loop, a do while loop works as well though
        for(int x=0; x<counter;x++)
        {
            System.out.println("Enter the sign type (wood or plastic)");
            signType = sc.next();
//When comparing Strings, there is a function that you can use to compare them rather than using '=='          
// It is also good to use the 'equalsIgnoreCase()' function to be more user friendly and robust         

            if(signType.equalsIgnoreCase("wood")) {
                i+=10;
            }

            if(signType.equalsIgnoreCase("plastic")) {
                i+=5;
            }

//Flush the buffer (I haven't tested if this is necessary or not, it is good practice though)            
            sc.nextLine();
            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();

//Same concept as above, the differene is the ! before the function to test if it is false or not
            if(!color.equalsIgnoreCase("white") || !color.equalsIgnoreCase("black")) {
                i += 8;
            }
        }
            total =  i;
//You will not want to print this out until the end due to the possibility of it being over $100            
//          System.out.println("Total is: $" + total);
            if( total > 100 ) {
//Mathematically speaking, you are making your total a quarter of what the original is, rather than taking a quarter off. You want 75% rather than 25%
               // total = (total * 0.25);
                total = (total * 0.75);               
            }
            System.out.println("Total is: $" + total);
        }




}