Languages

Java - tablica z cyframi od 00 do 99

3 points
Asked by:
Nikki
1659

Chcę zrobić tablicę z liczbami od 00 do 99 i wyprintować je potem w rzędach 

00, 01, ..., 09,

10, 11, ..., 19,

20, 21, ..., 29,

...

90, 91, ..., 99,

Mój aktualny kod:

public class TablicaFrom0To99 {

    public static void main(String[] args) {

        int[] cyfra = new int[100];

        for (int i = 0; i < cyfra.length; i++) {
            cyfra[i] = i;
        }
        for (int i = 0; i < cyfra.length; i++) {

            if (i < 10) {
                System.out.println("0" + cyfra[i] + ", ");
            } else {
                System.out.println(cyfra[i] + ", ");
            }
            if (i >10 && (i + 1) %10 ==0) {
                System.out.println();
            }
        }
    }
}

Aktualny wynik z konsoli:

00, 
01, 
02, 
03, 
04, 
05, 
06, 
07, 
08, 
09,
 
10, 
11, 
12, 
13, 
14, 
15, 
16, 
17, 
18, 
19, 

...

90, 
91, 
92, 
93, 
94, 
95, 
96, 
97, 
98, 
99, 

 

1 answers
3 points
Answered by:
Nikki
1659

Potrzebna jest zmiana w 3 miejscach:

public class TablicaFrom0To99 {

    public static void main(String[] args) {

        int[] cyfra = new int[100];

        for (int i = 0; i < cyfra.length; i++) {
            cyfra[i] = i;
        }
        for (int i = 0; i < cyfra.length; i++) {

            if (i < 10) {
                System.out.print("0" + cyfra[i] + ", "); //zmienić na "print" zamiast "println"
            } else {
                System.out.print(cyfra[i] + ", ");     //zmienić na "print" zamiast "println"
            }
            if ((i + 1) %10 ==0) {                 //usunąć pierwszy warunek "i >10 &&"
                System.out.println();
            }
        }
    }
}

Wynik końcowy z konsoli:

00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,

 

0 comments Add comment
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.