2017-06-20 75 views
-2

下面列出了我应该做的事情的描述。该程序运行,但我不能得到正确的数字输出。我认为逻辑是对的,但它不起作用。为什么我得到错误的输出 - 如果&其他语句

我们得到#个Cookie,这些盒子将= Cookie的数量除以24,如果有额外的Cookie> 0,我们增加一个盒子。

我们带箱子的数量,额外的箱子数量> 0,我们增加一个集装箱。

然后我们有运输容器和箱子的总数。

我不知道为什么它不能正常工作,并且在过去的3个小时里一直困在这个问题上,最后放弃了并寻求帮助。

一盒饼干可以容纳24个饼干,一个容器可容纳75盒饼干 。编写一个程序,提示用户输入总数为 的Cookie,盒子中的Cookie数量以及 容器中的Cookie数量。程序然后输出盒子的数量和容器的数量以运送饼干。请注意,每个方框必须包含指定的 个cookie,并且每个容器必须包含指定数量的 方框。如果最后一盒Cookie的数量少于指定的 Cookie数量,您可以丢弃它并输出剩余的Cookie数量。同样, 如果最后一个容器包含少于指定框的数量,则您可以丢弃它并输出剩余框的数量。因为这是章节 在选择性控制结构上,如果没有cookie或没有框 剩余,则剩余的cookie或剩余的框不得输出。

#include<iostream> 
#include<cmath> 

using namespace std; 

int main() 

{ 
// Declare variables 
    int cookies, extraCookies; 
    int boxes,totalBoxes; 
    int leftOverBoxes; 
    int containers; 
    boxes = (cookies/24); 

    extraCookies = cookies%24; 
    containers = boxes/75; 
    leftOverBoxes = boxes%75; 


// Ask for the user input (doesnt make sense because we are given the capacities) 
    cout << " How many cookies do you have ?"<< endl; 
    cin >> cookies; 
    cout << " You have this many cookies: " << cookies << endl; 

    cout << " How many cookies can you fit in one box ?" << endl; 
    cin >> cookies; 

    cout << " How many cookie boxes can you fit into a container ?" << endl; 
    cin >> boxes; 

    // Computer number of boxes needed 
    if(extraCookies > 0) 
     totalBoxes = (boxes + 1); 
    cout <<" We need this many boxes:" << totalBoxes << endl; 

    if(leftOverBoxes > 0) 
     containers = (containers + 1); 
    cout << "We need this many containers: " << containers << endl; 


    return 0; 



    } 
+0

该代码运行,但它实际上并没有给我正确的数字,我一直坚持过去3个小时试图弄清楚这一点。有人可以帮帮我吗 ? –

+0

我强烈建议在继续之前参加[游览]和学习[问]。 – jonrsharpe

+0

仅仅提供你已经被问及你的解决方案到目前为止的问题不足以解决堆栈溢出问题。请解释你的例子假设要做什么,它在做什么,你尝试过什么,以及这些努力的结果。 –

回答

0

C++自顶向下读取。请允许我评论添加到您的代码,并解释这是怎么回事:

#include<iostream> 
#include<cmath> 

using namespace std; // Bad practice, by the way 

int main() 

{ 
// Declare variables 
    int cookies, extraCookies; // cookies = 0, extraCookies = 0 
    int boxes,totalBoxes; // boxes = 0, totalBoxes = 0 
    int leftOverBoxes; // leftOverBoxes = 0 
    int containers; // containers = 0 
    boxes = (cookies/24); // boxes = 0/24 = 0, this line does nothing 

    extraCookies = cookies%24; // extraCookies = 0%24 = 0, this line does nothing 
    containers = boxes/75; // containers = 0/75 = 0, this line does nothing 
    leftOverBoxes = boxes%75; // leftOverBoxes = 0%75 = 0, this line does nothing 


// Ask for the user input (doesnt make sense because we are given the capacities) 
    cout << " How many cookies do you have ?"<< endl; 
    cin >> cookies; 
    cout << " You have this many cookies: " << cookies << endl; 

    cout << " How many cookies can you fit in one box ?" << endl; 
    cin >> cookies; // You throw away how many cookies the user has, and rewrite it with how many cookies fit in one box 

    cout << " How many cookie boxes can you fit into a container ?" << endl; 
    cin >> boxes; 

    // Computer number of boxes needed 
    if(extraCookies > 0) // 0 > 0 == false 
     totalBoxes = (boxes + 1); 
    cout <<" We need this many boxes:" << totalBoxes << endl; 

    if(leftOverBoxes > 0) // 0 > 0 == false 
     containers = (containers + 1); 
    cout << "We need this many containers: " << containers << endl; 


    return 0; 



    } 

你从用户采取输入之前计算boxesextraCookiescontainersleftOverBoxes。它们全部初始化为0.

您应该在接受用户输入后计算它们。

将代码视为一系列指令。如果你还没有一定数量的cookies,你无法弄清楚你需要多少盒子。

+0

我是新手,所以这意味着我需要将这些计算移动到if语句之下? –

+0

在if语句之上,输入以下。 –

+0

那么为什么cookies/24行什么都不做?我以为我可以使用操作员的话? 从我可以告诉我很多我的代码大部分没有做什么?我已经阅读了与这个迷你项目有关的整个章节,我想我完全失去了。我可以不考虑任何其他方式来做到这一点? –