2010-09-13 23 views
5

Ortaya çıkması ve yüzdesini göstermesi beklenen her 200 olasıdan 3 puan alan bu programa sahibim. ama sayıları girdiğimde cevap olarak 00.0 aldım. Neyi yanlış yapabilirim?C# 'de Basit Matematik Sayı #

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int Score1; 
      int Score2; 
      int Score3; 

      Console.Write("Enter your score (out of 200 possible) on the first test: "); 

      Score1 = int.Parse(Console.ReadLine()); 
      Console.Write("Enter your score (out of 200 possible) on the second test: "); 

      Score2 = int.Parse(Console.ReadLine()); 
      Console.Write("Enter your score (out of 200 possible on the third test: "); 

      Score3 = int.Parse(Console.ReadLine()); 
      Console.WriteLine("\n"); 

      float percent = ((Score1+ Score2+ Score3)/600); 

      Console.WriteLine("Your percentage to date is: {0:00.0}", percent); 
      Console.ReadLine(); 
     } 
    } 
} 

cevap

17

Sen bir tamsayı ile bir tamsayı bölüyorlar - her zaman tamsayı aritmetik kullandığı, bir float sonucu atama yaparken bile. Bunu düzeltmenin en basit yolu, işlenenlerden birini bir yüzdürme yapmaktır, örn. o (girişler 0 ile 200 arasında olduğunu varsayarsak) 0 ile 1 arasında bir numara vereceğiz - Bu aslında size rağmen yüzde verecek olmaz

float percent = (Score1 + Score2 + Score3)/600f; 

Not.

gerçek bir yüzdesini almak için, 100 ile çarpın gerekir - sadece 6 ile bölünerek eşdeğer olan: Bir yüzdesinin hesaplanması değildir

float percent = (Score1 + Score2 + Score3)/6f; 
+4

Neden? Bunu 33 saniyeden daha kısa sürede nasıl yazabildiniz? : -P – Patrick

+2

@Patrick: Buradan ilk satır: http://meta.stackexchange.com/questions/9134/jon-skeet-facts/9135#9135;) – Bobby

+0

Bu sanırım C'nin en acımasız tuzaklarından biridir. -deri dillerin ifade sözdizimi ... – sum1stolemyname

3

. Kullanıcı maksimum puan girer düşünün: 200 + 200 + 200 = 600, bu 600 = 1 ile bölünür. Eğer puanların herhangi biri 200'ün altına girerse, toplam 1'den az olur ve 'a yuvarlanır. Onları float olarak saklamalısınız (yuvarlama için hiçbir bilgiyi kaybetmediğinizden emin olmak için) ve 100 ile çarpın.

2

Bu bir veri türü sorunudur, bence. Değişken yüzdelerinizin yüzdüğü ve tüm puanların int olduğu için, yüzdelik puanlardan birini tahmin etmelisiniz.

0
using System; 

namespace stackOverflow 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int Score1; 
      int Score2; 
      int Score3; 

      Console.Write("Enter your score (out of 200 possible) on the first test: "); 
      Score1 = int.Parse(Console.ReadLine()); 
      Console.Write("Enter your score (out of 200 possible) on the second test: "); 
      Score2 = int.Parse(Console.ReadLine()); 
      Console.Write("Enter your score (out of 200 possible on the third test: "); 
      Score3 = int.Parse(Console.ReadLine()); 
      Console.WriteLine("\n"); 
      var percent = ((Score1 + Score2 + Score3)/6D); 
      Console.WriteLine("Your percentage to date is: {0:00.0}", percent); 
      Console.ReadLine(); 

     } 
    } 

}