String aus 10 Ziffern

discere

Mitglied
Hallo, ich schreibe das Programm: Befehlszeilenargument (ein String aus maximal 10 Ziffern 0-9) mit sieben Zeilen von Hash-Zeichen (#).

Es gibt ein Problem. " Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at ziffern2.Ziffern2.main(Ziffern2.java:188)"
Ich weiß nicht, wie kann ich diese Problem lösen? Danke



Java:
public class Ziffern2 {
   


    public static void main(String[] args) {
       
        int zahl = 22665; //Integer.parseInt(args[0]);
        int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; int f = 0;     
        int g = 0; int h = 0; int i = 0; int j = 0;
        int A = 0; 
            	
    	String[][] s={{
 		   "  ###   ",
 		   " #   #  ",
 		   "#     # ",
 		   "#     # ",
 		   "#     # ",
 		   " #   #  ",
 		   "  ###   "
 		},{
 		   "  #   ",
 		   " ##   ",
 		   "# #   ",
 		   "  #   ",
 		   "  #   ",
 		   "  #   ",
 		   "##### "
 		},{
 		   " #####  ",
 		   "#     # ",
 		   "      # ",
 		   " #####  ",
 		   "#       ",
 		   "#       ",
 		   "####### "
 		},{
 		   " #####  ",
 		   "#     # ",
 		   "      # ",
 		   " #####  ",
 		   "      # ",
 		   "#     # ",
 		   " #####  "
 		},{
 		   "#       ",
 		   "#    #  ",
 		   "#    #  ",
 		   "#    #  ",
 		   "####### ",
 		   "     #  ",
 		   "     #  ",
 		},{
 		   "####### ",
 		   "#       ",
 		   "#       ",
 		   "######  ",
 		   "      # ",
 		   "#     # ",
 		   " #####  "
 		},{
 		   " #####  ",
 		   "#     # ",
 		   "#       ",
 		   "######  ",
 		   "#     # ",
 		   "#     # ",
 		   " #####  ",
 		},{
 		   "####### ",
 		   "#    #  ",
 		   "    #   ",
 		   "   #    ",
 		   "  #     ",
 		   "  #     ",
 		   "  #     "
 		},{
 		   " #####  ",
 		   "#     # ",
 		   "#     # ",
 		   " #####  ",
 		   "#     # ",
 		   "#     # ",
 		   " #####  "
 		},{
 		   " #####  ",
 		   "#     # ",
 		   "#     # ",
 		   " ###### ",
 		   "      # ",
 		   "#     # ",
 		   " #####  "
 		}};
        
        
            if (zahl > 0) {

              j = zahl % 10;
              zahl = zahl/10;
              A++;
            }
        
                if (zahl > 0) {

                  i = zahl % 10;
                  zahl = zahl/10;
                  A++;
                }

                    if (zahl > 0) {

                      h = zahl % 10;
                      zahl = zahl/10;
                      A++;
                    }

                        if (zahl > 0) {

                          g = zahl % 10;
                          zahl = zahl/10;
                          A++;
                       }
                        
                         if (zahl > 0) {

                            f = zahl % 10;
                            zahl = zahl/10;
                            A++;
                         }
                         
                            if (zahl > 0) {

                                e = zahl % 10;
                                zahl = zahl/10;
                                A++;
                             }
                            
                                if (zahl > 0) {

                                    d = zahl % 10;
                                    zahl = zahl/10;
                                    A++;
                                 }
                                
                                if (zahl > 0) {

                                    c = zahl % 10;
                                    zahl = zahl/10;
                                    A++;
                                 }
                                
                                    if (zahl > 0) {

                                        b = zahl % 10;
                                        zahl = zahl/10;
                                        A++;
                                     }
                                    
                                    if (zahl > 0) {

                                        a = zahl % 10;
                                        zahl = zahl/10;
                                        A++;
                                     }
                                
  
		for (int y = 0; y < 8; ++y) {
                        
                        if (A == 1) {
                        System.out.println(s[j][y]);
                        }
                        
                        else if (A == 2) {
                        System.out.println(s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 3) {
                        System.out.println(s[h][y]+s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 4) {
                        System.out.println(s[g][y]+s[h][y]+s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 5) {
                        System.out.println(s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 6) {
                        System.out.println(s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 7) {
                        System.out.println(s[e][y]+s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 8) {
                        System.out.println(s[e][y]+s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 9) {
                        System.out.println(s[b][y]+s[c][y]+s[d][y]+s[e][y]+s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
                        }
                        
                        else if (A == 10) {
                        System.out.println(s[a][y]+s[b][y]+s[c][y]+s[d][y]+s[e][y]+s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
                        }
		}
    
        }
 } //End of main
 
Zuletzt bearbeitet:
S

SlaterB

Gast
eine rote Linie ist eine Linie in einer gewissen Farbe, rot

ohne dein erstaunliches Programm genau zu analysieren fällt auf, dass das Array für die 1 relativ knapp gehalten ist,
geht es schon (zumindest hinsichtlich dieses Fehlers) wenn du da bisschen mit Leerzeichen auffüllst so dass es so breit ist wie die anderen?

edit:
alle Arrays haben auch nur 7 Zeilen, ist es falsch dass deine Schleife
> for (int y = 0; y < 8; ++y)
bis zur 8 läuft, also Indexe 0 bis 7 = 8 Indexe?
 
Zuletzt bearbeitet von einem Moderator:

discere

Mitglied
noch eine Frage. Ich überlege, ich kann ein Programm kürzen???

Java:
            if (zahl > 0) {

              j = zahl % 10;
              zahl = zahl/10;
              A++;
            }
        
                if (zahl > 0) {

                  i = zahl % 10;
                  zahl = zahl/10;
                  A++;
                }

                    if (zahl > 0) {

                      h = zahl % 10;
                      zahl = zahl/10;
                      A++;
                    }

                        if (zahl > 0) {

                          g = zahl % 10;
                          zahl = zahl/10;
                          A++;
                       }
                        
                         if (zahl > 0) {

                            f = zahl % 10;
                            zahl = zahl/10;
                            A++;
                         }
                         
                            if (zahl > 0) {

                                e = zahl % 10;
                                zahl = zahl/10;
                                A++;
                             }
                            
                                if (zahl > 0) {

                                    d = zahl % 10;
                                    zahl = zahl/10;
                                    A++;
                                 }
                                
                                if (zahl > 0) {

                                    c = zahl % 10;
                                    zahl = zahl/10;
                                    A++;
                                 }
                                
                                    if (zahl > 0) {

                                        b = zahl % 10;
                                        zahl = zahl/10;
                                        A++;
                                     }
                                    
                                    if (zahl > 0) {

                                        a = zahl % 10;
                                        zahl = zahl/10;
                                        A++;
                                     }

While? Ich habe versucht. Ich habe gemerkt, mein Programm ist schon lang. Kann man kürzen?
 
Zuletzt bearbeitet:
S

SlaterB

Gast
dein Programm enthält noch paar Fehler
Java:
    else if (A == 5) {
           System.out.println(s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
    }
                        
    else if (A == 6) {
          System.out.println(s[f][y]+s[g][y]+s[h][y]+s[i][y]+s[j][y]);
    }
für A==6 werden auch nur 5 Ziffern ausgegeben, im Folgenden noch mehr unvollständig

kürzer geht es einerseits mit einem Array für die Ziffern statt a, b, c usw.
und noch der Technik System.out.print() statt println() bzw. notfalls eine Zeile als String mit + zusammenbauen,
nicht alle A unterscheiden und dann jeweils so viele Teile wie nötig ausgeben

kleines a statt groß A wäre noch besser, keine Variablen groß schreiben, aber ich belasse es mal dabei:

Java:
        int zahl = 991060; // Integer.parseInt(args[0]);
        int[] z = new int[30];
        int A = -1;
        while (zahl > 0)
        {
            A++;
            z[A] = zahl % 10;
            zahl = zahl / 10;
        }


        for (int y = 0; y < 7; ++y)
        {
            for (int x = A; x >= 0; x--)
            {
                System.out.print(s[z[x]][y]);
            }
            System.out.println();

        }
 

ARadauer

Top Contributor
Jedes mal wenn du im Code Strg+C Strg+V machst stirbt irgendwo auf der Welt ein kleines Kätzchen! Denk dran!

if (zahl > 0) {

c = zahl % 10;
zahl = zahl/10;
A++;
}
du machst immer das selbe..
der einzige unterschied ist hier das du das ergebnis von zahl % 10 in eine andere variable schreibst...
wenn du statt a, b, c,... ein array nimmst, kannst du das ein einer schleife schreiben...
 

Neue Themen


Oben