我的程序没有输入else语句。当我从5键入不同的数字时。只打印if语句我该如何解决这个问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Question2
{
class Program
{
static void Main(string[] args)
{
String ID = "5";
Console.WriteLine("Enter Student ID");
Console.ReadLine();
string answer = "TFTTFTFFTF";
ID = "TTFFTFTTFT";
int count = 0;
for (int i = 0; i < answer.Length; i++)
{
if (answer[i] == ID[i])
{
count++;
}
}
if (ID == "5")
{
Console.WriteLine("This student mark is");
Console.WriteLine(count);
Console.ReadLine();
}
else
{
Console.WriteLine("error no student with this ID");
Console.ReadLine();
}
}
}
}
答案 0 :(得分:0)
您正在更改ID
变量
ID = "TTFFTFTTFT";
如果您想从用户那里收到学生ID,您应该保存它。 将一个变量用于StudentId,将另一个变量用于他填写的答案。
Console.WriteLine("Enter Student ID");
String ID = Console.ReadLine();
string answer = "TFTTFTFFTF";
string studentAnswers = "TTFFTFTTFT";
int count = 0;
for (int i = 0; i < answer.Length; i++)
{
if (answer[i] == studentAnswers[i])
{
count++;
}
}
if (ID == "5")
{
Console.WriteLine("This student mark is");
Console.WriteLine(count);
}
else
{
Console.WriteLine("error no student with this ID");
}
Console.ReadLine();
重要说明:调试器是必须使用工具,使用它!