- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
任何人都可以帮我解决我的代码吗,我在卖苹果时遇到问题,它减去我的所有股票,有时当我的输入超过股票时,数字会变成负数
int main(void){
int choice = 0;
int days = 1, i, buyApple;
int stocks[99] = {0};
for (;;){
clrscr();
printf("Day %d\n", days);
printf("1. harvest\n");
printf("2. View Stocks\n");
printf("3. Sell\n");
printf("\nchoice: ");
scanf("%d", &choice);
if (choice == 1){
clrscr();
printf("Input No. of Apple harvested: ");
scanf("%d", &stocks[days]);
days++;
}
if (choice == 2){
clrscr();
printf("Day Stocks\n");
for (i = 1; i < days; i++){
printf("%2d %4d\n", i, stocks[i]);
}
getch();
}
if(choice == 3){
printf("Input No. of Appple to be sold: ");
scanf("%d", &buyApple);
for (i = 0; i < buyApple; i++){
stocks[i] = stocks[i] - buyApple;
}
if(stocks[i] > buyApple)
printf("Out of Stocks!");
getch();
}
}
}
预期输出:
例如我的股票是这样的
Day Stocks
1 100
2 50
3 50
4 180
5 200
如果我输入要出售的苹果数是200
我的股票会变成这样
Day Stocks
1 0
2 0
3 0
4 180
5 200
一进一出
当我的输入超过库存时,它会显示“缺货!”
并且不会继续减少我的库存
最佳答案
第三个选择中的问题如问题评论中所示,正确的代码应该是这样的(它是c#):
if (choice == 3)
{
printf("Input No. of Appple to be sold: ");
scanf("%d", &buyApple);
for (i = 1; i < sizeof(stocks); i++) //Looping on all stocks you have
{
if (buyApple <= stocks[i]) //if amount of apple less than stock apples ,then remove them from stock
{
stocks[i] = stocks[i] - buyApple;
buyApple = 0;
break;
}
else //if amount of apple is bigger than stock apples
{
if ((sizeof(stocks) - 1) == i){ //if it's the Last stock,then there is no apples in stock
printf("Out of Stocks!");
}
else //take amount of apples from current stock
{
buyApple = buyApple - stocks[i];
stocks[i] = 0;
}
}
}
getch();
}
关于c - 出售苹果股票(一进一出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35920230/
需要帮助将这些给定的数字打印成星号,但我是编程新手;我该怎么做? #include int main(void) { int a[5]={20,1,5,15,12}; int i=0
使用 Delphi XE 2 我试图确定缩放方向以将缩放效果应用于图像(TImage),但没有找到执行此操作的函数,并且图像的 OnGesture 事件中的 EventInfo 属性没有此信息. 我见
我不知道制服在内存中是如何表示的。 制服似乎会占用宝贵的寄存器空间,但它们最终会传入/通过/传出到全局内存中,对吗? 制服不用时情况会发生变化吗?编译器可以将它们优化掉吗?--在这种情况下,我已经将无
我正在尝试在名为“timeclock”的模型上记录“time_in”和“time_out”记录。这是我想做但无法开始工作的事情! 检查最后一个时钟条目,看看它是否同时填充了“time_in”和“tim
我想听听您如何解决这种编程任务!?每种类型(OPER = 1 类型)对应一种特定的信息。 这只是大约 10 个具有相同结构的规范之一。首选创建这些“转换器”(协议(protocol))的通用方法。 最
我正在使用 Rest API(NodeJS、Express)和 PostgreSQL 制作 React-Native 应用。 在我的本地机器上托管时一切正常。当 API 托管在我的机器上并且 Post
我是一名优秀的程序员,十分优秀!