虽然声明不起作用C.

时间:2013-06-28 05:01:15

标签: c while-loop do-while

我正在学习C,我正在尝试制作一个基本的计算器,但是在使用while语句时遇到了一些麻烦。我尝试过多种方式,但它从不重复,只需完成脚本。

有什么想法吗?

//
//  main.c
//  Calculator
//
//  Created by Austen Patterson on 2013-06-27.
//  Copyright (c) 2013 Austen Patterson. All rights reserved.
//

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int number[100];
    int operator = '0';
    int doAgainAnswer = '0';
    bool doAgain;
    do{ 
    printf("Please enter your first number:");
    scanf("%d", &number[1]);
    printf("\nYou entered %d as your first number. Please enter your second: ", number[1]);
    scanf("%d", &number[2]);
    printf("\nYou entered %d as your second number.", number[2]);
    printf("\nYour numbers are now %d and %d", number[1], number[2]);
    printf("\nNow enter your operator.\n1 for addition\n2 for subraction\n3 for multiplication\n4 for division.\n");
    scanf("%d", &operator);

    if(operator == 1){
        int finished = number[1] + number[2];
        printf("\n\n%d \+ %d is: %d", number[1], number[2], finished);

    }
    if(operator == 2){
        int finished = number[1] - number[2];
        printf("\n\n%d \- %d is: %d", number[1], number[2], finished);

    }
    if(operator == 3){
        int finished = number[1] * number[2];
        printf("\n\n%d \* %d is: %d", number[1], number[2], finished);

    }
    if(operator == 4){
        int finished = number[1] / number[2];
        printf("\n\n%d \/ %d is: %d", number[1], number[2], finished);
    }

        printf("\nWant to continue?\n 1 for Yes\n 2 for No\nAnswer: ");
        scanf("%d", &doAgainAnswer);
        if(doAgainAnswer == 1) {
          doAgain = '1';
        } else {
          doAgain = '0';

        }
    }while(doAgain == '1');
}

2 个答案:

答案 0 :(得分:5)

已编辑的代码

#include <stdio.h>
#include <stdbool.h>

int main()
{
    int number[100];
    int operator = '0';
    int doAgainAnswer = 0;//edited
    int doAgain=0;//edited
    do{ 
    printf("Please enter your first number:");
    scanf("%d", &number[1]);
    printf("\nYou entered %d as your first number. Please enter your second: ", number[1]);
    scanf("%d", &number[2]);
    printf("\nYou entered %d as your second number.", number[2]);
    printf("\nYour numbers are now %d and %d", number[1], number[2]);
    printf("\nNow enter your operator.\n1 for addition\n2 for subraction\n3 for multiplication\n4 for division.\n");
    scanf("%d", &operator);

    if(operator == 1){
        int finished = number[1] + number[2];
        printf("\n\n%d \+ %d is: %d", number[1], number[2], finished);

    }
    if(operator == 2){
        int finished = number[1] - number[2];
        printf("\n\n%d \- %d is: %d", number[1], number[2], finished);

    }
    if(operator == 3){
        int finished = number[1] * number[2];
        printf("\n\n%d \* %d is: %d", number[1], number[2], finished);

    }
    if(operator == 4){
        int finished = number[1] / number[2];
        printf("\n\n%d \/ %d is: %d", number[1], number[2], finished);
    }

        printf("\nWant to continue?\n 1 for Yes\n 2 for No\nAnswer: ");
        scanf("%d", &doAgainAnswer);
        if(doAgainAnswer == 1) {
          doAgain = 1;//edited
        } else {
          doAgain = 0;//edited

        }
    }while(doAgain == 1);//edited
return 0;//edited
}

答案 1 :(得分:2)

将while条件更改为

while(doAgain==1)

在所有doMain作业中,使用0或1。

其他方式是使用While(1)并在用户在doAgainAnswer中的提示符下输入0时中断。