开发者

Adding a while and for loop

开发者 https://www.devze.com 2023-02-26 05:13 出处:网络
The question asks: Design and implement an application that first reads a list of 10 three-digit integers and then counts the number of appearances for each digit from 0 to 9.

The question asks: Design and implement an application that first reads a list of 10 three-digit integers and then counts the number of appearances for each digit from 0 to 9.

Here is an example of 3 three-digit numbers

Input number [123, 456, 789]

Output:

Digit 0 has appeared 0 times

Digit 1 has appeared 1 times

Digit 2 has appeared 1 times

Digit 9 has appeared 1 times

I believe that I have figured out the correct formulas to computate the amount of times each number appears, but I am not sure how to create the array and begin searching through it. I think I need a while and a for loop, but I am not sure how to incorporate them. I am afraid however, that my current if statements will need to change once the loops are implemented. Am I going in the right direction here? Any help would be greatly appreciated!

double i1, i2, i3, i4, i5, i6, i7, i8, i9, i10;
int c0=0, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0, c7=0, c8=0, c9=0;

Scanner scan = new Scanner(System.in);

System.out.println ("Enter 10 3-digit integers");

//Counts the 1st number
System.out.println ("Enter first 3-digit integer");
i1 = scan.nextDouble();

    if (i1%10==0)
    c0++;
    if (i1%10==1)
    c1++;
    if (i1%10==2)
    c2++;
    if (i1%10==3)
    c3++;
    if (i1%10==4)
    c4++;
    if (i1%10==5)
    c5++;
    if (i1%10==6)
    c6++;
    if (i1%10==7)
    c7++;
    if (i1%10==8)
    c8++;
    if (i1%10==9)
    c9++;

    if ((i1%100>=0) & (i1%100<10))
    c0++;
    if ((i1%100>=10) & (i1%100<20))
    c1++;
    if ((i1%100>=20) & (i1%100<30))
    c2++;
    if ((i1%100>=30) & (i1%100<40))
    c3++;
    if ((i1%100>=40) & (i1%100<50))
    c4++;
    if ((i1%100>=50) & (i1%100<60))
    c5++;
    if ((i1%100>=60) & (i1%100<70))
    c6++;
    if ((i1%100>=70) & (i1%100<80))
    c7++;
    if ((i1%100>=80) & (i1%100<90))
    c8++;
    if ((i1%100>=90) & (i1%100<100))
    c9++;


    if((i1/1000>=.0) & (i1/1000<.1))
    c0++;
    if((i1/1000>=.1) & (i1/1000<.2))
    c1++;
    if((i1/1000>=.2) & (i1/1000<.3))
    c2++;
    if((i1/1000>=.3) & (i1/1000<.4))
    c3++;
    if((i1/1000>=.4) & (i1/1000<.5))
    c4++;
    if((i1/1000>=.5) & (i1/1000<.6))
    c5+开发者_如何转开发+;
    if((i1/1000>=.6) & (i1/1000<.7))
    c6++;
    if((i1/1000>=.7) & (i1/1000<.8))
    c7++;
    if((i1/1000>=.8) & (i1/1000<.9))
    c8++;
    if((i1/1000>=.9) & (i1/1000<1.00))
    c9++;


Hints:

  1. Get rid of all of the c<n> and i<n> declarations and all of the if tests. They don't help you solve the problem.
  2. Make use of the fact that '0' + <n> == '<n>' where <n> is 0 .. 9.
  3. Or use Character.digit(char, 10). Read the javadocs.
  4. You don't use nextDouble() to read an integer. Read the javadocs.


More Hints:

  1. Generalise the problem to 'count the distinct characters in a string'. Since you're reading the input as a string anyway, just forget that you're counting integers for the moment, and focus on counting char appearances within a string.

  2. Think of a data structure that can contain a pair of (distinct char, count).

  3. Iterate through the characters within a string, add the character to your data structure in point 2 if it doesn't exist with a default value of 0, then increment the count of the char you're currently looking at.

  4. When showing the output of your program, iterate through the entries of your data structure, validate that the char is an integer (if validation is important), and return the count for that char.

I'm not sure if you have trouble with iteration or not, but here is how to loop through the characters of a string:

String s = "123 456 789";

for (int i = 0; i < s.length(); i++){
    char c = s.charAt(i);
    // process c
}

// or, if you know what 'smart loops' in Java are...

for(char c : s.toCharArray()) {
    // process c
}

To confuse matters, here it is in... 'pseudocode' (edited based on comments) ;)

>>> s = "012 345 678 900"
>>> datastructure = {}
>>> for c in s:
...     if c not in datastructure:
...         datastructure[c] = 0
...     datastructure[c] += 1
... 
>>> for c in '0123456789':
...     if c not in datastructure:
...         datastructure[c] = 0
...     print 'Digit %s appeared %d times' % (c, datastructure[c])
... 
Digit 0 appeared 3 times
Digit 1 appeared 1 times
Digit 2 appeared 1 times
Digit 3 appeared 1 times
Digit 4 appeared 1 times
Digit 5 appeared 1 times
Digit 6 appeared 1 times
Digit 7 appeared 1 times
Digit 8 appeared 1 times
Digit 9 appeared 1 times


Your idea isn't that bad, you just obviously need some loops :)

1: loop to get numbers - you can't duplicate the same code 10x - write it once, in a loop.

2: loop to check digits - you can use your's ifs, or do it as Josh said but before - you need data structure to store your data - occurs of digits. 10 variables is bad, BAD:), idea.
use array[2][10] or simply array[10] (where number of digits = index of array and value = occurs) or even better - HashMap<> - google it.

then inside loop you do:

for(int i = 0; i<10; i++){
    if (myCurrentNumberToCheck %10 == i)
        array[i] = array[i] + 1; // approach with simple array[10]
    if ((myCurrentNumberToCheck %100 >= i*10) && (myCurrentNumberToCheck %100 < (i+1)*10 )) // needs to be tested
        array[i] = array[i] + 1;
    if ((myCurrentNumberToCheck %1000 >= i*100) && (myCurrentNumberToCheck %1000 < (i+1)*100 )) // needs to be tested
        array[i] = array[i] + 1;
}

ahh and your inserted numbers should be in some structure too - try List numbers = new ArrayList<int>() here :) and iterate thou list, to take a look at next number.

so you will need to add numbers to List, and nextly - go thou that list and check them and here use that loop i wrote above


the problem with readInt() and readDouble() is that it ignores leading 0 and trailing 0 after the decimal i.e. 01.10 => 1.1

A much simpler approach is to just read every character.

  int ch;
  while((ch = System.in.read())>=0) {
      // you have one character

  }
0

精彩评论

暂无评论...
验证码 换一张
取 消