帮助需要纠正Java代码

时间:2022-06-20 02:04:38

I have this but there is an error, I don't get it :(. I'm relatively new to 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);                                
        }
    }                
}

// I just want the user to enter the cost for a product, give a number of units to be ordered, and I want the program find out the final price of the product with a 40% profit.

//我只想让用户输入产品的成本,给出一些订购的单位,我希望程序找出产品的最终价格,获得40%的利润。

9 个解决方案

#1


In printf, %d is for a signed decimal integer, whereas cost_unit and total are doubles. You should use %f instead.

在printf中,%d用于带符号的十进制整数,而cost_unit和total是双精度数。你应该用%f代替。

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

#2


Try and actually describe the problem:

尝试并实际描述问题:

Did javac not compile it? That's a syntax problem.

javac没有编译吗?这是一个语法问题。

Did the program not behave as you intended it to be? What parts is not behaving? That's a logic problem.

程序没有按照您的意图行事吗?哪些部分没有表现?这是一个逻辑问题。

Without adequate description of the problem, how could you expect that you, or others, could locate the errors easily? You're going to need this skill as you go on learning more about programming.

如果没有对问题的充分描述,您怎么能指望您或其他人轻易找到错误?当你继续学习有关编程的更多知识时,你将需要这种技能。

Back to finding problems in your code:

回到代码中发现问题:

  • cost of the individual items are integers. Doesn't make sense if I want to sell gum that costs .75 cents
  • 单个项目的成本是整数。如果我想卖掉0.75美分的口香糖是没有意义的

  • the while loop would loop infinitely if you enter anything but 0 for the cost
  • 如果输入除成本之外的任何值,则while循环将无限循环

  • if condition does nothing... i.e. it makes no difference whether you've entered 0 or less units or more than 0 units
  • 如果条件什么也不做......也就是说,无论你输入的是0或更少的单位还是超过0单位都没有区别

  • cost_unit = cost * 14 you probably want to name cost_unit to something like retail_unit - because that's the retail price. cost * profit margin = retail price, no?
  • cost_unit = cost * 14您可能希望将cost_unit命名为retail_unit,因为这是零售价格。成本*利润率=零售价,不是吗?

#3


Some hints:

while (cost>0)

Do you really want a while-loop here? You seem to be checking if the cost is positive.

你真的想要一个while循环吗?你似乎正在检查成本是否为正。

if (number>0);

This if statement doesn't do anything useful.

这个if语句没有做任何有用的事情。

#4


It would seem the "if (number>0);" statement wouldn't help much.

它似乎是“if(number> 0);”声明无济于事。

Try changing it to:

尝试将其更改为:

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

EDIT: Also, it seems there is an infinite loop if the (first) cost specified is > 0, since you aint changing it (cost) in the loop.

编辑:此外,如果指定的(第一)成本> 0,似乎存在无限循环,因为您不在循环中更改它(成本)。

#5


Without looking too deep at the code, one thing that catch my attention is:

在不深入研究代码的情况下,引起我注意的一件事是:

if (number>0);

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

Probably you would like this:

可能你会喜欢这个:

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

BTW, I've seen this before..... but I'm not quite sure where...

顺便说一下,我以前看过这个.....但我不太确定在哪里......

#6


change

    if (number>0);

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

to

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

#7


If you study the documentation for the format syntax that you are using when printing your result, you will realize you are using integer formatting (%d) where you want to format as a floating point number (%f).

如果您研究打印结果时使用的格式语法的文档,您将意识到要使用整数格式(%d)格式化为浮点数(%f)。

#8


The last two lines should be:

最后两行应该是:

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

#9


Here, try this:

在这里,试试这个:

I think it pretty much does what you need.

我认为它几乎可以满足您的需求。

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

Try it out and see if it works. By any chance are you at the ITAM?

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

EDIT

About the loop your original was correct. Just have to surround the code you want to repeat, and add a condition to exit.

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

Something like this after creating the scanner ( pretty much the way you did in the first attempt ) :

创建扫描仪之后会出现类似的情况(几乎与第一次尝试时的方式相同):

 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)

 }

That will repeat the code between braces while cost is greater than 0.

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

Of course you should change the initial value of cost, otherwise it won't enter in the lopp the first time and perhaps you should clean up the values before each iteration.

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

#1


In printf, %d is for a signed decimal integer, whereas cost_unit and total are doubles. You should use %f instead.

在printf中,%d用于带符号的十进制整数,而cost_unit和total是双精度数。你应该用%f代替。

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

#2


Try and actually describe the problem:

尝试并实际描述问题:

Did javac not compile it? That's a syntax problem.

javac没有编译吗?这是一个语法问题。

Did the program not behave as you intended it to be? What parts is not behaving? That's a logic problem.

程序没有按照您的意图行事吗?哪些部分没有表现?这是一个逻辑问题。

Without adequate description of the problem, how could you expect that you, or others, could locate the errors easily? You're going to need this skill as you go on learning more about programming.

如果没有对问题的充分描述,您怎么能指望您或其他人轻易找到错误?当你继续学习有关编程的更多知识时,你将需要这种技能。

Back to finding problems in your code:

回到代码中发现问题:

  • cost of the individual items are integers. Doesn't make sense if I want to sell gum that costs .75 cents
  • 单个项目的成本是整数。如果我想卖掉0.75美分的口香糖是没有意义的

  • the while loop would loop infinitely if you enter anything but 0 for the cost
  • 如果输入除成本之外的任何值,则while循环将无限循环

  • if condition does nothing... i.e. it makes no difference whether you've entered 0 or less units or more than 0 units
  • 如果条件什么也不做......也就是说,无论你输入的是0或更少的单位还是超过0单位都没有区别

  • cost_unit = cost * 14 you probably want to name cost_unit to something like retail_unit - because that's the retail price. cost * profit margin = retail price, no?
  • cost_unit = cost * 14您可能希望将cost_unit命名为retail_unit,因为这是零售价格。成本*利润率=零售价,不是吗?

#3


Some hints:

while (cost>0)

Do you really want a while-loop here? You seem to be checking if the cost is positive.

你真的想要一个while循环吗?你似乎正在检查成本是否为正。

if (number>0);

This if statement doesn't do anything useful.

这个if语句没有做任何有用的事情。

#4


It would seem the "if (number>0);" statement wouldn't help much.

它似乎是“if(number> 0);”声明无济于事。

Try changing it to:

尝试将其更改为:

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

EDIT: Also, it seems there is an infinite loop if the (first) cost specified is > 0, since you aint changing it (cost) in the loop.

编辑:此外,如果指定的(第一)成本> 0,似乎存在无限循环,因为您不在循环中更改它(成本)。

#5


Without looking too deep at the code, one thing that catch my attention is:

在不深入研究代码的情况下,引起我注意的一件事是:

if (number>0);

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

Probably you would like this:

可能你会喜欢这个:

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

BTW, I've seen this before..... but I'm not quite sure where...

顺便说一下,我以前看过这个.....但我不太确定在哪里......

#6


change

    if (number>0);

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

to

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

#7


If you study the documentation for the format syntax that you are using when printing your result, you will realize you are using integer formatting (%d) where you want to format as a floating point number (%f).

如果您研究打印结果时使用的格式语法的文档,您将意识到要使用整数格式(%d)格式化为浮点数(%f)。

#8


The last two lines should be:

最后两行应该是:

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

#9


Here, try this:

在这里,试试这个:

I think it pretty much does what you need.

我认为它几乎可以满足您的需求。

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

Try it out and see if it works. By any chance are you at the ITAM?

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

EDIT

About the loop your original was correct. Just have to surround the code you want to repeat, and add a condition to exit.

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

Something like this after creating the scanner ( pretty much the way you did in the first attempt ) :

创建扫描仪之后会出现类似的情况(几乎与第一次尝试时的方式相同):

 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)

 }

That will repeat the code between braces while cost is greater than 0.

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

Of course you should change the initial value of cost, otherwise it won't enter in the lopp the first time and perhaps you should clean up the values before each iteration.

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