2012-10-09 33 views
-1

我想運行一個程序和編譯器似乎只是在執行後崩潰......我沒有生成錯誤。試圖運行我的代碼和編譯器似乎只是關閉後,它執行

我試過註釋掉代碼塊,但仍無濟於事。我已經將主複製到另一個文件並運行它,它的工作....所以我真的難住。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
namespace ConsoleApplication1 
{ 
    public class Student 
{ 

string Fname, Lname, Program ; 
int Sid ; 

// Inputting information for students 
public void InputStudentInfo() 
{ 
    Console.WriteLine ("Please enter your first name") ; 
    Fname = Console.ReadLine() ; 
    Console.WriteLine ("Please enter you last name") ; 
    Lname = Console.ReadLine() ; 
    Console.WriteLine ("Please enter you student ID#") ; 
    Sid = int.Parse(Console.ReadLine()) ; 
    Console.WriteLine ("Enter the Program that you are completeing") ; 
    Program = Console.ReadLine() ; 
} 

// Printing information for students 
public void PrintStudentInfo() 
{ 
    Console.Write (" Your name is " + Fname) ; 
    Console.Write(" " + Lname); 
    Console.WriteLine (" Your student identification number is " + Sid) ; 
    Console.WriteLine (" The program you are registered for is " + Program) ; 
} 

/* public void MenuInterface() 
{ 
    Console.WriteLine (" 1. Input Student information") ; 
    Console.WriteLine (" 2. Input Course information") ; 
    Console.WriteLine (" 3. Input Grade information") ; 
    Console.WriteLine (" 4. Print Course information") ; 
    Console.WriteLine (" 5. Print Student information") ; 
    Console.WriteLine (" 6. Print Grade information") ; 
    Console.WriteLine (" 7. Print Student information including Course they are registered in and the grade obtained for that course") ; 
    Console.WriteLine (" 8. Print grade info of the course in which student has achieved the highest grade") ; 
    Console.WriteLine (" 0. Exit") ; 
    Console.WriteLine (" Please select a choice from 0-8") ; 
    accode = Console.ReadLine(); 
} */ 

} 

public class Course 
{ 

string course1, course2, course3 ; 
int Stuid ; 

// Inputting Course Information 
public void InputCourseInfo() 
{ 
    Console.WriteLine (" Please re-enter your identification number") ; 
    Stuid = int.Parse(Console.ReadLine()) ; 
    Console.WriteLine (" Enter the name of your first course") ; 
    course1 = Console.ReadLine() ; 
    Console.WriteLine (" Enter the name of your second course") ; 
    course2 = Console.ReadLine() ; 
    Console.WriteLine (" Enter the name of your third course") ; 
    course3 = Console.ReadLine() ; 
} 

// Printing Course Information 
public void PrintCourseInfo() 
{ 
    Console.WriteLine (" Your ID # is " + Stuid) ; 
    Console.Write (" The Courses you selected are " + course1) ; 
    Console.Write("," + course2); 
    Console.Write(" and " + course3); 
} 
} 
public class Grade : Course 
{ 
    int Studentid ; 
    int [] hwgrade ; 
    int [] cwgrade ; 
    int [] midegrade ; 
    int [] finalegrade ; 
    int [] totalgrade ; 
    string coursename ; 

    public Grade (string cname , int Studentident , int [] homework , int [] classwork , int [] midexam , int [] finalexam) 
    { 
     coursename = cname ; 
     Studentid = Studentident ; 
     hwgrade = homework ; 
     cwgrade = classwork ; 
     midegrade = midexam ; 
     finalegrade = finalexam ; 
    } 

    public string coname 
    { 
     get 
     { 
      return coursename ; 
     } 
     set 
     { 
      coursename = value ; 
     } 
    } 

    public int Studentidenty 
    { 
     get 
     { 
      return Studentid ; 
     } 
     set 
     { 
      Studentid = value ; 
     } 
    } 

    public void InputGradeInfo() 
    { 
     Console.WriteLine (" Please enter your Student ID") ; 
     grade.Studentidenty = Console.ReadLine() ; 
     for (int i = 0; i < 3; i++) 
     { 
      Console.Writeline (" Please enter the Course name") ; 
      grade.coname[i] = Console.Readline() ; 
      Console.Writeline (" Please enter your homework grade") ; 
      grade.hwgrade[i] = int.parse(Console.Readline()) ; 
      // ..... 
     } 
    } 

    public void CalcTotalGrade() 
    { 
     for (int i = 0; i < 3; i++) 
     { 
      grade.courseper[i] = (grade.hwgrade[i] + grade.cwgrade[i])/2; 
      grade.finalper[i] = (grade.midexam[i] + grade.finalegrade[i])/2; 
      grade.totalgrade[i] = (grade.courseper[i] + finalper[i])/2; 
     } 
    } 

    public void PrintGradeInfo() 
    { 
     for (int i = 0; i < 3; i++) 
     { 
      Console.Writeline (" Your homework grade is" + grade.hwgrade[i]) ; 
      // ..... 
     } 
    } 
     static void Main(string[] args) 
     { 
      int accode ; 
      Student student = new Student() ; 
      Course course = new Course() ; 
      Grade grade = new Grade() ; 
      do 
      { 
       Console.WriteLine(" 1. Input Student information"); 
       Console.WriteLine(" 2. Input Course information"); 
       Console.WriteLine(" 3. Input Grade information"); 
       Console.WriteLine(" 4. Print Course information"); 
       Console.WriteLine(" 5. Print Student information"); 
       Console.WriteLine(" 6. Print Grade information"); 
       Console.WriteLine(" 7. Print Student information including Course they are registered in and the grade obtained for that course"); 
       Console.WriteLine(" 8. Print grade info of the course in which student has achieved the highest grade"); 
       Console.WriteLine(" 0. Exit"); 
       Console.WriteLine(" Please select a choice from 0-8"); 
       accode = Console.ReadLine(); 
       switch (accode) 
       { 
        case 1: 
         student.InputStudentInfo(); 
         break; 
        case 2: 
         course.InputCourseInfo(); 
         break; 
        case 3: 
         grade.InputGradeInfo(); 
         break; 
        case 4: 
         course.PrintCourseInfo(); 
         break; 
        case 5: 
         student.PRintStudentInfo(); 
         break; 
        case 6: 
         grade.PrintGradeInfo(); 
         break; 
        case 0: 
         Console.WriteLine(" You have chosen to exit the program have a good day. =)"); 
         break; 
       } 
      } while (accode != 0); 
      Console.ReadKey(); 
     } 
    } 
} 

時,我說,我複製了主......我的意思是我把所有的代碼,這是在主,打開一個新的項目,放在後面的代碼,程序運行起來的條款。我嘗試了關於使用try和catch的建議......但同樣的問題仍然會發生,當我按start時,沒有錯誤,cmd會在瞬間打開和關閉。當我按Ctrl + F5屏幕出現,但只說'按任意鍵繼續'。

+1

您的代碼將無法編譯 - 你要分配從'string'值'Console.ReadLine()'到一個'int'變量。 –

+0

您需要做的第一件事是捕獲當int.Parse(Console.ReadLine())''嘗試解析非整數字符串時失敗時發生的異常。你需要做的第二件事是隻讀字符串。沒有理由說'Sid'需要是一個整數。一旦您嘗試自己調試問題,我們可能會幫助您。你確切的問題並不清楚,你的程序真的什麼都不做,你什至不告訴我們什麼是不工作的。 –

+0

你需要解釋你的意思是什麼「我已經將主複製到另一個文件並運行它,它的工作....所以我真的難住。」就這樣做! –

回答

3

把一個嘗試捕捉你的主要方法,這樣給你一個提示,什麼是錯的:

static void Main(string[] args) 
      { 
    try 
    { 
       int accode ; 
       Student student = new Student() ; 
       Course course = new Course() ; 
       Grade grade = new Grade() ; 
       do 
       { 
        Console.WriteLine(" 1. Input Student information"); 
... 
        Console.WriteLine(" 0. Exit"); 
        Console.WriteLine(" Please select a choice from 0-8"); 
        accode = Console.ReadLine(); 
        switch (accode) 
        { 
    ... 
        } 
       } while (accode != 0); 
    } 
    catch (Exception Ex) 
    { 
     Console.Writeline("Exception: " + ex.Message); 
    } 
       Console.ReadKey(); 
      } 
相關問題