循环矩形区域的公式

时间:2016-10-20 02:09:44

标签: c++ c++11

我在这个任务上的智慧结束了。我需要用户能够做一些事情。

  1. 输入他们想要藏羊的形状(按1或2)。
  2. 输入他们想要多少个畜栏; 3是最大值
  3. 输出每个畜栏的绵羊面积和数量。
  4. 圆形公式有效,但输入了多个符号后矩形会暂停。矩形部分的输出根本不显示。我尝试过使用while,switch,case等的组合,我无法让它工作。

    如何显示公式部分,以及如何使用“while”循环矩形公式3次?有帮助吗?

    // ConsoleApplication16.cpp : Defines the entry point for the console    application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <iomanip>
    
    using namespace std; 
    
    int main()
    {
    const double pie = 3.141592;
    double r;
    double area = 0;
    double length;
    double width;
    int shape;
    double sheep = 2.54;
    int corral;
    
    cout << " Which shape do you want? " << endl;
    cout << " 1. Circle.   2. Rectangle. ";
    cin >> shape;
    cout << " How many corrals do you want? " << endl;
    cin >> corral;
    
    while (corral <= 3)
    
        if (shape == 1)
        {
            cout << "Input radius. " << endl;
            cin >> r;
            cout << fixed << setprecision(2);
            area = pie * pow(r, r);
            cout << endl;
    
            cout << " Here's the area. << " endl;
            cout << area << endl;
            cout << fixed << setprecision(1) << "Number of sheep " << area / sheep << endl;
        }
    
    
    else (shape = 2);
            {   
            cout << "Enter lenght and width. " << endl;
            cin >> length;
            cin >> width;
            area = length * width;
    
            cout << "Here's the area. " << endl;
            cout << area << endl;
            cout << "Number of sheep" << endl;
            cout << fixed << setprecision(2);
            cout << area / sheep;
            }
    
    
    
    
    return 0;
    

    }

2 个答案:

答案 0 :(得分:1)

我建议你先写一个计算区域的方法。 然后使用循环来调用方法1,2或3的次数。

<强>逻辑

1)询问用户是否输入半径

2)调用计算区域方法或仅计算区域

顺便说一句,你不能使用&#34; if else&#34;像这样(shape = 2);

if (condition) 
{}
else
{}

if you need more condition checks

if (condition){}
else if (condition) {}
else {}

答案 1 :(得分:1)

else (shape = 2);

我觉得想写

else if (shape == 2)