Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反赢';t工作到结束循环_Java - Fatal编程技术网

Java 反赢';t工作到结束循环

Java 反赢';t工作到结束循环,java,Java,我正在做一项作业,到目前为止效果很好。但有几个方面不起作用。首先,我的int-total计数器和int-counter计数器不起作用。而且我的if语句似乎不起作用。我已经搔头好几天了 分配需要一个程序来输入订单号,并根据客户的订单数量进行循环。它还需要客户名称、标志类型(木质或塑料)、字符数和字符颜色 更多信息: 所有标志的底价为20美元 如果标志是木制的,加10美元。如果是塑料的,加5美元 前5个字母/数字包含在底价中,每增加一个字符2美元 黑色或白色字符包含在底价中,彩色字母另加8美元 如

我正在做一项作业,到目前为止效果很好。但有几个方面不起作用。首先,我的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);

    }

}
import java.util.Scanner;
公营木匠{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int订单号;
字符串名称;
字符串符号类型;
整数字符;
字符串颜色;
int i=20;
双倍总数;
整数计数器;
System.out.println(“输入您的订单号”);
orderNumber=sc.nextInt();
计数器=订单号;
计数器--;
sc.nextLine();
System.out.println(“输入客户名称”);
custName=sc.next();
做{
System.out.println(“输入标志类型(木质或塑料)”;
signType=sc.next();
如果(标志类型=“木材”){
i+=10;
}
如果(标志类型=“塑料”){
i+=5;
}
System.out.println(“输入字符数”);
numOfCharacters=sc.nextInt();
如果(字符数>5){
i+=2*(num-5);
}
System.out.println(“输入字符的颜色”);
颜色=sc.next();
如果(颜色!=“白色”| |颜色!=“黑色”){
i+=8;
}
总数=i;
System.out.println(“总计为:$”+总计);
如果(总数>100){
总计=(总计*0.25);
System.out.println(“总计为”+总计);
}
}

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

然后在循环结束时,您应该增加
计数器

do{
    //code
    counter++;
}
while(counter <= orderNumber);
do{
//代码
计数器++;
}

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

import java.util.Scanner;
公营木匠{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int订单号;
字符串名称;
字符串符号类型;
整数字符;
字符串颜色;
int i=20;
双倍总数;
整数计数器;
//我改变了措辞只是因为它有点混乱
System.out.println(“输入订单数量”);
orderNumber=sc.nextInt();
计数器=订单号;
sc.nextLine();
System.out.println(“输入客户名称”);
custName=sc.next();
sc.nextLine();
//当您知道要重复多少次(比如当用户告诉您有多少次)我更喜欢使用for循环时,do-while循环也可以

对于(int x=0;x
…几个方面不起作用…
--请更具体一些。您的程序不起作用了吗?请学习如何调试代码(或了解调试是什么)。这是一个很好的教程:如果您不使用eclipse,也可以这样做:您不应该将字符串与
=
!=
进行比较,因为这只会检查对象标识。请改用
.equals
。如果您指出当前未显示的特定行为,这将非常有用。我的计数器因为命令的数量是行不通的你是我的英雄,祝你好运。。。
do{
    //code
    counter++;
}
while(counter <= orderNumber);
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);
        }




}