Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8726401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:14:12+00:00 2026-06-13T08:14:12+00:00

I can’t seem to figure out why it’s not printing correctly: public class test3

  • 0

I can’t seem to figure out why it’s not printing correctly:

public class test3 {
  public static void main(String[] args) {
    double[] gens = {100, 200.1, 9.3, 10};
    double d0 = 0;
    double d1 = 0;
    double d2 = 0;
    double d3 = 0;
    double d4 = 0;
    double d5 = 0;
    double d6 = 0;
    double d7 = 0;
    double d8 = 0;
    double d9 = 0;
    for (int i = 0; i < gens.length; i++) {
      double percs = gens[i];
      while (percs < -9 || 9 < percs) percs /= 10;
      percs = Math.abs(percs);
      if (percs == 0) {
        d0 += 1;
      }
      else if (percs == 1) {
        d1 += 1;
      }
      else if (percs == 2) {
        d2 += 1;
      }
      else if (percs == 3) {
        d3 += 1;
      }
      else if (percs == 4) {
        d4 += 1;
      }
      else if (percs == 5) {
        d5 += 1;
      }
      else if (percs == 6) {
        d6 += 1;
      }
      else if (percs == 7) {
        d7 += 1;
      }
      else if (percs == 8) {
        d8 += 1;
      }
      else if (percs == 9) {
        d9 += 1;
      }
    }
    double[] pack = {
       d0 /= gens.length,
       d1 /= gens.length,
       d2 /= gens.length,
       d3 /= gens.length,
       d4 /= gens.length,
       d5 /= gens.length,
       d6 /= gens.length,
       d7 /= gens.length,
       d8 /= gens.length,
       d9 /= gens.length
    };
    System.out.println(pack[0]);
    System.out.println(pack[1]);
    System.out.println(pack[2]);
    System.out.println(pack[3]);
    System.out.println(pack[4]);
    System.out.println(pack[5]);
    System.out.println(pack[6]);
    System.out.println(pack[7]);
    System.out.println(pack[8]);
    System.out.println(pack[9]);
  }
}

I know it’s pretty repetitive, but that’s just how I’m dealing with it for now.

Anyway, essentially, it should take those four numbers in the gens array, cut them off to the first digit of each number (i.e. 1, 2, 9 & 1 respectively), then count how many times each digit from 0-9 shows up, so ‘1’ should give 0.5 (50%), 2 should give .25 (25%) and 9 should give .25 (25%) where every other digit from 0-9 should give 0. I get 0.5 for the digit ‘1’, but 0 for every other one, and I haven’t been able to find my mistake. The results I get vary depending on which numbers I input (hardcode), but they’re pretty much always wrong.

Any ideas?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:14:13+00:00Added an answer on June 13, 2026 at 8:14 am

    Change

    double percs = gens[i];
    

    to

    int percs = (int)gens[i];
    

    If you leave it as a double, your while-loop

    while (percs < -9 || 9 < percs) percs /= 10;
    

    will not properly cut off all the digits except the first because it will perform normal division not integer division. You can see this if you print percs after the while-loop.


    I also want to point out that this can be done much more easily. For example:

    double[] gens = {100, 200.1, 9.3, 10};
    
    int[] count = new int[10];
    
    for (double d : gens)
        count[("" + Math.abs(d)).charAt(0) - '0']++;
    
    for (int n : count)
        System.out.println((double)n / gens.length);
    

    will do the same thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can Anyone Tell me why the function is Not Calling Here Class MasterViewController.h @interface
Can't figure out how to do this in a pretty way : I have
Can find why i get this error can someone help? package Android.data; public class
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Can I change the field public virtual ClassOne ClassOne { get; set; } to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can anyone help me as what is going wrong here? Am not able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.