Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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创建运输成本计算器_Java_If Statement_Calculator - Fatal编程技术网

如何用Java创建运输成本计算器

如何用Java创建运输成本计算器,java,if-statement,calculator,Java,If Statement,Calculator,我正在创建一个计算器来计算运费。代码是这样的: class ShippingCalc { public static void main(String[] args) { int weight = 30; if (weight < 10) { System.out.println("Shipping costs $1."); } else if (weight < 20) {

我正在创建一个计算器来计算运费。代码是这样的:

class ShippingCalc {
    public static void main(String[] args) {
        int weight = 30;

        if (weight < 10) {
            System.out.println("Shipping costs $1.");
        }
        else if (weight < 20) {
            System.out.println("Shipping costs $2.");
        }
        else {
            System.out.println("Shipping costs $3.");
        }
    }
}
if (weight < threshold1) // price is first level
// or, if you like, you can even do a calculation here
else if (weight < threshold2) // price is second level
class ShippingCalc{
公共静态void main(字符串[]args){
整数权重=30;
如果(重量<10){
System.out.println(“运费1美元”);
}
否则,如果(重量<20){
System.out.println(“运费$2”);
}
否则{
System.out.println(“运费3美元”);
}
}
}
这是所有伟大的,但我想创建一个计算器,可以计算的基础上已经设定的值。例如,有这样一句话:

if (weight < 250) {
   // print("Shipping cost is $1);
} else if (weight < 499) {
   // print("Shipping cost is $2);
} else if (weight < 749) {
   // print...etc and it keeps going 
if(重量<250){
//打印(运费为1美元);
}否则,如果(重量<499){
//打印(运费为2美元);
}否则,如果(重量<749){
//打印…等等,它一直在运行
这将基于用户输入,这就是为什么我不希望已经有任何上述约束。是否有可能用Java制作这样一个计算器,无论重量多少,它都会适当地计算运输成本并给出答案


如果是,那我该怎么做呢?

如果成本权重遵循一个公式,你应该用它来计算成本(一点代数不会伤害任何人)

如果“权重到成本”分配是任意的,则可以使用权重作为键,成本作为值来创建

然后可以使用查找低于给定重量的最高重量

范例

public static Integer findCost(Integer weight, 
        NavigableMap<Integer, Integer> costMap){
    Map.Entry<Integer, Integer> cost;
    cost = costMap.lowerEntry(weight);
    if(cost == null){
        cost = costMap.firstEntry();
    }
    return cost.getValue();
}
公共静态整数findCost(整数权重,
导航地图(成本地图){
地图。进场费;
成本=costMap.lowerEntry(重量);
如果(成本==null){
cost=costMap.firstEntry();
}
返回成本。getValue();
}

使用地图的好处是,如果您使用a作为
NavigableMap
的实现,那么您的查找平均为O(log n)。

首先,您需要一个公式或表格来计算运费。例如,“运费是每整十磅重量一美元”

然后,你把重量加到公式中

System.out.println("Shipping cost is $" + (int)(weight/10));
如果希望公式更复杂,可以执行以下操作:

class ShippingCalc {
    public static void main(String[] args) {
        int weight = 30;

        if (weight < 10) {
            System.out.println("Shipping costs $1.");
        }
        else if (weight < 20) {
            System.out.println("Shipping costs $2.");
        }
        else {
            System.out.println("Shipping costs $3.");
        }
    }
}
if (weight < threshold1) // price is first level
// or, if you like, you can even do a calculation here
else if (weight < threshold2) // price is second level

欢迎来到计算机编程的奇妙世界!

是的,通过使用简单的数学和编写代码是可能的。为什么不试试看你能想出什么。最好先尝试解决问题,然后在发布作业之前展示你的努力。同样,这只不过是最基本的代数之类的东西ng我敢打赌,如果你把铅笔放在纸上,你一定能猜出来。这不是我的作业。我正试图把学习Java作为一种嗜好,因此我有了标记嗜好。至于尝试,我已经尝试过了,但我能想到的只是一种有限的方式,它需要无限的代码。我不需要别人帮我做,只要给我一个正确的方向。编辑,嗯,也许吧e不是那么容易,因为你的数字没有逻辑,除非它总是增加150美元。你必须为
…等
位显示更多的数字。请完全显示模式。我没有假设这是家庭作业,因为我的建议对家庭作业或家庭作业有效(来自另一个最有爱好的人)@HovercraftFullOfEels重量增加了150,价格增加了1美元。代码的这一部分就是一个例子,这也许就是为什么它不那么清楚。你只有一次增加了150——前两次增加了250,这让人困惑。同样,这是一个非常简单的数学。做两列数字,你就可以解决它。或者你可以请有人在这里解决它,但真的,不要为此而这样做。锻炼你的大脑;你会很高兴你这么做的。+1对不起,还不能投票。谢谢你的欢迎和回答。@Borelid,关于无界层数。我不太明白你的方法。你能解释一下吗。如果你必须设置,它怎么会是无界的[num_阈值]。如果重量没有限制怎么办?