| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:菜鸟试用循环... 不知道错在哪里...
只看楼主 加入收藏
tc121091
Rank: 1
等 级:新手上路
帖 子:6
专家分:2
注 册:2011-1-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
菜鸟试用循环... 不知道错在哪里...
我想做的是
先打了价钱 , 再询问付几多钱
并显示还要付的余额
直至付完为止

/* Trial for break function*/
#include<stdio.h>
int main()
{
    int price,pay=0,remain;
    printf("What is the price? ");scanf("%d",&price);
    while (price>0)
        {
            printf("How much do you pay? ");scanf("%d",&pay);
            if (pay<=0)
                printf("You need to enter the positive no.\n");
            else
                price=price-pay;
                printf("You still need to pay %d\n",price);
            if(price<=0)
                break;
    }
        printf("You have clear the pay\n");
    return 0;
}

问题是 : 假若还剩 5元 , 我付了8元
他会先显示我还剩 -3 元
才说我已经付清
如何直接说我付清了, 而不显示那负数?
还有一开始输入价钱时
要展开 if 才可以确保输入正数吗?

只在学校上了两课最基本的
想自己先自习一下
却发现真的很有难度...

问题多多的,抱歉了...
搜索更多相关主题的帖子: 余额 
2011-01-25 01:23
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:10 
#include<stdio.h>
int main()
{
    int price,pay=0,remain;
    printf("What is the price? ");
    scanf("%d",&price);
    while (price>0)
        {
            printf("How much do you pay? ");
            scanf("%d",&pay);
            if (pay<=0)
                printf("You need to enter the positive no.\n");
            else
               {
                price=price-pay;
                if(price>0)
                printf("You still need to pay %d\n",price);
                else
                break;
                }
       }
        printf("You have clear the pay\n");
    return 0;
}

   唯实惟新 至诚致志
2011-01-25 05:20
yimaoqian198
Rank: 4
等 级:业余侠客
帖 子:108
专家分:287
注 册:2010-5-10
收藏
得分:0 
来过
2011-01-25 09:40
刘定邦
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:687
专家分:1570
注 册:2010-9-21
收藏
得分:0 
学习...
2011-01-25 13:34
cxycctv123
Rank: 2
等 级:论坛游民
帖 子:8
专家分:19
注 册:2010-11-22
收藏
得分:10 
#include<stdio.h>
main()
{
    int price,pay,remain;
    while(1)
    {
       printf("what is the price:\n");
       scanf("%d",&price);
       if(price>0)
           break;
    }
    while(price>0)
    {
    printf("how much do you pay?\n");
    scanf("%d",&pay);
    if(pay<=0)
        printf("you need to enter the positive now\n");
    else
    {
        if((price=price-pay)>0)
            printf("you still need to pay:%d\n",price);
        else
        {
            remain=(-1)*price;
            break;
        }


    }
    }
    printf("you have cleared the pay\n");
    printf("you can get back:$%d\n",remain);
    return(0);
}
2011-01-25 15:27
快速回复:菜鸟试用循环... 不知道错在哪里...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011954 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved