C# 即使没有实现循环,如何修复循环问题?

C# 即使没有实现循环,如何修复循环问题?,c#,C#,好吧,我想弄明白为什么我会有循环问题。方法GetNewDvdInfo()的目的是返回一个具有5个属性的新dvd类,并将在CreateDvd()方法中传递给DvdController.cs,然后将显示所有dvd和用户添加的dvd。问题是GetNewDvdInfo()方法正在重复自身,但当我返回null时,它没有循环 dvdvview.cs using System; using System.Collections.Generic; using System.Linq; using System.

好吧,我想弄明白为什么我会有循环问题。方法
GetNewDvdInfo()
的目的是返回一个具有5个属性的新dvd类,并将在
CreateDvd()
方法中传递给
DvdController.cs
,然后将显示所有dvd和用户添加的dvd。问题是
GetNewDvdInfo()
方法正在重复自身,但当我返回null时,它没有循环

dvdvview.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DvdManager.Models;

/* 
    GetMenuChoice() : int
    GetNewDvdInfo(): Dvd
    DisplayDvd(Dvd dvd) : void
    EditDvdInfo(Dvd dvd) : Dvd
    SearchDvd() : int
    ConfirmRemoveDvd(Dvd) : boolean
*/

namespace DvdManager.View
{
    public class DvdView
    {
        public int GetMenuChoice()
        {
            string input;
            int choice;

            Console.Clear();
            Console.WriteLine("Press 1 to display movies");
            Console.WriteLine("Press 2 to add movie");
            input = Console.ReadLine();

            if (int.TryParse(input, out choice))
            {
                switch (choice)
                {
                    case 1:
                        break;
                    case 2:
                        break;
                    default:
                        Console.WriteLine("Invalid input");
                        break;
                }

            }
            return choice;


        }

        public Dvd GetNewDvdInfo() //looping here
        {
            string inputReleaseYear;
            string inputRating;

            int id = 4;
            string readTitle;            
            int readReleaseYear;
            string readDirector;            
            float readRating;

            Console.WriteLine("What is the Title of the DVD?");
            readTitle = Console.ReadLine();

            Console.WriteLine("What is the Release Year of the DVD?");
            inputReleaseYear = Console.ReadLine();
            int.TryParse(inputReleaseYear, out readReleaseYear);

            Console.WriteLine("Who is the Director of the DVD?");
            readDirector = Console.ReadLine();

            Console.WriteLine("What is the star rating of the DVD?");
            inputRating = Console.ReadLine();
            float.TryParse(inputRating, out readRating);


            var dvd = new Dvd(id, readTitle, readReleaseYear, readDirector, readRating);
            Dvd newDvd = GetNewDvdInfo();
            return dvd;
        }

        public void DisplayDvd(Dvd dvd)
        {

        }

        public Dvd EditDvdInfo(Dvd dvd)
        {
            return null;
        }

        public int SearchDvd()
        {
            return 0;
        }

        public Boolean ConfirmRemoveDvd(Dvd dvd)
        {
            return false;
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DvdManager.Models;
using DvdManager.Data;
using DvdManager.View;

/* 
    Run() : void
    Private CreateDvd(): void
    Private DisplayDvds(): void
    Private SearchDvds(): void
    Private EditDvd() : void
    Private RemoveDvd() : void
*/

namespace DvdManager.Controllers
{
    public class DvdController
    {
        public DVDList _dvds = new DVDList();        


        public void Run()
        {
            Console.WriteLine("Welcome To Dvd Manager");
            DvdView view = new DvdView();

            var pass = view.GetMenuChoice();

            if (pass == 1)
            {
                CreateDvd();
            } 
            else if (pass == 2)
            {
                view.GetNewDvdInfo();
                CreateDvd();
            }
            else
                Console.WriteLine("Invalid.");            
        }

        private void CreateDvd() //Create
        {
            var myView = new DvdView();
            var dvdInfos = myView.GetNewDvdInfo();

            List<Dvd> Dvds = _dvds.GetList();

            Dvds.Add(new Dvd(0, "Batman", 2010, "Bruce", 4));
            Dvds.Add(new Dvd(1, "Superman", 2009, "John", 4));
            Dvds.Add(new Dvd(2, "Wonderwoman", 2012, "Omar", 4));
            Dvds.Add(dvdInfos);


            DisplayDvds();
        }

        private void DisplayDvds() //Read List<Dvd> dvds
        {
            List<Dvd> Dvds = _dvds.GetList();

            for (int i = 0; i < Dvds.Count; i++)
            {                
                Console.WriteLine(Dvds[i]);
            }

            RemoveDvd();
        }

        private void SearchDvds() //List
        {

        }

        private void EditDvd(int id, Dvd dvd) //Update
        {

        }

        private void RemoveDvd() //Delete int id
        {
            List<Dvd> Dvds = _dvds.GetList();

            //Dvds.RemoveAt(Dvds[1]);
            Dvds.Remove(Dvds.Single(x => x.Id == 1));

            Console.WriteLine("Removed movie from list:");

            for (int i = 0; i < Dvds.Count; i++)
            {
                Console.WriteLine(Dvds[i]);
            }
        }
    }
}
DvdController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DvdManager.Models;

/* 
    GetMenuChoice() : int
    GetNewDvdInfo(): Dvd
    DisplayDvd(Dvd dvd) : void
    EditDvdInfo(Dvd dvd) : Dvd
    SearchDvd() : int
    ConfirmRemoveDvd(Dvd) : boolean
*/

namespace DvdManager.View
{
    public class DvdView
    {
        public int GetMenuChoice()
        {
            string input;
            int choice;

            Console.Clear();
            Console.WriteLine("Press 1 to display movies");
            Console.WriteLine("Press 2 to add movie");
            input = Console.ReadLine();

            if (int.TryParse(input, out choice))
            {
                switch (choice)
                {
                    case 1:
                        break;
                    case 2:
                        break;
                    default:
                        Console.WriteLine("Invalid input");
                        break;
                }

            }
            return choice;


        }

        public Dvd GetNewDvdInfo() //looping here
        {
            string inputReleaseYear;
            string inputRating;

            int id = 4;
            string readTitle;            
            int readReleaseYear;
            string readDirector;            
            float readRating;

            Console.WriteLine("What is the Title of the DVD?");
            readTitle = Console.ReadLine();

            Console.WriteLine("What is the Release Year of the DVD?");
            inputReleaseYear = Console.ReadLine();
            int.TryParse(inputReleaseYear, out readReleaseYear);

            Console.WriteLine("Who is the Director of the DVD?");
            readDirector = Console.ReadLine();

            Console.WriteLine("What is the star rating of the DVD?");
            inputRating = Console.ReadLine();
            float.TryParse(inputRating, out readRating);


            var dvd = new Dvd(id, readTitle, readReleaseYear, readDirector, readRating);
            Dvd newDvd = GetNewDvdInfo();
            return dvd;
        }

        public void DisplayDvd(Dvd dvd)
        {

        }

        public Dvd EditDvdInfo(Dvd dvd)
        {
            return null;
        }

        public int SearchDvd()
        {
            return 0;
        }

        public Boolean ConfirmRemoveDvd(Dvd dvd)
        {
            return false;
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DvdManager.Models;
using DvdManager.Data;
using DvdManager.View;

/* 
    Run() : void
    Private CreateDvd(): void
    Private DisplayDvds(): void
    Private SearchDvds(): void
    Private EditDvd() : void
    Private RemoveDvd() : void
*/

namespace DvdManager.Controllers
{
    public class DvdController
    {
        public DVDList _dvds = new DVDList();        


        public void Run()
        {
            Console.WriteLine("Welcome To Dvd Manager");
            DvdView view = new DvdView();

            var pass = view.GetMenuChoice();

            if (pass == 1)
            {
                CreateDvd();
            } 
            else if (pass == 2)
            {
                view.GetNewDvdInfo();
                CreateDvd();
            }
            else
                Console.WriteLine("Invalid.");            
        }

        private void CreateDvd() //Create
        {
            var myView = new DvdView();
            var dvdInfos = myView.GetNewDvdInfo();

            List<Dvd> Dvds = _dvds.GetList();

            Dvds.Add(new Dvd(0, "Batman", 2010, "Bruce", 4));
            Dvds.Add(new Dvd(1, "Superman", 2009, "John", 4));
            Dvds.Add(new Dvd(2, "Wonderwoman", 2012, "Omar", 4));
            Dvds.Add(dvdInfos);


            DisplayDvds();
        }

        private void DisplayDvds() //Read List<Dvd> dvds
        {
            List<Dvd> Dvds = _dvds.GetList();

            for (int i = 0; i < Dvds.Count; i++)
            {                
                Console.WriteLine(Dvds[i]);
            }

            RemoveDvd();
        }

        private void SearchDvds() //List
        {

        }

        private void EditDvd(int id, Dvd dvd) //Update
        {

        }

        private void RemoveDvd() //Delete int id
        {
            List<Dvd> Dvds = _dvds.GetList();

            //Dvds.RemoveAt(Dvds[1]);
            Dvds.Remove(Dvds.Single(x => x.Id == 1));

            Console.WriteLine("Removed movie from list:");

            for (int i = 0; i < Dvds.Count; i++)
            {
                Console.WriteLine(Dvds[i]);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用DvdManager.Models;
使用DvdManager.Data;
使用DvdManager.View;
/* 
Run():void
私有CreateDvd():void
专用显示DVD():无效
私有搜索DVD():无效
私有EditDvd():无效
Private RemoveDvd():void
*/
命名空间管理器.控制器
{
公共类DVD控制器
{
公共DVD列表_dvds=新DVD列表();
公开募捐
{
Console.WriteLine(“欢迎使用Dvd管理器”);
DvdView视图=新的DvdView();
var pass=view.GetMenuChoice();
如果(通过==1)
{
CreateDvd();
} 
否则如果(通过==2)
{
view.GetNewDvdInfo();
CreateDvd();
}
其他的
Console.WriteLine(“无效”);
}
私有void CreateDvd()//创建
{
var myView=新的dvdvview();
var dvdInfos=myView.GetNewDvdInfo();
列出DVD=_Dvds.GetList();
Dvd.Add(新Dvd(0,“蝙蝠侠”,2010,“布鲁斯”,4));
Dvd.Add(新Dvd(1,“超人”,2009,“约翰”,4));
Dvd.Add(新Dvd(2012年《神奇女侠》第2部,《奥马尔》第4部);
添加(DVD信息);
显示DVD();
}
私有void displayDVD()//读取列表DVD
{
列出DVD=_Dvds.GetList();
对于(int i=0;ix.Id==1));
Console.WriteLine(“已从列表中删除电影:”);
对于(int i=0;i

您正在GetNewDvdInfo()的末尾进行递归调用,只需将其删除。

您在此处递归调用此方法:

            var dvd = new Dvd(id, readTitle, readReleaseYear, readDirector, readRating);
            Dvd newDvd = GetNewDvdInfo(); //!!!
            return dvd;

因此出现了循环。似乎不需要这行代码。

GetNewDVD()在方法内部调用自身:
Dvd newDvd=GetNewDVD()
GetNewDvdInfo
在末尾调用自身。你期望它做什么?实际上,我在没有这个的情况下遇到了一个循环问题。添加这个问题的目的是因为我被告知我没有存储值。
else if(pass==2)
calls
view.GetNewDvdInfo()然后
CreateDvd()调用
view.GetNewDvdInfo()再次。