【问题标题】:Finding percent occurrence always is equal to 0? [duplicate]发现百分比出现总是等于 0? [复制]
【发布时间】:2015-03-13 12:00:13
【问题描述】:

我试图找出 pi 中每个数字的出现次数(有多少个 1、2、3)。我已经得到每个数字的出现次数,但是当我尝试找到每个数字的百分比时,我总是得到 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 + "%");
      }
   }
}

【问题讨论】:

    标签: java pi


    【解决方案1】:

    int 除以intint。所以3/4 = 07/2 = 3等等。

    【讨论】:

    • 嗯,好的。我改变了 int percentOccurence = ((digitCount[i] / sum));将百分比加倍 = ((digitCount[i] / (double)sum)*100);现在它完美地工作了。谢谢!
    【解决方案2】:

    改变

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

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

    或者,如果您希望百分比显示为分数,请进行浮点除法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      相关资源
      最近更新 更多