1

私の最後のステップは、合計の整数パーセントを取得することです。したがって、次のように入力すると、次のようになります。2 1 1 2出力は2になります。これは、合計の33.333%です。1、これは合計の16.666%です。1、これは合計の16.666%です。2、これは合計の33.333%です。

私はアレイに非常に慣れていないので、非常に戸惑っています。ユーザーは任意の量の整数を入力できるため、パーセントを取得する方法がわかりません。整数が2つしかない場合、たとえば2と2の場合、それぞれ50パーセントになります。

import java.util.Scanner;
   public class Integers {
   /* program 7-1*/
      public static void main(String[] args) 
      {
         Scanner keyboard = new Scanner(System.in);
         System.out.println("How many numbers will you enter?");
         int size = keyboard.nextInt();
         int[] entry = new int[size];

         System.out.println("Enter " + entry.length + " integers, one per line:");

         int sum = 0;
         for (int index = 0; index < entry.length; index++)
         {
            entry[index] = keyboard.nextInt();
            sum +=  size;
         }

         System.out.println("The sum is " + sum + "." + "\nThe numbers are:" );         
      }
   }
4

4 に答える 4

2

Now that you have all entries and the sum, you are almost there:

  • You need another loop to go over entries one at a time
  • To calculate percentage, multiply the entry by 100.0, and then divide by the sum. Note the dot zero at the end of 100.0 - it's there on purpose
  • If you are on Java 5 or later, a very convenient way of printing out a number followed by percentage sign is printf. Note, however, that the percent sign % needs to be escaped.
于 2012-07-14T00:24:12.987 に答える
1

What answer are you getting that's incorrect? That's what is puzzling. I looked at your code, but I don't see any division, percentage calculation, or output.

I compiled and ran your code. Here's the output I got. So far, so good. What's wrong? What's your question?

"C:\Program Files\Java\jdk1.7.0_02\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 120.11\bin" com.intellij.rt.execution.application.AppMain cruft.Integers
How many numbers will you enter?

4
Enter 4 integers, one per line:
2
1
1
2
The sum is 16.
The numbers are:

Process finished with exit code 0

Write more code. Be careful to remember that integer division isn't what you want; percentages need to be doubles.

int x = 1/2;  // x will equal zero.  know why?

You've already calculated the sum. You know that if you take in ten numbers, no matter how many or what their values are, the percentage of the sum that each one will represent is the number divided by the sum. Is that what you're asking?

1 hour later:

It's been a whole hour, and you seem to think that writing comments is more educational than actually writing the four lines of code that you need. Okay, I'll bite - here's your solution. I'll risk the wrath of all those who will be outraged by someone who does homework. I want you to see how ridiculous it is that you wouldn't even attempt four lines of code:

import java.util.Scanner;

public class Integers {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("How many numbers will you enter?");
        int size = keyboard.nextInt();
        int[] entry = new int[size];
        System.out.println("Enter " + entry.length + " integers, one per line:");
        int sum = 0;
        for (int index = 0; index < entry.length; index++) {
            entry[index] = keyboard.nextInt();
            sum += entry[index];  // this was wrong - I fixed it.
        }
        // This is all you had to do. 
        for (int anEntry : entry) {
            System.out.println(String.format("value: %d %6.2f%%", anEntry, anEntry * 100.0 / sum));
        }
        System.out.println(String.format("total: %d %6.2f%%", sum, 100.0));
    }
}
于 2012-07-14T00:25:41.613 に答える
0

'''スペースで区切られた不明な数の整数がある場合は、それらを文字列として取り込み、String.split関数を使用できます。ドキュメントはhttp://docs.oracle.com/javase/1.5にあります。 0 / docs / api / java / lang / String.html#split(java.lang.String)-これにより、必要な大きさの配列が得られ、その長さはの長さフィールドに格納されます。アレイ。

于 2012-07-14T00:46:29.417 に答える
0

Since this is not homework, I am not going to give you an answer, but give you an approach that you can figure out to implement (and learn from :-D)

create two arrays of the length the user enters and one counter for the second. the first array will be the one you currently have. The second will store unique values and the counter will represent how many unique values are in that array. When the user enters a value, check to see if its in the current array, if not add it to unique and increment the counter. Then iterate through the unique array at the end of your program performing the calculation.

--cheers

于 2012-07-14T00:23:43.293 に答える