2015-03-13 116 views
0

我试图找到pi中每个数字的出现次数(有多少个1,2,3在那里)。我有发生的每一位工作的数量,但是当我试图找到每个数字的百分比,我总是得到0下面的代码:查找百分比总是等于0?

import java.util.*; 
import java.io.*; 
import java.net.*; 
import java.text.DecimalFormat; 

public class PIAnalyzer { 

    static int digit, index; 

    public static Scanner openFileURL(String website) { 
     Scanner scan = null; 
     try { 
     URL webAddress = new URL(website); 
     InputStream input = webAddress.openStream(); 
     System.out.println("Opened web page: " + webAddress.getPath()); 
     scan = new Scanner(input); 
     } catch (MalformedURLException e) { 
     System.out.println("Cannot open web site:"); 
     System.out.println(e); 
     } catch (IOException io) { 
     System.out.println("IO Error:" + io.toString()); 
     } 
     return scan; 
    } 

    public static void main(String[] args) { 
     //System.getProperties().put("http.proxyHost", "10.10.1.5"); 
     //System.getProperties().put("http.proxyPort", "8080"); 
     DecimalFormat df = new DecimalFormat("#.##"); 
     int[] digitCount = new int[10]; 
     Scanner in = openFileURL("http://www.eveandersson.com/pi/digits/1000000"); 
     if (in == null) { 
     System.out.println("URL error"); 
     } 
     //ignore web page fluff at the beginning; adjust the loop so this works. 
     //we are displaying the lines of text to make it easier to determine if we are ignoring the correct amount. 
     for (int i = 0; i < 20; i++) { 
     System.out.println(in.nextLine()); 
     } 



     String line; 
     char ch; 
     int index; 
     //We analyze the file line by line 
     int countChars = 0; 
     while (in.hasNext() && countChars < 1000) { 
     line = in.nextLine(); 
     countChars += line.length(); 
     System.out.println(line); 
     char[] pear = line.toCharArray(); 
     //to look at each character, use charAt or toCharArray 
     // look at each character. 
     //for loop through the string. 
     for (int i = 0; i < pear.length; i++) { 
      ch = pear[i]; 
      if (Character.isDigit(ch)) { 
       index = ch - '0'; 
       digitCount[index]++; 
      } 
     } 


     } //end while loop through lines 
     //get sum of all numbers in for loop 
     int sum = 0; 
     for (int a = 0; a <= digitCount.length - 1; a++) { 
     sum += digitCount[a]; 

     } 
     System.out.println("Number of total char: " + countChars); 
     System.out.println("Number of char proccessed: " +sum + " //why?"); 

     System.out.println("Digit" + "\t" + "# occurrence" + "\t" + "% occurrence"); 


     for (int i = 0; i <= digitCount.length - 1; i++) { 
     System.out.print(i + "\t" + digitCount[i]); 
     int percentOccurence = ((digitCount[i]/sum)); 
     System.out.println("\t\t" + percentOccurence + "%"); 
     } 
    } 
} 

回答

2

int通过int划分为int。所以3/4 = 0,7/2 = 3等等。

+0

啊确定。我改变了int percentOccurence =((digitCount [i]/sum));加倍百分比=((digitCount [i] /(double)sum)* 100);现在它完美地工作。谢谢! – 2015-03-13 12:04:09

1

变化

int percentOccurence = ((digitCount[i]/sum)) 

int percentOccurence = (100 * digitCount[i])/sum 

或者,如果你想显示为分数的百分比,做一个浮点除法:

double percentOccurence = (double) digitCount[i]/sum