gpt4 book ai didi

c++ - 无法计算方程,总是给出零

转载 作者:行者123 更新时间:2023-11-28 07:54:56 27 4
gpt4 key购买 nike

我错过了什么吗?变量 percentage_ 始终等于 0。我检查了 nTimes 和 winnings,它们给出了正确的输入值。即使我测试了一个简单的方程式,例如 percentage_=1+1,percentage_ 也会给出 0。有人可以帮忙吗?

    #pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;


class GuessMachine
{
private:
int nTimes;
int winnings;
string nM[6];

public:
GuessMachine();
void displayPrizes();
void displayMenu();
int getInput();
void checkNumber();
void checkPrize();
};



void GuessMachine::checkPrize()
{
MagicNumber mn;
int prize_=mn.generateNumber();
float percentage_;

percentage_ = float (winnings/nTimes*100); //<--On this line percentage is always 0 no matter what winnings and nTimes are
cout<<"Percentage is "<<percentage_<<endl;

if(percentage_ >= 50)
{
cout<<"You have scored "<<percentage_<<"% and won "<<nM[prize_];
}
else
{
cout<<"You have scored "<<percentage_<<"%. You lose!!";
}

cin.ignore();
cin.ignore();
}

最佳答案

尝试

float (winnings) /nTimes*100

相反。

您的版本仍将 int - 0 转换为 float。

如果 / 的一个操作数是一个 float ,它就可以工作。

关于c++ - 无法计算方程,总是给出零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12880912/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com