Guten Tag, ich kann die Aufgabe nur zum Teil lösen, wenn ich die Aufgabestellung richtig verstanden habe.
Die Vokale zu zählen ist nicht schwerig, Konsonaten auch nicht bis auf Zahlen und Sonderzeichen.
Da die Zahlen unendlich gross sind. Am besten schaut euch bitte die Aufgabe und meine Lösung an.
Danke.
Aufgabestellung: Erstellen Sie ein Programm, das eine Zeichenkette einliest und dann die Anzahl der Vokale sowie die Anzahl der Konsonanten in der Zeichenkette ermittelt. Als Konsonant dürfen dabei alle Zeichengezählt werden, die kein Vokal sind - also zum Beispiel auch Zahlen und Sonderzeichen.
Berücksichtigen Sie bei der Ermittlung der Vokale und Konsonanten sowohl Groß-als auch Kleinbuchstaben. Benutzen Sie für die Lösung dieser Aufgabe eine switch-Konstruktion.
Mein Lösungsvorschlag:
tippe oder füge den Code hieimport java.util.Scanner;
public class VokalKonsonantZaehler {
public static void main(String[] args) {
int Vocal = 0;
int A = 0, E = 0, I = 0, O = 0, U = 0;
int a = 0, e = 0, i = 0, o = 0, u = 0;
int Kons = 0;
int b = 0, c = 0, d = 0, f = 0, g = 0, h = 0, j = 0, k = 0, l = 0, m = 0, n = 0, p = 0;
int q = 0, r = 0, s = 0, t = 0, v = 0, w = 0, x = 0, y = 0, z = 0, ß = 0;
int B = 0, C = 0, D = 0, F = 0, G = 0, H = 0, J = 0, K = 0, L = 0, M = 0, N = 0, P = 0;
int Q = 0, R = 0, S = 0, T = 0, V = 0, W = 0, X = 0, Y = 0, Z = 0;
System.out.println("Geben Sie einen Satz ein:");
String sentence = new Scanner(System.in).nextLine();
for (int vokal = 0; vokal < sentence.length(); vokal++) {
switch (sentence.charAt(vokal)) {
case 'A': ++A; ++Vocal; break;
case 'E': ++E; ++Vocal; break;
case 'I': ++I; ++Vocal; break;
case 'O': ++O; ++Vocal; break;
case 'U': ++U; ++Vocal; break;
case 'a': ++a; ++Vocal; break;
case 'e': ++e; ++Vocal; break;
case 'i': ++i; ++Vocal; break;
case 'o': ++o; ++Vocal; break;
case 'u': ++u; ++Vocal; break;
}
}
for (int kons = 0; kons < sentence.length(); kons++) {
switch (sentence.charAt(kons)) {
case 'b': ++b; ++Kons; break;
case 'c': ++c; ++Kons; break;
case 'd': ++d; ++Kons; break;
case 'f': ++f; ++Kons; break;
case 'g': ++g; ++Kons; break;
case 'h': ++h; ++Kons; break;
case 'j': ++j; ++Kons; break;
case 'k': ++k; ++Kons; break;
case 'l': ++l; ++Kons; break;
case 'm': ++m; ++Kons; break;
case 'n': ++n; ++Kons; break;
case 'p': ++p; ++Kons; break;
case 'q': ++q; ++Kons; break;
case 'r': ++r; ++Kons; break;
case 's': ++s; ++Kons; break;
case 't': ++t; ++Kons; break;
case 'v': ++v; ++Kons; break;
case 'w': ++w; ++Kons; break;
case 'x': ++x; ++Kons; break;
case 'y': ++y; ++Kons; break;
case 'z': ++z; ++Kons; break;
case 'ß': ++ß; ++Kons; break;
case 'B': ++B; ++Kons; break;
case 'C': ++C; ++Kons; break;
case 'D': ++D; ++Kons; break;
case 'F': ++F; ++Kons; break;
case 'G': ++G; ++Kons; break;
case 'H': ++H; ++Kons; break;
case 'J': ++J; ++Kons; break;
case 'K': ++K; ++Kons; break;
case 'L': ++L; ++Kons; break;
case 'M': ++M; ++Kons; break;
case 'N': ++N; ++Kons; break;
case 'P': ++P; ++Kons; break;
case 'Q': ++Q; ++Kons; break;
case 'R': ++R; ++Kons; break;
case 'S': ++S; ++Kons; break;
case 'T': ++T; ++Kons; break;
case 'V': ++V; ++Kons; break;
case 'W': ++W; ++Kons; break;
case 'X': ++X; ++Kons; break;
case 'Y': ++Y; ++Kons; break;
case 'Z': ++Z; ++Kons; break;
}
}
System.out.println("Anzahl der Vokale: " + Vocal);
System.out.println("A: " + A);
System.out.println("E: " + E);
System.out.println("I: " + I);
System.out.println("O: " + O);
System.out.println("U: " + U);
System.out.println("a: " + a);
System.out.println("e: " + e);
System.out.println("i: " + i);
System.out.println("o: " + o);
System.out.println("u: " + u);
System.out.println("Anzahl der Konsonanten: " + Kons);
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println("d: " + d);
System.out.println("f: " + f);
System.out.println("g: " + g);
System.out.println("h: " + h);
System.out.println("j: " + j);
System.out.println("k: " + k);
System.out.println("l: " + l);
System.out.println("m: " + m);
System.out.println("n: " + n);
System.out.println("p: " + p);
System.out.println("q: " + q);
System.out.println("r: " + r);
System.out.println("s: " + s);
System.out.println("t: " + t);
System.out.println("v: " + v);
System.out.println("w: " + w);
System.out.println("x: " + x);
System.out.println("y: " + y);
System.out.println("z: " + z);
System.out.println("ß: " + ß);
System.out.println("B: " + B);
System.out.println("C: " + C);
System.out.println("D: " + D);
System.out.println("F: " + F);
System.out.println("G: " + G);
System.out.println("H: " + H);
System.out.println("J: " + J);
System.out.println("K: " + K);
System.out.println("L: " + V);
System.out.println("M: " + M);
System.out.println("N: " + N);
System.out.println("P: " + P);
System.out.println("Q: " + Q);
System.out.println("R: " + R);
System.out.println("S: " + S);
System.out.println("T: " + T);
System.out.println("V: " + V);
System.out.println("W: " + W);
System.out.println("X: " + X);
System.out.println("Y: " + Y);
System.out.println("Z: " + Z);
}
}
r ein