2014-10-09 37 views
0

我正在进行一项任务,目前运行良好。但有几个方面不起作用。对于初学者来说,我的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); 

    } 

} 
+0

'......几个方面都不起作用......' - 请更具体一些。你的程序如何不起作用? – azurefrog 2014-10-09 22:12:47

+1

请了解如何调试代码(或了解调试的内容)。这是一个很好的教程: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

+3

您不应该将字符串与'=='和'!='进行比较,因为这会检查对象身份。改用'.equals'。 – 2014-10-09 22:13:12

回答

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); 
     } 




} 
+0

你是我的英雄。一帆风顺...... – confused 2014-10-10 02:57:30

1

您应该设置counter为正确的初始值(这大概是1,你的情况):在结束

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

然后你应该增加你的counter

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