冗余问题请删除 [英] Redundant question please delete

查看:124
本文介绍了冗余问题请删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *使用switch语句的程序

* /

#include< stdio.h>



int main()

{

char oper;



printf(输入数学运算符\ n);



oper = getchar();



//使用switch语句

开关(操作)

{

case'+':

{

printf(你输了加号\ n);

休息;

}



case ' - ':

{

printf(你输了一个减号\ n);

休息;

}



case'*':

{

printf(你输了一个乘法\ n);

休息;



}



case'/':

{

printf(你输入了分数\ n);

休息;

}



默认值:

{

printf(无效运算符输入\ n) ;

休息;

} //结束默认



} //结束开关

返回0;





} //结束主要





我尝试了什么:



甚至不知道从哪里开始。我只是很困惑。

/* program that uses the switch statement
*/
#include <stdio.h>

int main()
{
char oper;

printf("enter a mathematical operator \n");

oper=getchar();

//use the switch statement
switch(oper)
{
case'+':
{
printf("you entered a plus \n");
break;
}

case'-':
{
printf("you entered a minus \n");
break;
}

case'*':
{
printf("you entered a multiply \n");
break;

}

case'/':
{
printf("you entered a divide \n");
break;
}

default:
{
printf("invalid operator entered \n");
break;
} //end default

} //end switch
return 0;


} //end main


What I have tried:

Dont even know where to start. I'm just so confused.

推荐答案

你注意到的错误很简单:你忘记了这一行末尾的分号:

The error you have noticed is simple: You forgot the semicolon at the end of this line:
char goAgain='y'

添加它将解决编译问题:

Adding it will fix the compilation problem:

char goAgain='y';



但是......那么你有更多的问题。

1)你没有看到用户的选择。

2)你打破了开关无论如何用户选择y或n。

3)您的条件不会检查用户PIN是否为1234,它会分配。 =是赋值,==是比较。

4)你应该在循环终止条件中使用你的goAgain变量。


But...then you have more problems.
1) You don't read the choice from the user.
2) You break out of the switch regardless of the user selection of "y" or "n".
3) Your condition doesn't check the user PIN is 1234, it assigns it. "=" is assignment, "==" is comparison.
4) You should be using your "goAgain" variable in the loop termination condition.


你在初始化值之前使用了变量 choice ,你也忘了编写默认的情况。



可能的解决方案是在switch语句之前接受用户输入。



类似于 -

You have used the variable choice before initializing the value and you have also forgotten to write the default case.

The possible solution could be to accept user input just before the switch statement.

Something like-
//--your other previous stuffs
printf("4. Exit \n");
scanf("%d",choice); //accept choice from user

    switch(choice)
    {
       //---rest of the logic





希望,它有帮助:)



Hope, it helps :)


这篇关于冗余问题请删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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