简体   繁体   中英

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

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. But whenever I am submitting this to the URI Compiler, it is not accepting my answer and is giving a run time error. I am not sure if there is a bug or not?

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.

... 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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