****第二次编辑**** 找出错误处理。但是输出有一个新问题。无论输入什么,数组的所有索引都归零。唯一坚持的指数是最后一个。例如: 您在一周内完成的学习时数如下: 星期天:0 星期一:0 星期二:0 星期三:0 星期四:0 星期五:0 星期六:(无论用户输入的最后一个号码是什么......这是唯一被保存的索引)
我在参数和方法方面遇到了很多麻烦。我不明白应该去哪里。对我来说这感觉很陌生。无论如何,这是一个家庭作业,但它是一个超级小片。我希望我能得到一些指导,而不是像过去那样得到投票...... 我得到了一个' NullReferenceException"当调用EnterHours时。
public class StudentUI
{
int myHours;
private string[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
public StudentUI()
{
}
public void MainMethod()
{
Student my = new Student();
Console.WriteLine("Please enter your name:\t ");
my.Name = Console.ReadLine();
Console.WriteLine("Please enter your student ID number:\t ");
my.ID = int.Parse(Console.ReadLine());
FillHours(my);
this.DisplayData(my);
this.DisplayAverage(my);
}
public void FillHours(Student my)
{
for (int i = 0; i < this.days.Length; i++)
{
Console.Write("Enter the number of hours that you studied ITDEV-115 on {0}:\t ", this.days[i]);
myHours = int.Parse(Console.ReadLine());
my.EnterHours(i, myHours);
}
}
public void DisplayAverage(Student my)
{
Console.Write("\n\nThe average number of hours studied per day was {0:N2}", my.CalculateAverage());
}
public void DisplayData(Student my)
{
double[] numHours = new double[this.days.Length];
Console.Clear();
Console.WriteLine("\n\n\nName: {0}", my.Name);
Console.WriteLine("ID: {0}", my.ID);
Console.WriteLine("\n\nThe number of hours of study that you did during the week are as follows:");
numHours = my.GetNumHours();
for (int i = 0; i < this.days.Length; i++)
{
Console.Write("\t{0}:", this.days[i]);
Console.WriteLine("\t{0:N2}", numHours[i]);
}
}
}
public class Student
{
private int id;
private string name;
private double[] hours;
public void EnterHours(int index, int myHours)
{
hours[index] = myHours;
}
public double[] GetNumHours()
{
return this.hours;
}
public double CalculateAverage()
{
double num = 0;
foreach (double number in this.hours)
{
num += number;
}
return (num / ((double)this.hours.Length));
}
}
答案 0 :(得分:1)
实例字段private double[] hours;
最初为null
,在这种情况下您无法设置其条目。
您需要创建一个实际的数组对象。
某处您必须hours = new double[length]
,其中length
是您要创建的新数组的长度。
答案 1 :(得分:1)
你可以这样做
requestify.post('https://example.com/', {
email: 'foo@bar.com'
}, {
timeout: 60000
})
.then(function(response) {
var answer = response.getBody();
console.log("answer:" + answer);
}, function(error) {
var errorMessage = "Post Failed";
if(error.code && error.body)
errorMessage += " - " + error.code + ": " + error.body
console.log(errorMessage);
// dump the full object to see if you can formulate a better error message.
console.log(error);
});
数组具有固定长度,因此您需要在分配任何值之前定义它的大小。如果你看一下Student类的构造函数,我已经超过了被用作数组大小的天数。 希望这会有所帮助。
答案 2 :(得分:0)
我从一个数组转换为一个列表。
using System;
using System.Collections.Generic;
class StudentUI {
int myHours;
private string[] days = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
public StudentUI() { }
public static void Main (string[] args) {
Student my = new Student();
Console.WriteLine("Please enter your name:\t ");
my.Name = Console.ReadLine();
Console.WriteLine("Please enter your student ID number:\t ");
my.ID = int.Parse(Console.ReadLine());
var StudentUI = new StudentUI();
StudentUI.FillHours(my);
// DisplayData(my);
// DisplayAverage(my);
}
public void FillHours(Student my) {
for (int i = 0; i < this.days.Length; i++) {
Console.Write("Enter the number of hours that you studied ITDEV-115 on {0}:\t ", this.days[i]);
myHours = int.Parse(Console.ReadLine());
my.EnterHours(i, myHours);
}
}
}
public class Student {
public int ID;
public string Name;
private List<int> Hours = new List<int>();
public void EnterHours(int index, int myHours) {
Hours.Add(myHours);
}
}