简体   繁体   English

Java For循环或While循环用于任务

[英]Java For Loop or While Loop For the Task

Inside a Box can fit 5 Books.一个盒子里面可以放 5 本书。

Shipping the first 5 Boxes cost 5 Dollars /Box.运送前 5 盒的费用为 5 美元/盒。

Shipping more than five Boxes cost 4 Dollars /Box.运送超过五盒的费用为 4 美元/盒。

If the user types in 30 Books.如果用户输入 30 本书。

It will 29 Dollars (5* 5 Dollar / Box ) + ( 4 Dollar / Box)它将 29 美元(5* 5 美元/盒)+(4 美元/盒)

Now if I want to make Java calculate the cost of shipping, I ran into problems.现在如果我想让 Java 计算运费,我遇到了问题。

First, I don't know if I should use For loops or while loops.首先,我不知道我应该使用 For 循环还是 while 循环。

I first did this:我首先这样做:

int Box = books_count_X / 5
int VolumeofFive$BOX = 0
int VolumeofFour$BOX = 0 ;


for (Box ; VolumeofFive$BOX < 6 ; ) {
                    VolumeofFive$BOX = i++;

                }
        

I'm not even sure if that's how you do it, and it doesn't work to my surprise.我什至不确定你是不是这样做的,这并不令我惊讶。

I don't know how to write that in code: for each Box, increase VolumeofFive$BOX by 1, as long as VolumeofFive$BOX didn't reach 6.我不知道如何在代码中编写它:对于每个 Box,只要VolumeofFive$BOX没有达到 6, VolumeofFive$BOX增加 1。

How do you write it in code?你是怎么用代码写出来的?

Thank you a lot!十分感谢!

Below is an example of what would give you the right result.下面是一个例子,说明什么会给你正确的结果。 30 books would result in 29 Dollars. 30本书将产生29美元。

int Box = (books_count_X + 4) / 5;
int VolumeofFive$BOX = 0;
int VolumeofFour$BOX = 0 ;

for ( int i = 1; i <= Box; i++ ) {
    if ( i <= 5 ) {
        VolumeofFive$BOX++; // increment the amount of boxes
    } else {
        VolumeofFour$BOX++; // increment the other amount of boxes
    }
}

// total for first 5 boxes:
int totalA = VolumeofFive$BOX * 5;
// total for all the remaining boxes:
int totalB = VolumeofFour$BOX * 4;
// total amount
int total = totalA + totalB;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM