用于在第二次调用时终止数组内的循环
本文关键字:数组 循环 终止 第二次 调用 用于 | 更新日期: 2023-09-27 18:27:03
我用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");
}
}
}
}
First off Console.Read()返回一个int,表示所输入内容的ascii值。如果用户输入1
,则Console.Read()
返回49。(见此ascii表)
您可以使用Console.ReadKey()
其次,您需要对循环方式进行一些修复,并要求继续。。。。
int again = Console.Read();
你的问题就在这里——Console.Read()
返回输入的第一个字符(用ASCII码表示),而不是你输入的数字。我把解决方案留给你的家庭作业。