你能算出编码错误吗? [英] Can u figure out the coding error ?
问题描述
#包括< stdio.h中>
int main(无效)
{
float dis;
浮动费用,od,odPay;
char车辆;
printf(插入车辆(V / C / T):);
scanf(%c,& vehicle);
printf(插入你走过的距离:);
scanf(%f,& dis);
if(vehicle =='V')
if(dis< = 50)
cost = 45.50 * dis;
printf(旅行费用为%.2f \ n,费用);
else
od = dis - 50;
odPay = od * 45.00;
cost =(45.50 * 50)+ odPay;
printf(旅行费用为%.2f \ n,费用);
else if(vehicle =='C')
if(dis< = 50)
cost = 55.00 * dis;
printf(旅行费用为%.2f \ n,费用);
else
od = dis - 50;
odPay = od * 56.50;
cost =(55.00 * 50)+ odPay;
printf(旅行费用为%.2f \ n,费用);
else if(vehicle =='T')
if(dis< = 50)
cost = 50.50 * dis;
printf(旅行费用为%.2f \ n,费用);
else
od = dis - 50;
odPay = od * 50.00;
cost =(50.50 * 50)+ odPay;
printf(旅行费用为%.2f \ n,费用);
我的尝试:
这是编译时显示的错误
vehicle.c:在函数'main'中:
vehicle.c:20:3:错误:'其他'没有先前'如果'
其他
^ ~~~
车辆.c:25:2:错误:'else'没有先前'if'
否则if(vehicle =='C')
^ ~~~
vehicle.c:29:17:错误:'其他'没有先前'如果'
其他
^ ~~~
vehicle.c:34:4:错误:'else'没有先前'if'
否则if(vehicle =='T')
^ ~~~
vehicle.c:38:3:错误:'else'没有先前'if'
else
是。你能吗?
这很明显,你重复同样的错误次数。
格式为如果
声明是:
if (条件)
语句其中
语句
可以是单个语句:
if (条件)
DoSomethingSimple;或复合语句:
如果(条件)
{
DoSomethingSimple;
DoSomethingElseSimple;
}
所以当你这样写:
if (vehicle == ' V')
if (dis< = 50 )
cost = 45 。 50 * dis;
printf( 旅行费用是%.2f \ n,费用);
else编译器无法理解你想要做什么。
如果我缩进它,它更明显:
如果(vehicle == ' V')
if (dis< = < span class =code-digit> 50 ) // 这是前一个if的简单陈述
cost = 45 。 50 * dis; // 这是第二个if
printf( 旅行费用是%.2f \ n,费用); // 这不在ifs
else // 这与此无关。
总是使用复合语句,特别是在你开始时 - 它使它更清晰,更容易修改。
if (vehicle == ' V')
{
if (dis< = 50 )
{
cost = 45 。 50 * dis;
printf( 旅行费用是%.2f \ n,费用);
}
else
{
...并缩进代码!
问题是你没有在条件表达式中包含多语句子句的大括号。
换句话说 - 你应该使用这种格式:
if (表达式)
{
// 陈述......(一个或多个,无所谓)
}
else if (anotherExpression)
{
// 陈述......(一个或多个,无所谓)
}
else
{
// 陈述......(一个或多个,无所谓)
}
//我想我的抽奖速度较慢。
你需要学习大括号的用法{}
在C / C ++ / C#源代码中。然后你会明白是什么问题。
这是你在C 101班的第一天学到的东西。检查你的笔记。
#include<stdio.h> int main(void) { float dis; float cost, od, odPay; char vehicle; printf("Insert ur vehicle ( V / C / T ) : "); scanf("%c", &vehicle); printf("Insert distance that u travelled : "); scanf("%f", &dis); if (vehicle == 'V') if (dis <= 50) cost = 45.50 * dis; printf("Charge for travel is %.2f \n", cost); else od = dis - 50; odPay = od * 45.00 ; cost =( 45.50 * 50 ) + odPay; printf("Charge for travel is %.2f \n", cost); else if (vehicle == 'C') if (dis <= 50) cost = 55.00 * dis; printf("Charge for travel is %.2f \n", cost); else od = dis - 50; odPay = od * 56.50 ; cost =( 55.00 * 50 ) + odPay; printf("Charge for travel is %.2f \n", cost); else if (vehicle == 'T') if (dis <= 50) cost = 50.50 * dis; printf("Charge for travel is %.2f\n", cost); else od = dis - 50; odPay = od * 50.00 ; cost =( 50.50 * 50 ) + odPay; printf("Charge for travel is %.2f \n", cost);
What I have tried:
This is the error which is showing when compiling
vehicle.c: In function ‘main’:
vehicle.c:20:3: error: ‘else’ without a previous ‘if’
else
^~~~
vehicle.c:25:2: error: ‘else’ without a previous ‘if’
else if (vehicle == 'C')
^~~~
vehicle.c:29:17: error: ‘else’ without a previous ‘if’
else
^~~~
vehicle.c:34:4: error: ‘else’ without a previous ‘if’
else if (vehicle == 'T')
^~~~
vehicle.c:38:3: error: ‘else’ without a previous ‘if’
else
Yes. Can you?
It's pretty obvious, and you are repeating the same mistake loads of times.
The format of anif
statement is:
if(condition) statement
Where
statement
can be a single statement:
if (condition) DoSomethingSimple;
or a Compound statement:
if (condition) { DoSomethingSimple; DoSomethingElseSimple; }
So when you write this:
if (vehicle == 'V') if (dis <= 50) cost = 45.50 * dis; printf("Charge for travel is %.2f \n", cost); elsethe compiler can't understand what you are trying to do.
If I indent it, it's a lot more obvious:
if (vehicle == 'V') if (dis <= 50) // This is a simple statement for the previous if cost = 45.50 * dis; // This is a simple statement for the second if printf("Charge for travel is %.2f \n", cost); // This is outside both ifs else // This has nothing to match with.
Always use compound statements, especially when you are starting out - it makes it a lot clearer and easier to modify.
if (vehicle == 'V') { if (dis <= 50) { cost = 45.50 * dis; printf("Charge for travel is %.2f \n", cost); } else { ...And indent your code!
The problem is you have not included braces for multi-statement clauses in your conditional expressions.
In other words - you should use this type of format :
if( expression ) { // statements ... (one or many, doesn't matter) } else if( anotherExpression ) { // statements ... (one or many, doesn't matter) } else { // statements ... (one or many, doesn't matter) }
// I guess I was slower on the draw.
You need to learn the usage of braces{}
in C/C++/C# source code. Then you will understand what is the problem.
This is something you learn on first day of C 101 classes. Check you notes.
这篇关于你能算出编码错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!