简体   繁体   English

C# 运行时错误

[英]C# Runtime Error

I have been trying to solve the following problem in the following link:我一直在尝试解决以下链接中的以下问题:

https://www.urionlinejudge.com.br/judge/en/problems/view/1010 https://www.urionlinejudge.com.br/judge/en/problems/view/1010

And this is my code:这是我的代码:

using System;

namespace URIProblemsBeginner {

 class Program

 {
    static void Main(string[] args)
    {
        int PRODUCT_ONE_CODE, PRODUCT_TWO_CODE, PRODUCT_ONE_UNITS, PRODUCT_TWO_UNITS;
        float PRODUCT_ONE_PRICE, PRODUCT_TWO_PRICE, VALUE_TO_PAY;

        PRODUCT_ONE_CODE = Convert.ToInt32(Console.ReadLine());
        PRODUCT_ONE_UNITS = Convert.ToInt32(Console.ReadLine());
        PRODUCT_ONE_PRICE = Convert.ToSingle(Console.ReadLine());
        PRODUCT_TWO_CODE = Convert.ToInt32(Console.ReadLine());
        PRODUCT_TWO_UNITS = Convert.ToInt32(Console.ReadLine());
        PRODUCT_TWO_PRICE = Convert.ToSingle(Console.ReadLine());

        VALUE_TO_PAY = (PRODUCT_ONE_UNITS * PRODUCT_ONE_PRICE) + (PRODUCT_TWO_UNITS * PRODUCT_TWO_PRICE);

        Console.WriteLine("VALOR A PAGAR: R$ " + VALUE_TO_PAY.ToString("F2"));
        Console.ReadLine();
    }
}

Whenever I am running this code for testing it in Visual Studio 2015, it runs without any error.每当我运行此代码以在 Visual Studio 2015 中对其进行测试时,它都会运行而不会出现任何错误。 But whenever I am submitting this to the URI Compiler, it is not accepting my answer and is giving a run time error.但是,每当我将其提交给 URI 编译器时,它都不会接受我的回答并给出运行时错误。 I am not sure if there is a bug or not?不知道有没有BUG?

Can anyone please help?有人可以帮忙吗? Thanks in advance!提前致谢!

According to the problem statement...根据问题说明...

The input file contains two lines of data.输入文件包含两行数据。 In each line there will be 3 values: two integers and a floating value with 2 digits after the decimal point.每行将有 3 个值:两个整数和一个小数点后 2 位的浮点值。

... but your application as written would be looking for six lines of data. ...但是您编写的应用程序将寻找六行数据。 One line for product one code, one line for product one price, etc.一品一码,一品一价等。

I believe the approach you want is to read a line and split it into its three components.我相信您想要的方法是读取一行并将其分成三个部分。 Something along the lines of the following.类似于以下内容。

using System;使用系统;

namespace URIProblemsBeginner
{
    internal class Program
    {
    private static void Main(string[] args)
    {
        int PRODUCT_ONE_CODE, PRODUCT_TWO_CODE, PRODUCT_ONE_UNITS, PRODUCT_TWO_UNITS;
        float PRODUCT_ONE_PRICE, PRODUCT_TWO_PRICE, VALUE_TO_PAY;

        ConvertInput(Console.ReadLine(), out PRODUCT_ONE_CODE, out PRODUCT_ONE_UNITS, out PRODUCT_ONE_PRICE);
        ConvertInput(Console.ReadLine(), out PRODUCT_TWO_CODE, out PRODUCT_TWO_UNITS, out PRODUCT_TWO_PRICE);

        VALUE_TO_PAY = (PRODUCT_ONE_UNITS*PRODUCT_ONE_PRICE) + (PRODUCT_TWO_UNITS*PRODUCT_TWO_PRICE);

        Console.WriteLine("VALOR A PAGAR: R$ " + VALUE_TO_PAY.ToString("F2"));
        Console.ReadLine();
    }

    private static void ConvertInput(string input, out int CODE, out int UNITS, out float PRICE)
    {
        string[] split = input.Split(' ');
        CODE = Convert.ToInt32(split[0]);
        UNITS = Convert.ToInt32(split[1]);
        PRICE = Convert.ToSingle(split[2]);
    }
}

} }

class Program
{

    private static void Main(string[] args)
    {
        int PRODUCT_ONE_CODE, PRODUCT_TWO_CODE, PRODUCT_ONE_UNITS, PRODUCT_TWO_UNITS;
        float PRODUCT_ONE_PRICE, PRODUCT_TWO_PRICE, VALUE_TO_PAY;

        ConvertInput(Console.ReadLine(), out PRODUCT_ONE_CODE, out PRODUCT_ONE_UNITS, out PRODUCT_ONE_PRICE);
        ConvertInput(Console.ReadLine(), out PRODUCT_TWO_CODE, out PRODUCT_TWO_UNITS, out PRODUCT_TWO_PRICE);

        VALUE_TO_PAY = (PRODUCT_ONE_UNITS + PRODUCT_ONE_PRICE) + (PRODUCT_TWO_UNITS + PRODUCT_TWO_PRICE);

        Console.WriteLine("VALOR A PAGAR: R$ " + VALUE_TO_PAY.ToString("F2"));
        Console.ReadLine();
    }
    private static void ConvertInput(string input, out int CODE , out int UNITS, out float PRICE)
    {
        List<string> split = input.Split(' ').ToList();
        string[] changedInput =new string[3];
        for (int i = 3 - split.Count; i <= 3; i++)
        {
            split.Add("0");
            if (split.Count ==3 )
            {
                break;
            }

        }
        int.TryParse(split[0], out CODE);
        int.TryParse(split[1], out UNITS);
        float.TryParse(split[2], out PRICE);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM