我不知道为什么它没有执行 [英] I don't know why it is not executing

查看:90
本文介绍了我不知道为什么它没有执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天,兔子和他的朋友在森林里玩捉迷藏。特威蒂继续躲起来。她在一个山洞里发现了一个包含棋盘(8x8)游戏的废弃袋子。她很兴奋,开始玩游戏只是为了意识到它是神奇的。她有三次掷骰子的机会。每个回合的结果将导致一个新的环境,一些将是危险的。如果滚动骰子的结果是3的倍数,那么她将陷入危险阶段。编写一个程序,以确定Tweety是否完成游戏并安全返回。



如果滚动骰子的结果是0或小于那个你应该告诉tweety为无效转弯



测试案例

输入1

输入值转1

10

输入转2的价值

5

输入转3的价值

2

输出1

Tweety是安全的


输入2

输入价值转1

3

输入转2价值

9

输入转折价值3

12

产出2

Tweety有危险



输入3

输入转1的价值

0

输入转2的价值

9

输入值3转

12

输出3

转弯无效





我尝试了什么:



  #include   <   stdio。 h  >  
int main()
{
int a,b,c;
printf( 输入turn 1 \ n的值);
scanf( %d,& a);
printf( 输入转为2的数字<\\ n);
scanf( %d,& b);
printf( 输入转折值3'\\ n);
scanf( %d,& c);
if (a> 0& b> 0&& c> 0)
{
if (a%3 == 0 && b%3 == 0 && c%3 == 0
{
printf( Tweety处于危险之中);
}
else
{
printf( 特威蒂是安全的);
}
}
其他 if (a == 0 || b == 0 || c == 0
{
printf( 无效转弯);
}
else
{
printf( 无效转);

}
}

解决方案

引用:

我不知道为什么它没有执行



使用调试器找出程序的功能。



有一个工具可以让你看到代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

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

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



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



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

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

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。


因为你在第3轮中的输入是转1是0并且它会遇到你的代码:



 其他  if (a ==  0  || b ==  0  || c == < span class =code-digit> 0 ) //  这里你进入 
{
printf( 无效转弯);
}
else
{
printf( 无效转); // 其他???

}





你的其他人很奇怪。输出应该是不同的。如果做出否定输入,则会出现问题。



为什么不使用调试器并单步执行代码。


One day, Bunny and his friends were playing hide and seek in the forest. Tweety went along to hide. She finds an abandoned bag containing a board(8x8) game in a cave. She gets excited and starts playing the game only to realizes that it's magical. She has three chances to roll the dice. Each turn's outcome will lead into a new surrounding and some would be dangerous. If the outcome of rolling dices is in multiples of 3 then she will be caught in a dangerous phase. Write a program to find whether Tweety finishes the game and returns safely or not.

If the outcome of rolling dices is either 0 or less than that you should tell tweety as "Invalid Turn"

Test Case

Input 1

Enter value of turn 1

10

Enter value of turn 2

5

Enter value of turn 3

2

Output 1

Tweety is safe


Input 2

Enter value of turn 1

3

Enter value of turn 2

9

Enter value of turn 3

12

Output 2

Tweety is in danger



Input 3

Enter value of turn 1

0

Enter value of turn 2

9

Enter value of turn 3

12

Output 3

Invalid Turn



What I have tried:

#include<stdio.h>
int main()
{
    int a,b,c;
    printf("Enter value of turn 1\n");
    scanf("%d",&a);
    printf("Enter value of turn 2\n");
    scanf("%d",&b);
    printf("Enter value of turn 3\n");
    scanf("%d",&c);
    if(a>0 && b>0 && c>0)
    {
        if(a%3==0 && b%3==0 && c%3==0)
        {
            printf("Tweety is in danger");
        }
        else
        {
            printf("Tweety is safe");
        }
    }
    else if(a==0 || b==0 || c==0 )
    {
        printf("Invalid Turn");
    }
    else
    {
        printf("Invalid Turn");
        
    }
}

解决方案

Quote:

I don't know why it is not executing


Use the debugger to find out what the program does.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
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.

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.


Because your input in round 3 is in "turn 1" is 0 and it will run into your code:

else if(a==0 || b==0 || c==0 ) //here you go in
    {
        printf("Invalid Turn");
    }
    else
    {
        printf("Invalid Turn"); //other else ???
        
    }



your other else is strange. The output should be different. The case woul hit if negatice input is done.

Why dont you use a debugger and stepping through your code.


这篇关于我不知道为什么它没有执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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