C 如何基于另一个变量和某些算术定义变量的值

C++ How to define the value of a variable based on another variable and some arithmetic

本文关键字:变量 定义 何基于 另一个      更新时间:2023-10-16

我是C 的新手,并且通常是编程。在我的程序中,我试图通过向用户索取地球重量,然后他们想要将地球重量进行比较的行星来计算其他行星上的物体的重量。我希望使用Switch(PlanetNumber),希望根据他们选择的数字(1-6),该程序将根据所选情况定义可变的'''。我遇到了一个错误:"分配权重 * 0.78 = etheraweight;"所需的lvalue;''这没有按照预期的方式工作,任何人都可以指向正确的方向吗?

#include <iostream>
int main()
{
    double weight;
    int planetnumber;
    double otherweight;
    std::cout << "Enter weightn";
    std::cin >> weight;
    std::cout << "Please select your arenan";
    std::cin >> planetnumber;
    switch (planetnumber)
    {
        case 1:
            weight * 0.78 = otherweight;
            break;
        case 2:
            weight * 0.39 = otherweight;
            break;
        case 3:
            weight * 2.65 == otherweight;
            break;
        case 4:
            weight * 1.17 = otherweight;
            break;
        case 5:
            weight * 1.05 = otherweight;
            break;
        case 6:
            weight * 1.23 = otherweight;
            break;
        default:
            std::cout << "Not a valid inputn";
            break;
    }
    std::cout << "your weight is " << otherweight << std::endl;
}

在c/c 中,结果在左侧,方程式在右侧。您只需要转置例如。

otherweight = 0.78 * weight;