java - 计数器无法结束循环

标签 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);

    }

}

最佳答案

我添加了评论来指导您完成我所做的更改。另外,请记住在获得用户输入后调用 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);
        }




}

关于java - 计数器无法结束循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288707/

相关文章:

java - Spring AuthenticationEntryPoint 返回 Tomcat HTML 错误而不是 JSON

java - Hibernate注释: Many-to-many with shared composite key attribute

java - Spring Boot自动配置包括

java - 了解 JPA 唯一约束的原因

java - 有什么方法可以使用 JAX-WS 从 Javadoc 生成 WSDL 文档?

Java:使用字符串作为源代码的一部分

java - 二维数组中缺少数组检查 - Java

java - 为什么我们需要像 Web 应用程序中的 dozer 这样的 bean 到 bean 映射器

java - 如何检查一个类是否扩展了另一个

java - 如何在Java中的另一个文件中使用自定义类?