[不是问题] C编程中的逻辑问题 [英] [Not a question] logic problem in c programming
问题描述
我是C语言的初学者.我在逻辑上有问题.例如,汽车的可用性为1,但是当我插入要出租的汽车的数量大于1时,系统仍在运行,并且不输入if else语句.谢谢!
I''m beginner in C language.I have a problem with logic.For example, the availability of the car is 1,but when i insert the amount of the car i want to rent is more than 1, the system is still running and does not enter the if else statement.Thanks !
int car;
int amountcarSAGA;
int amountcarVIOS;
printf(" Which car you prefer\n\t1 PROTON SAGA %d",availabilitySAGA);
printf("\n\t2 TOYOTA VIOS\n\t");
scanf("%d", &car );
switch (car)
{
case 1 : if(availabilitySAGA<=0)
{
printf("No car available");
break;
}
else
{
printf("How many days you want to rent\n\t");
scanf("%d", &day);
printf("How many cars you want to rent\n\t");
scanf("%d",&amountcarSAGA);
if(availabilitySAGA>0)
{
calculation_carSAGA(day,amountcarSAGA);
}
else
{
printf("Amount of car exceed the availability");
}
availabilitySAGA=availabilitySAGA-amountcarSAGA;
break;
}
case 2 : printf("How many days you want to rent\n\t");
scanf("%d", &day);
printf("How many cars you want to rent\n\t");
scanf("%d",&amountcarVIOS);
calculation_carVIOS(day,amountcarVIOS);
break;
default: printf("\nEnter only one from the above");
}
推荐答案
我的朋友,
这是一个显而易见的逻辑,即使您只有3辆车,用户也可以输入100辆车.原因说明如下...您可能必须阅读:P
1.您提供的第一个if条件是:
My Friend,
This is an obvious logic that user can enter 100 cars even if you only have 3 cars. The reason is explained below... You may have to read :P
1. The first if condition as per given by you is :
if (availabilitySAGA > 0) { // implies that SAGA is avialable even if one the condition is true
// Rent the car to user
} else { // Now this condition is already checked by you previously before entring this if else condition am I right?
}
因此,这种情况是完全错误的.逻辑错误.
2.现在,逻辑应该是这样
->检查是否AvailabilitySAGA == 0'',因为您不想减价,所以可以吗?
->比较AvaialabilitySAGA> =输入的金额;这是正确的逻辑.
->根据条件的积极性做任何您想做的事情.
3.通常,在编写任何逻辑之前,通常应尝试进行纸笔模拟,即制作流程图和伪代码.由于您整夜过度调试,它将减少相当多的红眼:P
我试图以一种有趣的方式来表达它...希望它会有所帮助,尽管我已经读过您的评论,您可以自己解决问题.真的很棒. :)
致以问候
Tushar Srivastva
This condition thereby is completely wrong. The Logic is wrong.
2. Now, the logic should be this
-> Check if AvailabilitySAGA == 0 ''cause you are not going to go in minus can you?
-> Compare the AvaialabilitySAGA >= Entered amount; This is the correct logic.
-> Do whatever you want based on the positivity of the condition.
3. You generally should try to do a paper-pen simulation i.e. making flow charts and pseudo codes, before writing any logic. It will reduce considerable red eyes due to excessive debugging by you all night :P
I tried to put it in a fun way... Hope that it helped although I had read your comment already that you solved the problem yourself. It''s really good. :)
With Regards
Tushar Srivastva
这篇关于[不是问题] C编程中的逻辑问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!