Jak rozwiązać błąd typu Runtime Error?

2018-11-01 22:46
Majster Plytka
0

Program w jednym w visual studio działa poprawnie natomiast w innym ide jest blad, którego nie rozumiem:

Unhandled Exception:
System.FormatException: Input string was not in a correct format.
  at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00013] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00008] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Convert.ToInt32 (System.String value) [0x0000b] in <f56c876907e742b0aa586f051fcce845>:0 
  at Solution.Main (System.String[] args) [0x00022] in solution.cs:55 
[ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: Input string was not in a correct format.
  at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00013] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Int32.Parse (System.String s, System.IFormatProvider provider) [0x00008] in <f56c876907e742b0aa586f051fcce845>:0 
  at System.Convert.ToInt32 (System.String value) [0x0000b] in <f56c876907e742b0aa586f051fcce845>:0 
  at Solution.Main (System.String[] args) [0x00022] in solution.cs:55 
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution
{

    // Complete the countSwaps function below.
    static void countSwaps(int[] arr)
    {

        int temp = 0;
        int numSwaps = 0;

        for (int write = 0; write < arr.Length; write++)
        {
            for (int sort = 0; sort < arr.Length - 1; sort++)
            {
                if (arr[sort] > arr[sort + 1])
                {
                    temp = arr[sort + 1];
                    arr[sort + 1] = arr[sort];
                    arr[sort] = temp;
                    numSwaps++;
                }

            }

        }

        Console.WriteLine("Array is sorted in " + numSwaps + " swaps.",0);
        Console.WriteLine("First Element: " + arr[0]);
        Console.WriteLine("Last Element: " + arr[arr.Length - 1]);

    }

    static void Main(string[] args)
    {

        int n = Convert.ToInt32(Console.ReadLine());

        int[] arr = new int[n];
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = Convert.ToInt32(Console.ReadLine());
        }

        countSwaps(arr);

        Console.ReadKey();
    }
}
edytowany 1x, ostatnio: furious programming, 2018-11-01 23:29

Pozostało 580 znaków

2018-11-02 01:39
1

Input string was not in a correct format. - co w tym niejasnego? Oczekujesz liczby, wpisujesz nie liczbę. Poczytaj o tym, jak bezpiecznie konwertować tekst na liczby w C#.
IDE nie ma z tym nic wspólnego.


"HUMAN BEINGS MAKE LIFE SO INTERESTING. DO YOU KNOW, THAT IN A UNIVERSE SO FULL OF WONDERS, THEY HAVE MANAGED TO INVENT BOREDOM."

Pozostało 580 znaków

Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0