【问题标题】:My program closes as soon as it opens我的程序一打开就关闭
【发布时间】:2015-07-22 00:44:15
【问题描述】:

每当我尝试启动它时,它都会出现这些错误并且应用程序无法运行。为什么?我该如何解决它,它是我的编辑器吗? (我使用的是 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# 的新手,所以不要让它变得复杂,我愿意接受改进我的代码的建议。

【问题讨论】:

  • 我猜它正在启动另一个项目,主要是因为Console.WriteLine(str); 应该给出编译器错误; str 永远不会被赋予一个值。检查右侧的解决方案列表,右键单击您的解决方案并单击“设置为启动项目”
  • Int parse可以抛出异常,使用trycatch
  • 这段代码永远无法编译。请发布导致您遇到的问题的真实代码。
  • 返回输入 * inputa; - 变量 input 和 inputa 不在计算方法的范围内。正如@JohnSaunders 正确指出的那样,这个程序甚至不应该编译,更不用说运行了。

标签: c#


【解决方案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();
    }
}

您可以研究这些差异以开始了解问题所在..

【讨论】:

  • 谢谢老兄!这真的很有帮助。考虑到我在星期天盯着看,能够比较有很大帮助。
【解决方案2】:

看起来你是个初学者。

首先,将您的第二类(程序)设为静态。喜欢

public static void detail()

现在,而不是做

Int32.TryParse(input, out number);

这样做:将数字存储为“公开”在您的 Program 类的顶部。摆脱 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 的问题。

但是做我告诉你的那些事!

@Aaron Anodide 的回答是好的,但是说到 Console.WriteLine(answer);它会出错,因为您尝试编写的是 int 而不是字符串!!!!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-05-14
  • 2014-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多