2009-02-13 155 views
2

我有这一点,但有一个错误,我不明白这一点:(。我是比较新的的Java。帮助需要正确的Java代码

package hw; 
import java.util.Scanner; 

public class Problem1 
{ 
    public static void main (String [] args) 
    { 
     int cost; int number; double cost_unit; double total; 

     Scanner entrada = new Scanner(System.in);    

     System.out.println("Please enter the cost of the product."); 
     cost = entrada.nextInt(); 

     while (cost>0){ 

      System.out.println("Please enter the amount of units to be sold"); 
      number = entrada.nextInt(); 

      if (number>0); 

      cost_unit = cost * 1.4; 
      total = cost_unit*number; 

      System.out.printf("Cost per unit will be $ %d\n",cost_unit); 
      System.out.printf("Cost per unit will be $ %d\n",total);         
     } 
    }     
} 

//我只是想用户进入成本的产品,给多家单位订购,我想程序找出产品的最终价格有40%的利润

回答

2

在printf的,%d为符号十进制整数,而cost_unittotal双打。您应该使用%f

System.out.printf("Cost per unit will be $ %f\n",cost_unit); 
System.out.printf("Cost per unit will be $ %f\n",total); 
1

一些提示:

while (cost>0) 

待办事项你真的想要while -loop 这里?您似乎在检查如果的成本是正数。

if (number>0); 

if语句没有任何用处。

+0

我建议帖子作者使用像NetBeans这样的IDE ......它会强调`if(number> 0)`并给出警告。 – cdmckay 2009-02-13 23:58:30

1

它似乎是“if(number> 0);”声明不会有太大的帮助。

尝试将其更改为:

if (number>0) { 
    cost_unit = cost * 1.4; 
    total = cost_unit*number; 

    System.out.printf("Cost per unit will be $ %d\n",cost_unit); 
    System.out.printf("Cost per unit will be $ %d\n",total); 
} 

编辑:另外,它似乎有一个无限循环,如果指定的(第一个)成本> 0,因为你是不是在循环改变它(成本)。

1

没有在代码看太深,一件事,引起我的注意的是:

if (number>0); 

cost_unit = cost * 1.4; 
total = cost_unit*number; 

也许你会喜欢这样的:

if (number>0) { 
    cost_unit = cost * 1.4; 
    total = cost_unit*number; 
} 

顺便说一句,我以前见过这个。 ....但我不太确定哪里...

+0

谢谢你!但现在我正在运行该程序,似乎我的变量被错误地声明,有关于此的任何提示? – 2009-02-14 00:03:15

1

更改

if (number>0); 

    cost_unit = cost * 1.4; 
    total = cost_unit*number; 

 if (number>0) { 
     cost_unit = cost * 1.4; 
     total = cost_unit*number; 
     } 
1

如果你研究documentation打印你的结果,当您正在使用的格式语法,你会意识到你正在使用的整数格式(%d)如你想作为一个浮点数格式化(%F )。

1

最后两行应该是:

System.out.printf("Cost per unit will be $ %f\n", cost_unit); 
System.out.printf("Total cost will be $ %f\n", total); 
1

这里,试试这个:

我认为它几乎做了你所需要的。

package hw; 

import java.util.Scanner; 


public class Problem1 { 
    public static void main (String [] args) { 
     int cost; 
     int number; 
     double cost_unit = 0; 
     double total = 0; 

     Scanner entrada = new Scanner(System.in); 

     System.out.println("Please enter the cost of the product."); 
     cost = entrada.nextInt(); 
     System.out.println("Please enter the amount of units to be sold"); 
     number = entrada.nextInt(); 

     cost_unit = cost * 1.4; 
     if (number>0) { 
      total = cost_unit*number; 
     } 

     System.out.printf("Cost per unit will be $ %f\n",cost_unit); 
     System.out.printf("Total cost will be $ %f\n",total); 
    } 
} 

试一下,看看它是否有效。您有没有机会参加ITAM?

编辑

关于循环您原来是正确的。只需围绕你想要重复的代码,并添加一个条件退出。

像这样的东西创建扫描器(几乎你在第一次尝试做的方式)之后:

while(cost > 0) { 
    System.out.println("Please enter the cost of the product (0 to exit the progam"); 
    cost = entrada.nextInt(); 
    ......... 
    ......... 
    ......... 
    System.out.printf("Total cost will be $ %f\n",total) 

} 

这将重复括号之间的代码,而成本是大于0

中当然你应该改变成本的初始值,否则它不会首次进入lopp,也许你应该在每次迭代之前清理这些值。

+0

它的工作原理!这是一个消极的,我在洪都拉斯学习。顺便说一句..我怎么能让程序重复自己,我想用循环,但我没有太多的经验:) – 2009-02-14 00:23:54

2

尝试并实际描述问题:

是否javac未编译它?这是一个语法问题。

该程序是否表现得不像您想要的那样?哪些部分不起作用?这是一个逻辑问题。

没有足够的问题描述,你怎么能期望你或其他人能够轻松找到错误?当你继续学习更多关于编程的知识时,你将需要这个技能。

返回到你的代码中发现的问题:单个项目的

  • 成本是整数。如果我想出售价格为0.75美分的口香糖没有意义
  • 如果您输入任何内容但0为成本,while循环将无限循环
  • 如果条件不起作用,您已输入0或更少的单位或超过0个单位
  • cost_unit = cost * 14您可能想将cost_unit命名为retail_unit之类的东西 - 因为这是零售价格。成本*利润率=零售价格,不是?