Javakurs/Übungsaufgaben/DDDText/Musterloesung: Unterschied zwischen den Versionen
< Javakurs | Übungsaufgaben | DDDText
(alles neu, macht der maerz) |
(Musterlösung hinzugefügt) |
||
| (Eine dazwischenliegende Version von einem anderen Benutzer wird nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
| + | == Lösung: DDDText.java == | ||
| + | |||
| + | <pre> | ||
| + | public class DDDText { | ||
| + | |||
| + | public static final String[][] letters = { | ||
| + | {" ___ ", | ||
| + | " / \\ ", | ||
| + | " / ^ \\ ", | ||
| + | " / /_\\ \\ ", | ||
| + | " / _____ \\ ", | ||
| + | "/__/ \\__\\ ", | ||
| + | " "}, | ||
| + | |||
| + | {".______ ", | ||
| + | "| _ \\ ", | ||
| + | "| |_) | ", | ||
| + | "| _ < ", | ||
| + | "| |_) | ", | ||
| + | "|______/ ", | ||
| + | " "}, | ||
| + | |||
| + | {" ______ ", | ||
| + | " / |", | ||
| + | "| ,----'", | ||
| + | "| | ", | ||
| + | "| `----.", | ||
| + | " \\______|", | ||
| + | " "}, | ||
| + | |||
| + | {" _______ ", | ||
| + | "| \\ ", | ||
| + | "| .--. |", | ||
| + | "| | | |", | ||
| + | "| '--' |", | ||
| + | "|_______/ ", | ||
| + | " "}, | ||
| + | |||
| + | {" _______ ", | ||
| + | "| ____|", | ||
| + | "| |__ ", | ||
| + | "| __| ", | ||
| + | "| |____ ", | ||
| + | "|_______|", | ||
| + | " "}, | ||
| + | |||
| + | {" _______ ", | ||
| + | "| ____|", | ||
| + | "| |__ ", | ||
| + | "| __| ", | ||
| + | "| | ", | ||
| + | "|__| ", | ||
| + | " "}, | ||
| + | |||
| + | {" _______ ", | ||
| + | " / _____|", | ||
| + | "| | __ ", | ||
| + | "| | |_ | ", | ||
| + | "| |__| | ", | ||
| + | " \\______| ", | ||
| + | " "}, | ||
| + | |||
| + | {" __ __ ", | ||
| + | "| | | | ", | ||
| + | "| |__| | ", | ||
| + | "| __ | ", | ||
| + | "| | | | ", | ||
| + | "|__| |__| ", | ||
| + | " "}, | ||
| + | |||
| + | {" __ ", | ||
| + | "| | ", | ||
| + | "| | ", | ||
| + | "| | ", | ||
| + | "| | ", | ||
| + | "|__| ", | ||
| + | " "}, | ||
| + | |||
| + | {" __ ", | ||
| + | " | | ", | ||
| + | " | | ", | ||
| + | ".--. | | ", | ||
| + | "| `--' | ", | ||
| + | " \\______/ ", | ||
| + | " "}, | ||
| + | |||
| + | {" __ ___ ", | ||
| + | "| |/ / ", | ||
| + | "| ' / ", | ||
| + | "| < ", | ||
| + | "| . \\ ", | ||
| + | "|__|\\__\\ ", | ||
| + | " "}, | ||
| + | |||
| + | {" __ ", | ||
| + | "| | ", | ||
| + | "| | ", | ||
| + | "| | ", | ||
| + | "| `----.", | ||
| + | "|_______|", | ||
| + | " "}, | ||
| + | |||
| + | {".___ ___. ", | ||
| + | "| \\/ | ", | ||
| + | "| \\ / | ", | ||
| + | "| |\\/| | ", | ||
| + | "| | | | ", | ||
| + | "|__| |__| ", | ||
| + | " "}, | ||
| + | |||
| + | {".__ __. ", | ||
| + | "| \\ | | ", | ||
| + | "| \\| | ", | ||
| + | "| . ` | ", | ||
| + | "| |\\ | ", | ||
| + | "|__| \\__| ", | ||
| + | " "}, | ||
| + | |||
| + | {" ______ ", | ||
| + | " / __ \\ ", | ||
| + | "| | | | ", | ||
| + | "| | | | ", | ||
| + | "| `--' | ", | ||
| + | " \\______/ ", | ||
| + | " "}, | ||
| + | |||
| + | {".______ ", | ||
| + | "| _ \\ ", | ||
| + | "| |_) | ", | ||
| + | "| ___/ ", | ||
| + | "| | ", | ||
| + | "| _| ", | ||
| + | " "}, | ||
| + | |||
| + | {" ______ ", | ||
| + | " / __ \\ ", | ||
| + | "| | | | ", | ||
| + | "| | | | ", | ||
| + | "| `--' '--. ", | ||
| + | " \\_____\\_____\\", | ||
| + | " "}, | ||
| + | |||
| + | {".______ ", | ||
| + | "| _ \\ ", | ||
| + | "| |_) | ", | ||
| + | "| / ", | ||
| + | "| |\\ \\----.", | ||
| + | "| _| `._____|", | ||
| + | " "}, | ||
| + | |||
| + | {" _______.", | ||
| + | " / |", | ||
| + | " | (----`", | ||
| + | " \\ \\ ", | ||
| + | ".----) | ", | ||
| + | "|_______/ ", | ||
| + | " "}, | ||
| + | |||
| + | {".___________.", | ||
| + | "| |", | ||
| + | "`---| |----`", | ||
| + | " | | ", | ||
| + | " | | ", | ||
| + | " |__| ", | ||
| + | " "}, | ||
| + | |||
| + | {" __ __ ", | ||
| + | "| | | | ", | ||
| + | "| | | | ", | ||
| + | "| | | | ", | ||
| + | "| `--' | ", | ||
| + | " \\______/ ", | ||
| + | " "}, | ||
| + | |||
| + | {"____ ____ ", | ||
| + | "\\ \\ / / ", | ||
| + | " \\ \\/ / ", | ||
| + | " \\ / ", | ||
| + | " \\ / ", | ||
| + | " \\__/ ", | ||
| + | " "}, | ||
| + | |||
| + | {"____ __ ____ ", | ||
| + | "\\ \\ / \\ / / ", | ||
| + | " \\ \\/ \\/ / ", | ||
| + | " \\ / ", | ||
| + | " \\ /\\ / ", | ||
| + | " \\__/ \\__/ ", | ||
| + | " "}, | ||
| + | |||
| + | {"___ ___ ", | ||
| + | "\\ \\ / / ", | ||
| + | " \\ V / ", | ||
| + | " > < ", | ||
| + | " / . \\ ", | ||
| + | "/__/ \\__\\ ", | ||
| + | " "}, | ||
| + | |||
| + | {"____ ____ ", | ||
| + | "\\ \\ / / ", | ||
| + | " \\ \\/ / ", | ||
| + | " \\_ _/ ", | ||
| + | " | | ", | ||
| + | " |__| ", | ||
| + | " "}, | ||
| + | |||
| + | {" ________ ", | ||
| + | "| / ", | ||
| + | "`---/ / ", | ||
| + | " / / ", | ||
| + | " / /----.", | ||
| + | " /________|"} | ||
| + | |||
| + | }; | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | String input; | ||
| + | if (args.length == 0) { | ||
| + | System.out.println("Warnung: Keine Parameter übergeben!"); | ||
| + | input = "Hello World"; | ||
| + | } else { | ||
| + | input = args[0]; | ||
| + | } | ||
| + | |||
| + | |||
| + | String inputLowerCase = input.toLowerCase(); | ||
| + | |||
| + | //Variante 1: Ein Buchstabe pro Zeile | ||
| + | for (int i = 0; i < inputLowerCase.length(); i++) { | ||
| + | int index = inputLowerCase.charAt(i) - 'a'; | ||
| + | if (index >= 0 && index <= 25) { | ||
| + | for (int line = 0; line < letters[index].length; line++) { | ||
| + | System.out.println(letters[index][line]); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | //Variante 2: Alle Buchstaben in einer Zeile | ||
| + | for (int line = 0; line < letters[0].length; line++) { | ||
| + | for (int i = 0; i < inputLowerCase.length(); i++) { | ||
| + | int index = inputLowerCase.charAt(i) - 'a'; | ||
| + | if (index >= 0 && index <= 25) { | ||
| + | System.out.print(letters[index][line] + " "); | ||
| + | } | ||
| + | if (inputLowerCase.charAt(i) == ' ') { | ||
| + | System.out.print(" "); | ||
| + | } | ||
| + | } | ||
| + | System.out.println(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | == Alternativer Lösungsvorschlag ohne Verwendung der Vorgabe == | ||
| + | |||
<pre> | <pre> | ||
/** | /** | ||
Aktuelle Version vom 9. März 2012, 11:54 Uhr
Lösung: DDDText.java
public class DDDText {
public static final String[][] letters = {
{" ___ ",
" / \\ ",
" / ^ \\ ",
" / /_\\ \\ ",
" / _____ \\ ",
"/__/ \\__\\ ",
" "},
{".______ ",
"| _ \\ ",
"| |_) | ",
"| _ < ",
"| |_) | ",
"|______/ ",
" "},
{" ______ ",
" / |",
"| ,----'",
"| | ",
"| `----.",
" \\______|",
" "},
{" _______ ",
"| \\ ",
"| .--. |",
"| | | |",
"| '--' |",
"|_______/ ",
" "},
{" _______ ",
"| ____|",
"| |__ ",
"| __| ",
"| |____ ",
"|_______|",
" "},
{" _______ ",
"| ____|",
"| |__ ",
"| __| ",
"| | ",
"|__| ",
" "},
{" _______ ",
" / _____|",
"| | __ ",
"| | |_ | ",
"| |__| | ",
" \\______| ",
" "},
{" __ __ ",
"| | | | ",
"| |__| | ",
"| __ | ",
"| | | | ",
"|__| |__| ",
" "},
{" __ ",
"| | ",
"| | ",
"| | ",
"| | ",
"|__| ",
" "},
{" __ ",
" | | ",
" | | ",
".--. | | ",
"| `--' | ",
" \\______/ ",
" "},
{" __ ___ ",
"| |/ / ",
"| ' / ",
"| < ",
"| . \\ ",
"|__|\\__\\ ",
" "},
{" __ ",
"| | ",
"| | ",
"| | ",
"| `----.",
"|_______|",
" "},
{".___ ___. ",
"| \\/ | ",
"| \\ / | ",
"| |\\/| | ",
"| | | | ",
"|__| |__| ",
" "},
{".__ __. ",
"| \\ | | ",
"| \\| | ",
"| . ` | ",
"| |\\ | ",
"|__| \\__| ",
" "},
{" ______ ",
" / __ \\ ",
"| | | | ",
"| | | | ",
"| `--' | ",
" \\______/ ",
" "},
{".______ ",
"| _ \\ ",
"| |_) | ",
"| ___/ ",
"| | ",
"| _| ",
" "},
{" ______ ",
" / __ \\ ",
"| | | | ",
"| | | | ",
"| `--' '--. ",
" \\_____\\_____\\",
" "},
{".______ ",
"| _ \\ ",
"| |_) | ",
"| / ",
"| |\\ \\----.",
"| _| `._____|",
" "},
{" _______.",
" / |",
" | (----`",
" \\ \\ ",
".----) | ",
"|_______/ ",
" "},
{".___________.",
"| |",
"`---| |----`",
" | | ",
" | | ",
" |__| ",
" "},
{" __ __ ",
"| | | | ",
"| | | | ",
"| | | | ",
"| `--' | ",
" \\______/ ",
" "},
{"____ ____ ",
"\\ \\ / / ",
" \\ \\/ / ",
" \\ / ",
" \\ / ",
" \\__/ ",
" "},
{"____ __ ____ ",
"\\ \\ / \\ / / ",
" \\ \\/ \\/ / ",
" \\ / ",
" \\ /\\ / ",
" \\__/ \\__/ ",
" "},
{"___ ___ ",
"\\ \\ / / ",
" \\ V / ",
" > < ",
" / . \\ ",
"/__/ \\__\\ ",
" "},
{"____ ____ ",
"\\ \\ / / ",
" \\ \\/ / ",
" \\_ _/ ",
" | | ",
" |__| ",
" "},
{" ________ ",
"| / ",
"`---/ / ",
" / / ",
" / /----.",
" /________|"}
};
public static void main(String[] args) {
String input;
if (args.length == 0) {
System.out.println("Warnung: Keine Parameter übergeben!");
input = "Hello World";
} else {
input = args[0];
}
String inputLowerCase = input.toLowerCase();
//Variante 1: Ein Buchstabe pro Zeile
for (int i = 0; i < inputLowerCase.length(); i++) {
int index = inputLowerCase.charAt(i) - 'a';
if (index >= 0 && index <= 25) {
for (int line = 0; line < letters[index].length; line++) {
System.out.println(letters[index][line]);
}
}
}
//Variante 2: Alle Buchstaben in einer Zeile
for (int line = 0; line < letters[0].length; line++) {
for (int i = 0; i < inputLowerCase.length(); i++) {
int index = inputLowerCase.charAt(i) - 'a';
if (index >= 0 && index <= 25) {
System.out.print(letters[index][line] + " ");
}
if (inputLowerCase.charAt(i) == ' ') {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Alternativer Lösungsvorschlag ohne Verwendung der Vorgabe
/**
* This little java programm prints a given text in ascii-Art.
* Start the programm with:
* # java DDDText <Place you text here>
*
* e.g.
* # java DDDText Java
*
* @author tkroenert
*
*
* Warning: This programm is written on a little higher
* programming level as you are on the first day learing
* java. So you programm can be much simpler.
*
*/
public class DDDText {
public static void main(String[] arguments /*[0] is the message we want to print out */) {
/*
* This is for the case you do not give the programm a parameter
*/
if (arguments.length == 0) {
System.out.println("Start the programm with:");
System.out.println("\t# java DDDText <Place you text here>\n");
System.out.println("e.g.");
System.out.println("\t# java DDDText Java");
System.out.println("");
}
/*
* Here begins the formatted output of the 3D-Text.
*
* Test on your own how it works. Trial & error is
* the best way.
*/
//Userfriendly case-ignoring trick
arguments[0] = arguments[0].toLowerCase();
//take a character...
for (int i = 0; i < arguments[0].length(); i++) {
char nextChar = arguments[0].charAt(i);
//...and have a look at it
switch(nextChar)
{
case 'a':
System.out.println(" ___ ");
System.out.println(" / /\\ ");
System.out.println(" / /::\\ ");
System.out.println(" / /:/\\:\\ ");
System.out.println(" / /:/~/::\\ ");
System.out.println(" /__/:/ /:/\\:\\ ");
System.out.println(" \\ \\:\\/:/__\\/");
System.out.println(" \\ \\::/ ");
System.out.println(" \\ \\:\\ ");
System.out.println(" \\ \\:\\ ");
System.out.println(" \\__\\/ "); break;
/*
* case 'b': ...
* case 'c': ...
* case 'd': ...
* etc.
*/
case 'j':
System.out.println(" ___ ");
System.out.println(" / /\\ ");
System.out.println(" / /:/ ");
System.out.println(" /__/::\\ ");
System.out.println(" \\__\\/\\:\\ ");
System.out.println(" \\ \\:\\ ");
System.out.println(" \\__\\:\\ ");
System.out.println(" / /:/ ");
System.out.println(" /__/:/ ");
System.out.println(" \\__\\/ "); break;
case 'v':
System.out.println(" ___ ");
System.out.println(" /__/\\ ");
System.out.println(" \\ \\:\\ ");
System.out.println(" \\ \\:\\ ");
System.out.println(" _ _ \\__\\:\\ ");
System.out.println(" /__/\\ | |:| ");
System.out.println(" \\ \\:\\| |:|");
System.out.println(" \\ \\:\\__|:| ");
System.out.println(" \\__\\::::/ ");
System.out.println(" ~~~~ "); break;
default:
System.out.println(nextChar); break; //this would never happen
}
//some ascii-cosmetics
System.out.println("");
}
}
}