Java :: Aufgabe #28 :: Lösung #1

7 Lösungen Lösungen öffentlich
#28

Text abwechselnd in Groß- und Kleinschreibung

Anfänger - Java von Dome - 29.12.2012 um 01:34 Uhr
Schreiben Sie ein Programm, welches einen eingegeben Text so manipuliert, das der Text abwechselnd in Groß- und Kleinschreibung auf den Bildschirm ausgegeben wird.

Konsolenausgabe:

Texteingabe: Beispieltext
Textausgabe: BeIsPiElTeXt
#1
vote_ok
von 0 (0 Punkte) - 10.08.2013 um 15:26 Uhr
Main.java
Quellcode ausblenden Java-Code
package de.trainyourprogrammer.java28;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Print a requested word in upper and lower case letters alternating.
 * 
 * @author jsb
 */
public class Main {

	/**
	 * Execute the program.
	 * 
	 * @param args
	 *            Is ignored in our case.
	 */
	public static void main(String[] args) {
		BufferedReader buffer = new BufferedReader(new InputStreamReader(
				System.in)); // start reading from the default input

		System.out.print("Texteingabe: "); // request a word

		try { // try to...
			String word = buffer.readLine(); // read word

			System.out.print("Textausgabe: ");

			for (int i = 0; i < word.length(); i++) { // for each character in
														// the word...
				if (i % 2 == 0) { // ...print the even chars...
					System.out.print(word.toUpperCase().charAt(i)); // upper
																	// case
				} else { // print the odd chars...
					System.out.print(word.toLowerCase().charAt(i)); // lower
																	// case
				}
			}
		} catch (IOException e) { // ignore occurring IOExceptions
		} finally { // on quit...
			try { // try to...
				buffer.close(); // stop reading the input
			} catch (IOException e) { // again ignore occurringIOExceptions
			}
		}
	}
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben