2015-07-22 61 views
0

每當我嘗試啓動它時,它都會提出這些錯誤,並且應用程序無法運行。爲什麼?我該如何解決它,是我的編輯器? (我正在使用Microsoft Visual Studio)我的程序在打開時立即關閉

  • 線程0x1688已退出,代碼爲259(0x103)。
  • 線程0x470已退出,代碼爲259(0x103)。
  • 線程0xc1c已退出,代碼爲259(0x103)。
  • 線程0x26f0已退出,代碼爲259(0x103)。
  • 線程0x2708已退出,代碼爲259(0x103)。
  • 程序'[7956] ConsoleApplication1.vshost.exe'已退出,代碼爲0(0x0)。

這是代碼。

using System; 

namespace Inputoftext 
{ 
    class Program 
    { 
     string str; 
     public void detail() 
     { 
      Console.WriteLine("Multiplication Calculator."); 
      Console.WriteLine("Number 1: "); 
      string input = Console.ReadLine(); 
      int number; 
      Int32.TryParse(input, out number); 
      Console.WriteLine("Number 2: "); 
      string inputa = Console.ReadLine(); 
      int number; 
      Int32.TryParse(input, out number); 
     } 
     public void calculations() 
     { 
      return input * inputa; 
     } 
     public void display() 
     { 
      Console.WriteLine(); 
      Console.WriteLine(str); 
      Console.ReadKey(); 
     } 
    } 
    class call 
    { 
     static void Main() 
     { 
      Program r = new Program(); 
      r.detail(); 
      r.calculations(); 
      r.display(); 
      Console.ReadKey(); 
     } 
    } 
} 

順便說一句我是新來的C#,所以不要使其向複雜和IM開放的建議,以改善我的代碼。

+1

我猜它正在啓動另一個項目,主要是因爲'控制檯。WriteLine(str);'應該給編譯器一個錯誤; 'str'永遠不會被賦予價值。檢查右邊的解決方案列表,右鍵點擊你的解決方案並點擊'Set as startup project' – Rob

+0

Int解析可以拋出異常,使用trycatch – meda

+0

這段代碼永遠不能編譯。請發佈導致您遇到問題的真實代碼。 –

回答

1

這是你的程序的固定版本:

class Program 
{ 
    int answer; 
    int number; 
    int numbera;  
    public void detail() 
    { 
     Console.WriteLine("Multiplication Calculator.");  

     string input; 

     Console.WriteLine("Number 1: "); 
     input = Console.ReadLine(); 
     Int32.TryParse(input, out number); 

     Console.WriteLine("Number 2: "); 
     input = Console.ReadLine(); 
     Int32.TryParse(input, out numbera); 
    } 
    public void calculations() 
    { 
     answer = number * numbera; 
    } 
    public void display() 
    { 
     Console.WriteLine(); 
     Console.WriteLine(answer); 
    } 
} 
class call 
{ 
    static void Main() 
    { 
     Program r = new Program(); 
     r.detail(); 
     r.calculations(); 
     r.display(); 
    } 
} 

您可以研究這些差異開始瞭解什麼是錯的..

enter image description here

+0

謝謝老兄!這真的有幫助。考慮到我週日能夠比較幫助很多。 – lesquishy

-2

看起來你是一個初學者。

首先,使你的第二類(程序)靜態。像

public static void detail() 

現在,而不是做

Int32.TryParse(input, out number); 

做到這一點:請在您的程序類的頂級存儲爲「公共」的數字。擺脫的TryParse,你不需要它

class Program 
{ 
    public int number; // can be accessed by other functions/methods 
    public int othernumber ; // can be accessed by other functions/methods 
    public int sum; 

    public void detail() 
    { 
     Console.WriteLine("Multiplication Calculator."); 
     Console.WriteLine("Number 1: "); 
     string input = Console.ReadLine(); 
     number = int.Parse(input); 
     Console.WriteLine("Number 2: "); 
     string inputa = Console.ReadLine(); 
     othernumber = int.Parse(inputa); 
    } 

爲了計算()你正在返回的東西,但它是無效的(這意味着它不返回)

待辦事項這樣的:

public void calculations() 
    { 
     sum = number * othernumber; // sum should be a global variable 
    } 

對於顯示器這樣做:

public void display() 
    { 
     Console.WriteLine("\n"); // doing \n makes a new line (you'll see) 
     Console.WriteLine(sum.toString()); // we need to make it a string like that 
     Console.ReadKey(); 
    } 

如果仍然出現錯誤,請創建一個新項目。這可能是一個Visual Studio問題。

但做那些事我告訴過你!

@Aonon Anodide的回答是好的,但是談到Console.WriteLine(answer);它會錯誤,因爲你正在嘗試寫一個int而不是一個字符串!

+0

'Console.WriteLine()'處理'int'參數就好了。 https://msdn.microsoft.com/en-us/library/70x4wcx1(v=vs.110).aspx –