黑马程序员技术交流社区
标题:
if else 出问题了 帮忙看看
[打印本页]
作者:
小周学诚
时间:
2013-7-26 16:12
标题:
if else 出问题了 帮忙看看
Console.WriteLine("输入结业成绩");
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90)
{
Console.WriteLine("a");
}
if (input3 >= 80 && input3 < 90)
{
Console.WriteLine("b");
}
if (input3 >= 70 && input3 < 80)
{
Console.WriteLine("c");
}
if (input3 >= 60 && input3 < 70)
{
Console.WriteLine("d");
}
else
{
Console.WriteLine("e");
}
Console.ReadKey();
我输了个90 结果是a
e
怎么回事啊
作者:
zhangcheng5468
时间:
2013-7-26 17:00
这是必然的啊,这几组if之间是并列关系,最后一个if和else是一组,当执行到最后一个if是不满足所以就执行了else的代码,也就输出了‘e’,楼主可以在前面几个if前加上else
if (input3 >= 90)
{
Console.WriteLine("a");
}
else if (input3 >= 80 && input3 < 90)
{
Console.WriteLine("b");
}
else if (input3 >= 70 && input3 < 80)
{
Console.WriteLine("c");
}
else if (input3 >= 60 && input3 < 70)
{
Console.WriteLine("d");
}
else
{
Console.WriteLine("e");
}
复制代码
作者:
小天
时间:
2013-7-26 20:10
本帖最后由 小天 于 2013-7-30 23:47 编辑
出错原因:当进行最后一个if判断的时候,条件为假,执行else中的语句所以输出了一个a,又输出一个e
if-else if和switch都可以处理分支结构
区别:if-else if可以处理范围,switch只能处理整数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入结业成绩");
try{
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 > 100 || input3 < 0)
{
Console.WriteLine("输入的数据有误(只能输入0-100之间的数),程序结束");
}
else if (input3 >= 90)
{
Console.WriteLine("a");
}
else if (input3 >= 80)
{
Console.WriteLine("b");
}
else if (input3 >= 70)
{
Console.WriteLine("c");
}
else if (input3 >= 60)
{
Console.WriteLine("d");
}
else
{
Console.WriteLine("e");
}
}
catch{
Console.WriteLine("数据有误,程序结束");
}
Console.ReadKey();
}
}
}
作者:
王云峰
时间:
2013-7-26 20:14
Console.WriteLine("输入结业成绩");
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90)
{
Console.WriteLine("a");
}
else if
(input3 >= 80 && input3 < 90)
{
Console.WriteLine("b");
}
else if
(input3 >= 70 && input3 < 80)
{
Console.WriteLine("c");
}
else if
(input3 >= 60 && input3 < 70)
{
Console.WriteLine("d");
}
else
{
Console.WriteLine("e");
}
Console.ReadKey();
养成这样用的习惯就好啦,注意红色的else if
作者:
mzh901024
时间:
2013-7-26 20:24
楼主前面的几位都说的对,这种情况用if else if判断,当然switch ~case判断更好,我用的手机,电脑没法上网,所以代码就不好写,我建议用switch~case
作者:
ww448483689
时间:
2013-7-26 22:53
else 与它最近的if 配对 ,它前一个if里面的条件不能满足,所以就要输出else里面的语句。。。。。。
作者:
brucel50
时间:
2013-7-27 02:22
Console.WriteLine("输入结业成绩"); //你在这里输入90 ↓
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90) //符合这里的条件,所以进入这里的大括号,并执行里面的代码:输出a
{
Console.WriteLine("a");
}
if (input3 >= 80 && input3 < 90) //不符合这里的条件,跳过大括号。
{
Console.WriteLine("b");
}
if (input3 >= 70 && input3 < 80) //不符合这里的条件,跳过大括号。
{
Console.WriteLine("c");
}
if (input3 >= 60 && input3 < 70) //不符合这里的条件,跳过大括号。 并进入和他最近的一个else,直接执行里面的代码。输出e
{
Console.WriteLine("d");
}
else
{
Console.WriteLine("e");
}
Console.ReadKey();
作者:
xt654005440
时间:
2013-7-27 09:57
本帖最后由 xt654005440 于 2013-7-27 10:42 编辑
对于if else语句其执行方式为:先判断if条件,若条件符合则执行紧跟if的代码,若
不符合则执行else,或者后续代码
,如下
Console.WriteLine("输入结业成绩");
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90)
{
Console.WriteLine("a");
}
else Console.WriteLine("e");
输入
大于等于90的数
结果为
a
,而其他
小于90的数
结果为
e
。
现在根据楼主所想要达到的代码效果就是已if语句内的判定条件对给出的不同分数进行对应的结果输出:
那么正确的代码表示可以如下:
Console.WriteLine("输入结业成绩");
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90)
{
Console.WriteLine("a");
}
else if
(input3 >= 80 && input3 < 90)
{
Console.WriteLine("b");
}
else if
(……)
{
……
}
else
{
Console.WriteLine("e");
}
Console.ReadKey();
这样的话就可以达到楼主预期的结果,楼主可以去试试,希望对你有所帮助。
作者:
leayon
时间:
2013-7-27 10:51
mzh901024 发表于 2013-7-26 20:24
楼主前面的几位都说的对,这种情况用if else if判断,当然switch ~case判断更好,我用的手机,电脑没法上 ...
黑马论坛还有手机版的,哥表示没用过。。。:'(
作者:
mzh901024
时间:
2013-7-27 18:19
手机也不是不能用,只是不太方便
作者:
高文咪
时间:
2013-7-27 20:10
你第一个判断是 if (input3 >= 90){},你输个90他肯定会走这个判断的,你的IF条件里也包含有90这个值,就是当这个数是90或大于90都会进这个方法;
如果你的if语句是if(ipnut3>90){},那你输入90肯定不会进这个方法喽,就会进入你下面的if判断,直接条件匹配;
作者:
马晓凤
时间:
2013-7-28 11:42
Console.WriteLine("输入结业成绩");
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90)
{
Console.WriteLine("a");
}
if (input3 >= 80 && input3 < 90)
{
Console.WriteLine("b");
}
if (input3 >= 70 && input3 < 80)
{
Console.WriteLine("c");
}
if (input3 >= 60 && input3 < 70)
{
Console.WriteLine("d");
}
else
{
Console.WriteLine("e");
}
Console.ReadKey();
if else 是一对的,一开始程序执行的时候,90满足要求输出a,当程序执行到最后的 if else ,if 条件不满足,所以程序到else里面,建议 用 if else if else 这样的逻辑关系判断,
或者最后程序判断改为
if(input3 <60)
{
Console.WriteLine("e");
}
可以实现要求
作者:
独世
时间:
2013-7-29 16:21
else只与离它最近的if配对,你更上面的if与else完全没有关系,按照你的代码,最后的else条件是input3>=70&&input3<60,所以你输个90会跑出个a又跑出个e。
如果你想else与上面的if有关系就需要使用if else-if结构了。
下面的第一段是改进你的代码,第二段是if else-if结构。
Console.WriteLine("输入结业成绩");
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90)
{
Console.WriteLine("a");
}
if (input3 >= 80 && input3 < 90)
{
Console.WriteLine("b");
}
if (input3 >= 70 && input3 < 80)
{
Console.WriteLine("c");
}
if (input3 >= 60 && input3 < 70)
{
Console.WriteLine("d");
}
if (input3 < 60)
{
Console.WriteLine("e");
}
Console.ReadKey();
Console.WriteLine("输入结业成绩");
int input3 = Convert.ToInt32(Console.ReadLine());
if (input3 >= 90)
{
Console.WriteLine("a");
}
else if (input3 >= 80)
{
Console.WriteLine("b");
}
else if (input3 >= 70)
{
Console.WriteLine("c");
}
else if (input3 >= 60)
{
Console.WriteLine("d");
}
else
{
Console.WriteLine("e");
}
Console.ReadKey();
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2