虽然循环不会停止循环

时间:2015-09-18 05:09:30

标签: c while-loop

我想我搞砸了,我的代码过度循环(它不会停止)。输入0后,它将继续循环。请指出我哪里做错了。

// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
          <?php the_title(); echo "<br/>"; ?>
    <?php endwhile; ?>
    <nav>
      <?php previous_posts_link('&laquo; Newer',$wp_query->max_num_pages); ?>
      <?php next_posts_link('Older &raquo;',$wp_query->max_num_pages); ?>
    </nav>
    <?php wp_reset_postdata(); ?>                           
    <?php else : ?>
        <p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>

当前输出:

#include <stdio.h>

int main()
{
    int day=1, exp=1, tallow;
    float allow,tday,daily, texpen;

    printf("\nDay %d allowance. [Type -1 to stop] --> ", day);
    scanf("%f", &allow);

    while(allow != -1){
        printf("-- Expenses %d [type 0 to stop]  ", exp);
        scanf("%f", &daily);exp++;
        while(daily == 0){
            printf("\nDay %d allowance : %.2f", day, allow);
            tday += daily;
            printf("\nDay %d expenses  : %.2f", day, tday);
            break;
        }
    }

        printf("\nTotal allowance for %d days : %.2f", day, tallow);
        printf("\nTotal expenses for %d days : %.2f\n", day, texpen);

    getch();
    return 0;
}

如何摆脱&#34;费用4&#34;?/如何在输入0后停止循环?

1 个答案:

答案 0 :(得分:1)

您可以更改

    while(daily == 0){

    if(daily == 0){

所以休息;将退出

的循环
    while(allow != -1){

然后不再打印“费用4”。

并且您还需要使用

将allow定义为int
scanf("%d", &allow);

它应该是:

...
while(allow != -1){
    printf("-- Expenses %d [type 0 to stop]  ", exp);
    scanf("%f", &daily);exp++;
    tday += daily;
    if(daily == 0){
        printf("\nDay %d allowance : %.2f", day, allow);
        printf("\nDay %d expenses  : %.2f", day, tday);
        break;
    }
}
相关问题