C中的条件只打印1个结果,而不考虑输入
我有点不知所措。我有一种感觉,我在代码中的某个地方出错了,但似乎不能完全指出这一点。基本上,我被分配去编写一个C代码,该代码应该是模拟警察的雷达枪。用户将插入一个整数,并根据输入告知用户该输入是否超速,如果是特定的罚款 下面是我的代码C中的条件只打印1个结果,而不考虑输入,c,C,我有点不知所措。我有一种感觉,我在代码中的某个地方出错了,但似乎不能完全指出这一点。基本上,我被分配去编写一个C代码,该代码应该是模拟警察的雷达枪。用户将插入一个整数,并根据输入告知用户该输入是否超速,如果是特定的罚款 下面是我的代码 /* ***** Author: Shivan Kamal Date: 12-3-2015 Program Reason: To simulate a police speed radar by getting user to insert a number
/* *****
Author: Shivan Kamal
Date: 12-3-2015
Program Reason: To simulate a police speed radar by getting user to insert a number
and to give them an answer to whether they are speed or not, and provide a warning or a fine
where required.
*****/
#include <stdio.h>
int main()
{
/*****
Declaring values
******/
int speed;
const int LIMIT1 = 60;
const int LIMIT2 = 65;
const int LIMIT3 = 70;
const int LIMIT4 = 80;
const int LIMIT5 = 200;
const int FINE1 = 80;
const int FINE2 = 150;
const int FINE3 = 500;
/*****
Beginning of the program
*****/
printf(" If you want to check whether a certain speed issues you\n");
printf(" a warning or a fine and how much, please input a number value\n");
printf(" NOTE: the number value will be in KM/h.\n");
scanf("%d%*c" , speed);
if(speed == 0)
{
printf(" Your vehicle is not moving at '0'km/h. If your car was blocking traffic, you would be issued a fine. Please be careful.\n");
}
else
if(speed <0)
{
printf("Your vehicle is most likely in reverse. If this is the case and your vehicle disrupts traffic. You may be issued a fine. Please be careful.\n");
}
else
if(speed <= LIMIT1)
{
printf("You're within the speed limit.\n");
}
else
if(speed <= LIMIT2)
{
printf("SPEEDING - Warning! You are going over the speed limit.\n Please slow down or you may be issued a fine");
}
else
if(speed <= LIMIT3)
{
printf("SPEEDING - You have gone over the speed limit.\n Fined $FINE1.\n");
}
else
if(speed <= LIMIT4)
{
printf("SPEEDING - You have gone over the speed limit.\n Fined $FINE2.\n");
}
else
if(speed <= LIMIT5)
{
printf("SPEEDING - You have gone over the speed limit.\n Fined $FINE3.\n");
}
else
{
printf(" By getting caught going faster than any limit higher than 20km/h.\n Your fine will simply keep getting bigger.\n");
printf("Please stick to the speed limit and follow the rules.\n");
}
return(0);
}
/******
作者:Shivan Kamal
日期:2015年3月12日
程序原因:通过让用户插入一个数字来模拟警察的测速雷达
并回答他们是否超速,并给予警告或罚款
如果需要的话。
*****/
#包括
int main()
{
/*****
声明值
******/
整数速度;
常数int LIMIT1=60;
常数int LIMIT2=65;
常数int LIMIT3=70;
常数int LIMIT4=80;
常数int LIMIT5=200;
常数int FINE1=80;
常数int FINE2=150;
常数int FINE3=500;
/*****
节目开始
*****/
printf(“如果您想检查是否有某个速度问题,\n”);
printf(“警告或罚款以及金额,请输入数字值\n”);
printf(“注:数值单位为KM/h.\n”);
扫描频率(“%d%*c”,速度);
如果(速度==0)
{
printf(“您的车辆没有以“0”km/h的速度行驶。如果您的车辆阻塞交通,您将被处以罚款。请小心。\n”);
}
其他的
如果(速度改变
scanf("%d%*c" , speed);
到
当使用scanf()
接受值时,需要使用&
。一元&
返回其后面变量的地址。scanf()
将值存储在该地址中。因此,需要在scanf()
中使用&
,但不需要将&
用于printf()
(除非出于某种原因要打印该变量的地址)
您还应该将3printf()
语句更改为
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d.\n",FINE1);
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d .\n", FINE2);
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d .\n", FINE3);
否则,它不会显示罚款的价值我就把这个留在这里:哈哈,这是个愚蠢的错误。真不敢相信我错过了这么小的东西。我有很多东西要学。谢谢@Arun a.s.。在printf
中打印一个常量变量怎么样?@Shivarn,&
会在它后面返回变量的地址ndscanf()
将值存储在该地址中。但是对于printf()
Ahh,您不需要使用&
,这很有意义。谢谢Arun。
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d.\n",FINE1);
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d .\n", FINE2);
printf("SPEEDING - You have gone over the speed limit.\n Fined $%d .\n", FINE3);