C程序重写如果否则 [英] C program reagrding if else

查看:83
本文介绍了C程序重写如果否则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include < stdio.h >
    int main() {
        int age;
        char sex;
        char ms;
        printf("Enter the details of the driver\n");
        scanf("%c%c%d", & ms, & sex, & age);

        if (ms == 'y')
            printf("Driver is insured\n");
        else {
            if (sex == 'm') {
                if (age > 30)
                    printf("Driver is insured\n");
                else {
                    if (age > 25)
                        printf("Driver is insured\n");
                    else
                        printf("Driver is not insured\n");
                }
            }
        }
    }





已添加代码块 - OriginalGriff [/ edit]



我的尝试:



我尝试给出驱动程序的详细信息。

但程序没有给出正确的输出。

每个程序一个,它应该打印输出说驱动程序是保险的当我将婚姻状况(ms)表示为'y'时。但它没有



[edit]Code block added - OriginalGriff[/edit]

What I have tried:

I tried giving the details of the driver.
but the program doesn't give the proper output.
A per the program, it should print the output saying the driver is insured when I give the marital status(ms) as 'y'. but it doesnt

推荐答案

从调试器开始:在行上放置一个断点

Start with the debugger: put a breakpoint on the line
if (ms == 'y')

并运行它。当您输入数据时,它会停止并让您控制。

查看 ms 中的内容 - 它是'y'?如果没有,那是什么?这与你输入的内容相比如何?

单步执行代码:仔细查看代码会发生什么,以及变量的内容 - 它应该告诉你出了什么问题。 />
(我们不能告诉你如何使用你的特定调试器,我们不知道你使用的是哪个系统。但是一个简单的google与调试器名称你的编译系统应该很容易找到你。)



但是考虑一下可怜的用户:你的app开始了,他不知道应该输入什么。相反,请尝试向他提出单独的问题:

And run it. When you enter the data it shoudl stop and let you take control.
Look at what is in ms - it is 'y'? If not, what is it? How does that compare to what you typed?
Step through the code: look closely at what happens to the code, and to the contents of your variables - it should tell you what is going wrong.
(We can't tell you how to use your particular debugger, we have no idea which system you are using. But a simple google with "debugger nameofyourcompilationsystem" should find you all that very easily.)

But think about the poor user: You app starts and he has no idea what he should type. Instead, try asking him separate questions:

char line[100];
printf("Enter the details of the driver\n");
printf("Are they married? (y/n) : ");
scanf("%s", line);
ms = line[0];
printf("Sex?              (m/f) : ");
scanf("%s", line);
sex = line[0];
printf("Age?                    : ");
scanf("%d", &age);



如果你提示它们,你也可能会发现你的问题消失了!


You may also find your problem goes away if you prompt them!


输入

With the input
y m 35



您的程序产生输出


your program produces the output

Drive is insured



在我的Lubuntu盒子上。


on my Lubuntu box.


当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于C程序重写如果否则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆