C#一个简单的小题

来源:百度知道 编辑:UC知道 时间:2024/05/27 07:28:29
using System;
using System.Collections.Generic;
using System.Text;

namespace 一阶段课后作业
{
class Program
{
static void Main(string[] args)
{
char[] a={'a','i','o','u'};
char b;
int c=0;
Console.WriteLine("请输入一个字符");
b=char.Parse(Console.ReadLine());
for (int h = 0; h < a.Length;h++)
{
if (b == a[h])
{
Console.WriteLine("您输入的是元音字符");
break;
}
if (c == 0)
{

Console.WriteLine("这不是元音字符");

}
}
}
}
}

判断是不是元音,出是出来了,但是循环到后面会显示出不是元音,只

if (b == a[h])
{
Console.WriteLine("您输入的是元音字符");
break;
}
if (c == 0)
{

Console.WriteLine("这不是元音字符");

}

上面这一段改为:
if (b == a[h])
{
Console.WriteLine("您输入的是元音字符");
c=1;
}
在for循环外面写
if(c==0)
{
Console.WriteLine("这不是元音字符");

}

解释:
输入一个字符,进入循环里判断,是元音,就打印您输入的是元音字符,且c为1
不是元音时,c值无改变,c=0,if判断成立,打印这不是元音字符

for (int h = 0; h < a.Length;h++)
{
if (b == a[h])
{
Console.WriteLine("您输入的是元音字符");
c=1;
break;
}
else
{
continue;
}
}
if(c>0)
{
Console.WriteLine("这不是元音字符");
}

因为你的c永远等于0
所以只要不输入'a'每次循环都会先出现:"这不是元音字符"
if (c == 0)
{

Console.WriteLine("这不是元音字符");

}
这个if其实是没有作用的

你的break用的有问题撒!因为break只是跳出本循环!但是C一