c#中的数组和整数出了问题

本文关键字:问题 整数 数组 | 更新日期: 2023-09-27 18:11:41

我真的很抱歉,因为我没有把代码写成英文,因为我没有写所有的代码,所以你可以理解。

所以我编辑并删除了我写的帖子,这里是所有的代码和一些注释,以帮助你理解它。问题是标有***,如果我知道这可能是非常愚蠢的问题,但我找不到答案。

HERE IT IS: '//创建一个自动生成值为100的数组int arrayNumber = 0;List array = new List();

        while(true){
            if (array.Count == 100){
                break;
            } else{
            array.Add(arrayNumber);
                arrayNumber++;
            } 
        }

        // Changes the title of program
        Console.Title = "Calculator of time spent in school.";
        // Prints welcome message
        Console.WriteLine("Hi user.!");
        // Sleeps for a while
        Thread.Sleep(1500);
        // Writes the next message
        Console.WriteLine("'nWelcome to Calculator of time spent in school.");
        Thread.Sleep(2500);
    DoAgain:
        Console.Clear();
        // Asks user when school starts in hours
        Console.Write("Write the time when school starts in hours: ");
        Thread.Sleep(500);
        // Creates new variable type of INT and checks is it valid
        string TIMEschoolStarts = Convert.ToString(Console.ReadLine());
        int timeSchoolStarts;
        if (Convert.ToInt32(TIMEschoolStarts) < 6)
        {
            timeSchoolStarts = Convert.ToInt32(TIMEschoolStarts);
            do
            {
                timeSchoolStarts++;
            } while (timeSchoolStarts < 6);
        }
        else if (Convert.ToInt32(TIMEschoolStarts) > 18)
        {
            timeSchoolStarts = Convert.ToInt32(TIMEschoolStarts);
            do
            {
                timeSchoolStarts--;
            } while (timeSchoolStarts > 18);
        }
        else if (array.Contains(Convert.ToInt32(TIMEschoolStarts)) == false)
        {
            goto DoAgain;
        }
        // Creates new variable for the Time in Minutes when school starts
        Console.Write("Write the time when school starts in minutes: ");
        Thread.Sleep(500);
        string TimesInMinutes = Convert.ToString(Console.ReadLine());
        int minutesTime;
        // Checks is variable written and edit it
        if (TimesInMinutes == "")
        {
            minutesTime = 0;
        }
        else {
            minutesTime = Convert.ToInt32(TimesInMinutes);
        }
        Console.Clear();
        Console.Write("Write how much classes you have: ");
        int numberOfClasses = Convert.ToInt32(Console.ReadLine());
        Console.Clear();
        int timeSpentHours, timeSpentMinutes, timeSpent;
        // Gives new values of time SPENT in school
        timeSpent = numberOfClasses * 45 + (numberOfClasses - 2) * 5 + 20;
        timeSpentHours = timeSpent / 60;
        timeSpentMinutes=timeSpent % 60;
        // Gives new values of time to the variables
        // ****            
        timeSchoolStarts += timeSpentHours;
        // ****
        minutesTime += timeSpentMinutes;
        // Prints the value when school is ending to the user
        Console.WriteLine("School is ending in {0} hours, {1} minutes.'nTotal time spent in school is {2} hours, {3} minutes.", timeSchoolStarts, minutesTime, timeSpentHours, timeSpentMinutes);
        Thread.Sleep(4000);
        Console.Write("'Type RUN to start program again: ");
        string again = Convert.ToString(Console.ReadLine());
        if (again.ToUpper()=="RUN") {
            goto DoAgain;`

PS:这是一个计算在校时间的程序。如果你找到更好的方法来创建它,请发布。

来自塞尔维亚的问候!

c#中的数组和整数出了问题

首先:ReadLine()的返回值是字符串,不需要转换。

第二:do-while循环也是不必要的。

第三:goto在干净的代码中是不允许的!

第四个:"niz"是数组吗?你的目标是什么?