不断获取“类型不能从void隐式转换为字符串”

时间:2019-01-28 09:50:43

标签: c#

所以我试图写一个大学程序。预期目的是使学生姓氏和考试成绩,然后将其写入文本文件。我不断收到此错误。我不知道该怎么办。代码还没有完成,但是我仍然需要为其编写其他函数,因此留有空白。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Assignment2
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "StudentTestScores.txt";
            StreamWriter SW = File.CreateText(path);

            string sUsrOpt = "";
            byte bNumStudents = 0;

            Console.Write("Enter Amount Of Students In The Class (Max 20): ");
            byte.TryParse(Console.ReadLine(), out bNumStudents);

            string[] sSurnames = new string[bNumStudents];
            float[] fScores = new float[bNumStudents];



            mainMenu(sUsrOpt, sSurnames, fScores, ref bNumStudents, SW);

            Console.WriteLine("Press Any Key To Open File...");
            string temp = "";
            if (File.Exists(path))
            {
                using (StreamWriter sr = File.AppendText(path))
                    for (int i = 0; i < bNumStudents; i++)
                {
                    foreach (var line in (path))
                    {
                        Console.WriteLine("");

                        temp = sr.WriteLine();
                        Console.Write("Surname: {0}", temp);
                        Console.WriteLine("");

                        temp = sr.WriteLine();
                        Console.Write("Score");
                        Console.WriteLine("");
                    }
                    Console.WriteLine("press any key to continue");
                    Console.ReadKey();
                    Console.Clear();
                }

            }




            Console.ReadKey();

            System.Diagnostics.Process.Start(path);

            SW.Close();
        }


        //The menu

        private static void mainMenu(string sUsrOpt, string[] sSurnames, float[] fScores, ref byte bNumStudents, StreamWriter SW)
        {
            Console.WriteLine("\nOption A - Open and display a test\nOption B - Enter and save a test\nOption C - Help\nOption D - Exit");

            Console.Write("Choose An Option: ");
            sUsrOpt = Console.ReadLine();


            switch (sUsrOpt.ToUpper())
            {
                case "A":
                    Opentests(sSurnames, SW);
                    break;
                case "B":
                    EnterAndSaveaTest(fScores, sSurnames, ref bNumStudents);
                    break;
                case "C":
                    Help(fScores, sSurnames, SW);
                    break;
                case "D":
                    Exit();
                    break;

                default:
                    break;

            }
        }
        //Enter and save a test
        private static void EnterAndSaveaTest(float[] fScores, string[] sSurnames, ref byte bNumStudents)
        {
            if (bNumStudents > 20)
            {
                Console.WriteLine("Too High, try again");

            }

            for (int i = 0; i < bNumStudents; i++)
            {
                Console.Write("Enter Surname {0}: ", i + 1);
                sSurnames[i] = Console.ReadLine();

                Console.Write("Enter Score For {0}: ", sSurnames[i]);
                float.TryParse(Console.ReadLine(), out fScores[i]);
            }

            for (int y = 0; y < bNumStudents; y++)
            {
                Console.WriteLine(fScores[y]);
            }

        }

        //Open a test
        private static void Opentests(string[] sSurnames, StreamWriter SW)
        {
            Console.WriteLine("Please input a previously entered surname");


        }


        //Gives help to the user
        private static void Help(float[] fScores, string[] sSurnames, StreamWriter SW)
        {


        }
        //Ends the program
        private static void Exit()
        {
            Console.ReadLine();

        }
    }
}

1 个答案:

答案 0 :(得分:2)

path是一个字符串,您需要打开文件(我将使用StreamReader) 然后从StreamReader中读取每一行

>>> dis(f)
  2           0 BUILD_LIST               0
              3 STORE_FAST               0 (a)

  3           6 LOAD_CONST               1 (1)
              9 LOAD_CONST               2 (2)
             12 BUILD_LIST               2
             15 LOAD_FAST                0 (a)
             18 STORE_SLICE+0

  4          19 LOAD_FAST                0 (a)
             22 PRINT_ITEM
             23 PRINT_NEWLINE
             24 LOAD_CONST               0 (None)
             27 RETURN_VALUE

那么看来您正在尝试向作家阅读

foreach (var line in (path))

WriteLine是一个无效的返回值,您不能赋值不返回任何值。