问题描述
如何解决以下编码错误?请参阅下面的粗体.class代码:-)
How do i solve the following coding errors ? See bold .class code below :-)
- GetAvailableOffers()/命名空间不能包含成员sutch作为字段或方法
- foreach(文章中的文章)/名称文章不会在当前上下文中退出/
//代码
使用System;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
命名空间Sauna
{
SaunaApp班级$
{
私人清单<职员>工作人员;
私人清单<文章>文章;
$
public SaunaApp()
{
staff = new List< Staffmember>();
articles = new List< Article>();
FillWithData();
}
public void FillWithData()
{
articles.Add(新肉类(QUOT;牛排和QUOT ;,3.2米,DateTime.Now));
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ; articles.Add(new Meat(" Grill",3.9m,DateTime.Now.AddDays(3)));
$
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; articles.Add(新鱼(QUOT;萨拉蒙" ;,8.3米,DateTime.Now.AddDays(1)));
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; articles.Add(new Fish("rab",8.3m,DateTime.Now.AddDays(2)));
$
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; articles.Add(新毛巾("大",2.5米));
$
articles.Add(新毛巾("小",2.5米));
$
;&NBSP;&NBSP; articles.Add(新鱼(QUOT;萨拉蒙" ;,8.3米,DateTime.Now.AddDays(1)));
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; articles.Add(new Fish(" Krab",8.3m,DateTime.Now.AddDays(2)));
$
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; articles.Add(新毛巾("大",8.3米));
$
articles.Add(new Towel("Small",8.3m));
$
;&NBSP;&NBSP; articles.Add(new Relaxmassage("Relax",8.3m));
$
articles.Add(new Relaxmassage(" Sport",8.3m));
}
public IReadOnlyList< Staffmember> ShowAllStaffmembers()
{
return staff.AsReadOnly();
}
public bool AddStaffmember(String name,int age)
{
foreach(工作人员的工作人员)
{
if(staffmember.Name == name)
{
返回false;
}
}
工作人员newStaffmember =新员工(姓名,年龄);
staff.Add(newStaffmember);
$
返回true;
}
public bool RemoveStaffmember(字符串名称)
{
foreach(工作人员的工作人员)
{
if(staffmember.Name == name)
{
staff.Remove(staffmember);
$
;&NBSP;返回true;
}
}
返回false;
}
public IReadOnlyList< Article> ShowAllArticles(bool sortByPrice)
{
if(!sortByPrice)
{
articles.Sort();
}
否则为
{
articles.Sort(new ArticleCompare());
$
}
return articles.AsReadOnly();
}
public void ExportArticles(string filename)
{
using(FileStream stream = new FileStream(filename,FileMode.Create))
$
{
使用(StreamWriter的作家=新的StreamWriter(流))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
INT计数器= 0;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;的foreach(在文章文章文章)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; writer.Write(article.ToString()+英寸;");
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
如果(计数器== 3)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; writer.Write(Environment.NewLine);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;计数器= 0;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
}
}
}
}
public IReadOnlyList< IAvailable> GetAvailableOffers()
{
列表与LT; IAvailable> offers = new List< IAvailable>();
foreach(文章中的条款)
{
if(文章是IAvailable)
$
; offer.Add(文章为IAvailable);
}
return offers.AsReadOnly();
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sauna
{
class SaunaApp
{
private List<Staffmember> staff;
private List<Article> articles;
public SaunaApp()
{
staff = new List<Staffmember>();
articles = new List<Article>();
FillWithData();
}
public void FillWithData()
{
articles.Add(new Meat("Steak", 3.2m, DateTime.Now));
articles.Add(new Meat("Grill", 3.9m, DateTime.Now.AddDays(3)));
articles.Add(new Fish("Salamon", 8.3m, DateTime.Now.AddDays(1)));
articles.Add(new Fish("rab", 8.3m, DateTime.Now.AddDays(2)));
articles.Add(new Towel("Large", 2.5m));
articles.Add(new Towel("Small", 2.5m));
articles.Add(new Fish("Salamon", 8.3m, DateTime.Now.AddDays(1)));
articles.Add(new Fish("Krab", 8.3m, DateTime.Now.AddDays(2)));
articles.Add(new Towel("Large", 8.3m));
articles.Add(new Towel("Small", 8.3m));
articles.Add(new Relaxmassage("Relax", 8.3m));
articles.Add(new Relaxmassage("Sport", 8.3m));
}
public IReadOnlyList<Staffmember> ShowAllStaffmembers()
{
return staff.AsReadOnly();
}
public bool AddStaffmember(String name, int age)
{
foreach (Staffmember staffmember in staff)
{
if (staffmember.Name == name)
{
return false;
}
}
Staffmember newStaffmember = new Staffmember(name, age);
staff.Add(newStaffmember);
return true;
}
public bool RemoveStaffmember(string name)
{
foreach (Staffmember staffmember in staff)
{
if (staffmember.Name == name)
{
staff.Remove(staffmember);
return true;
}
}
return false;
}
public IReadOnlyList<Article> ShowAllArticles(bool sortByPrice)
{
if (!sortByPrice)
{
articles.Sort();
}
else
{
articles.Sort(new ArticleCompare());
}
return articles.AsReadOnly();
}
public void ExportArticles(string filename)
{
using (FileStream stream = new FileStream(filename, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
int counter = 0;
foreach (Article article in articles)
{
writer.Write(article.ToString() + ";");
}
if (counter == 3)
{
writer.Write(Environment.NewLine);
counter = 0;
}
}
}
}
}
public IReadOnlyList<IAvailable> GetAvailableOffers()
{
List<IAvailable> offers = new List<IAvailable>();
foreach (Article article in articles)
{
if (article is IAvailable)
offers.Add(article as IAvailable);
}
return offers.AsReadOnly();
}
}
推荐答案
- GetAvailableOffers()/命名空间不能包含成员sutch作为字段或方法
- foreach(文章中的文章文章)/名称文章不会在当前上下文中退出/
public IReadOnlyList< Article> ShowAllArticles(bool sortByPrice)
{
if(!sortByPrice)
{
articles.Sort();
}
否则为
{
articles.Sort(new ArticleCompare());
$
}
return articles.AsReadOnly();
}
public void ExportArticles(string filename)
{
using(FileStream stream = new FileStream(filename,FileMode.Create))
$
{
使用(StreamWriter的作家=新的StreamWriter(流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NBSP;&NBSP;&NBSP;&NBSP;计数器= 0;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
}
}
}
}
public IReadOnlyList< IAvailable> GetAvailableOffers()
{
列表与LT; IAvailable> offers = new List< IAvailable>();
foreach(文章中的条款)
{
if(文章是IAvailable)
$
; offer.Add(文章为IAvailable);
}
return offers.AsReadOnly();
}
}
public IReadOnlyList<Article> ShowAllArticles(bool sortByPrice)
{
if (!sortByPrice)
{
articles.Sort();
}
else
{
articles.Sort(new ArticleCompare());
}
return articles.AsReadOnly();
}
public void ExportArticles(string filename)
{
using (FileStream stream = new FileStream(filename, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
int counter = 0;
foreach (Article article in articles)
{
writer.Write(article.ToString() + ";");
}
if (counter == 3)
{
writer.Write(Environment.NewLine);
counter = 0;
}
}
}
}
}
public IReadOnlyList<IAvailable> GetAvailableOffers()
{
List<IAvailable> offers = new List<IAvailable>();
foreach (Article article in articles)
{
if (article is IAvailable)
offers.Add(article as IAvailable);
}
return offers.AsReadOnly();
}
}
使用插入代码块发布代码时编辑器菜单栏上的按钮。
它可以更好地保持格式化。
看起来你放了GetAvailableOffers班级以外的方法。
将其上移,使其符合班级定义。
- Wayne
Use the Insert Code Block button on the editor menu bar when posting code.
It does a better job of maintaining formatting.
It looks like you put the GetAvailableOffers method outside of the class.
Move it up so it's inside the class definition.
- Wayne
这篇关于我如何解决/ GetAvailableOffers()名称文章不会在电流上下文中退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!