Rev | Line | |
---|
[2] | 1 | import java.util.Scanner;
|
---|
| 2 |
|
---|
[6] | 3 | /** Sucht eine Zahl in einem Feld */
|
---|
[2] | 4 | public class SucheZahl {
|
---|
| 5 |
|
---|
[6] | 6 | /* einzige Methode der Klasse */
|
---|
[2] | 7 | public static void main(String[] arg) {
|
---|
| 8 | int[][] arr = {{1, 2, 3},
|
---|
| 9 | {4, 5, 6, 7},
|
---|
| 10 | {8, 9, 10, 11, 12}};
|
---|
| 11 |
|
---|
| 12 | Scanner sc = new Scanner(System.in);
|
---|
| 13 | System.out.print("Geben Sie eine Zahl ein: ");
|
---|
| 14 | int zahl = sc.nextInt();
|
---|
| 15 | boolean gefunden = false;
|
---|
| 16 | int i = 0, j = 0;
|
---|
| 17 | suche: for (i=0; i<arr.length; i++) {
|
---|
| 18 | for (j=0; j<arr[i].length; j++) {
|
---|
| 19 | if (arr[i][j] == zahl) {
|
---|
| 20 | gefunden = true;
|
---|
| 21 | break suche;
|
---|
| 22 | }
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 | if (gefunden)
|
---|
| 26 | System.out.println("Zahl " + zahl + " gefunden:" +
|
---|
| 27 | " arr[" + i + "][" + j + "]");
|
---|
| 28 | else
|
---|
| 29 | System.out.println("Zahl " + zahl +
|
---|
| 30 | " ist nicht gefunden");
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.