none
I tried to make some code that will determine the percentage of 2 numbers you type in yourself. So my problem is if you run it it will only display the numbers corrctly under 10% above it will round them heavily. RRS feed

  • Frage

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Prozent
    {
        class Prozent
        {
          
    
            static float Percentage(int x, int y)
            {
                float z;
                if (x > y)
                {
                    z = x / y;
                    z = 100 / z;
                    return z;
                }
                else
                {
                    z = y / x;
                    z = 100 / z;
                    return z;
                }
                      
            }
    
            static void Main(string[] args)
            {
                while (true)
                {
                    Console.WriteLine("Write the 2 numbers you want the percentage of, one line each");
                    Console.WriteLine("type stop to quit");
                    string g = Console.ReadLine();
    
                    if (g == "stop")
                        break;
                    else
                    {
                        int x = Convert.ToInt32(g);
                        int y = Convert.ToInt32(Console.ReadLine());
                        float f = Percentage(x, y);
                        if(x > y)
                            Console.WriteLine(y + " is " + f + " percent of " + x);
                        else
                            Console.WriteLine(x + " is " + f + " percent of " + y);
                    }
                              
                                 
                    
                }
                
            }
          
        }
    }
    
    
    
    


    • Bearbeitet DaFatPoro Samstag, 7. Oktober 2017 15:59
    • Bearbeitet Stefan FalzModerator Sonntag, 8. Oktober 2017 08:19 Codeformatierung hinzugefügt.
    Samstag, 7. Oktober 2017 15:58

Antworten

  • Hallo,

    wenn du deine floats durch was auch immer ersetzt, wird das nicht direkt weiterhelfen. Wirf mal einen Blick auf die Signatur deiner Methode:

    static float Percentage(int x, int y)

    Da solltest du also mal den Type deiner Parameter überdenken. Weil dein Rückgabetyp ein float ist, ändert das erstmal nichts daran, dass du 2 Integers dividierst. Ob du dieses Resultat dann als float oder decimal oder sonst was zurückgibst, ist nicht mehr so entscheidend.

    So könnte es beispielsweise funktionieren:

    public decimal Percentage(decimal z, decimal n) {
      if ( n == 0 ) { throw new ArgumentException("..."); }
      return z / n;
    }
    
    public decimal Percentage(int z, int n) {
      if ( n == 0 ) { throw new ArgumentException("..."); }
      return (decimal)z / (decimal)n;
    }

    Gruß





    Sonntag, 8. Oktober 2017 10:09

Alle Antworten

  • Hi,

    poste bitte auf Deutsch und stell deine Frage bitte in den Inhaltsbereich und nicht einfach so in Betreff.

    So ganz allgemein: float ist für dich wohl der falsche Datentyp. Siehe dazu:

      https://exceptionnotfound.net/decimal-vs-double-and-other-tips-about-number-types-in-net/

     


    Gruß, Stefan
    Microsoft MVP - Visual Developer ASP/ASP.NET
    http://www.asp-solutions.de/ - Consulting, Development
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community


    Samstag, 7. Oktober 2017 16:46
    Moderator
  • Danke für die Antwort. Tut mir Leid, dass ich auf Englisch geschrieben habe, hatte wohl einen Brainlag oder so. Soll ich jetzt in meinem Code die floats durch decimals erstzen?
    Samstag, 7. Oktober 2017 17:53
  • Hi,

    das wäre eine Möglichkeit. Ob das nun dein Problem löst, kann ich dir aber eher sagen, wenn Du deine Frage mal auf Deutsch und ein wenig ausführlicher beschreibst.


    Gruß, Stefan
    Microsoft MVP - Visual Developer ASP/ASP.NET
    http://www.asp-solutions.de/ - Consulting, Development
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community

    Sonntag, 8. Oktober 2017 08:18
    Moderator
  • Hallo,

    wenn du deine floats durch was auch immer ersetzt, wird das nicht direkt weiterhelfen. Wirf mal einen Blick auf die Signatur deiner Methode:

    static float Percentage(int x, int y)

    Da solltest du also mal den Type deiner Parameter überdenken. Weil dein Rückgabetyp ein float ist, ändert das erstmal nichts daran, dass du 2 Integers dividierst. Ob du dieses Resultat dann als float oder decimal oder sonst was zurückgibst, ist nicht mehr so entscheidend.

    So könnte es beispielsweise funktionieren:

    public decimal Percentage(decimal z, decimal n) {
      if ( n == 0 ) { throw new ArgumentException("..."); }
      return z / n;
    }
    
    public decimal Percentage(int z, int n) {
      if ( n == 0 ) { throw new ArgumentException("..."); }
      return (decimal)z / (decimal)n;
    }

    Gruß





    Sonntag, 8. Oktober 2017 10:09