运行程序后,我只能为第一个学生插入数据,之后它只显示我应该插入的内容,但没有给我这样做的权利。没有编译错误。虽然我认为问题出在我分配变量时,但这可能是一个问题,因为我不知道是否应该用 0 初始化 int。如果我不初始化变量,我会得到很多编译错误。另外,在我的情况下,为什么它会显示带有随机数字“48”的学生电话号码?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomCollections
{
internal class Program
{
string studentNumber;
string studentName;
string age;
int phoneNumber;
public static void enterData()
{
int studentNumber, age, phoneNumber;
string studentName;
Console.WriteLine("Enter Student Number:");
studentNumber = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Name:");
studentName = Console.ReadLine();
Console.WriteLine("Enter Age:");
age = int.Parse(Console.ReadLine());
Console.WriteLine("Enter phone number:");
phoneNumber = int.Parse(Console.ReadLine());
}
public static void displayData()
{
string studentNumber=null;
string studentName=null;
string age=null;
int phoneNumber=0;
Console.WriteLine("Student Number:{0}",studentNumber);
Console.WriteLine("Student Name:{0}",studentName);
Console.WriteLine("Student Age:{0}", age);
Console.WriteLine("Student phone number:{0}",phoneNumber);
Console.ReadKey();
}
public static void Main(String[] args)
{
string studentNumber;
string studentName;
string age;
int phoneNumber;
enterData();
displayData();
}
}
}
我还将在这里显示结果:
Enter Student Number:
2
Enter Name:
seli
Enter Age:
22
Enter phone number:
2207885
Student Number:
Student Name:
Student Age:
Student phone number:48