我已经在C#中进行了编码,但在控制台应用程序中没有多少(教师让我们在其中进行分配)
我遇到一个问题,我的静态方法在第一次调用时会正常工作(每个问题都被提出),但第二次通过控制台关闭。我需要这个函数执行10次,不知道为什么它不会。这是我拥有的,并提前感谢!:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab2
{
class Program
{
//Create the arrays
static string[] questions = new string[5]; //For questions
static int[] tableHeader = new int[10]; //Table Header
static int[,] responses = new int[5, 10]; //For answers
//Int for the number of times the questions have been asked
static int quizCount = 0;
static int answer;
static bool isGoing = true;
static void Main(string[] args)
{
//Set the questions in an array
questions[0] = "On a scale of 1-10, how do you feel about the drinking age in Wisconsin?";
questions[1] = "On a scale of 1-10, how often do you drink a week?";
questions[2] = "On a scale of 1-10, how important is this class?";
questions[3] = "On a scale of 1-10, how would you rate this campus?";
questions[4] = "On a scale of 1-10, how would you rate this command prompt?";
while(isGoing)
Questions();
}
static void Questions()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(questions[i]);
answer = Convert.ToInt16(Console.ReadLine());
responses[i, quizCount] = answer;
}
if (quizCount < 10)
{
Console.WriteLine("Enter more data? (1=yes, 0=no)");
int again = Console.Read();
if (again != 1)
Environment.Exit(0);
}
else
isGoing = false;
DisplayResults();
}
static void DisplayResults()
{
Console.WriteLine(tableHeader);
for (int i = 0; i < 5; i++)
{
for (int x = 0; x < 10; x++)
{
Console.Write(responses[i, x]);
}
Console.Write("\n");
}
}
}
}
答案 0 :(得分:0)
首先关闭Console.Read()返回一个int,表示输入内容的ascii值。如果用户输入1
,则Console.Read()
会返回49.(请参阅此ascii table)
您可以使用Console.ReadKey()
其次,你需要对循环方式进行一些修复并要求继续....
答案 1 :(得分:0)
int again = Console.Read();
您的问题就在这里 - Console.Read()
会返回输入的第一个字符(由其ASCII代码表示),而不是您输入的数字。我会为您的作业留下解决方案。< / p>