Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
サンプルコード7-2-修正
package testprogramm;
import java.util.Scanner;
class oshiete {
public void livingthing() {
Scanner scan = new Scanner(System.in);
System.out.print("種別を入力してください: ");
String hensuunitsusite = scan.nextLine();
String syubetsu[] = new String[4];
String animal[] = new String[4];
String tousuu[] = new String[4];
syubetsu[0] = "動物";
syubetsu[1] = "動物";
syubetsu[2] = "動物";
syubetsu[3] = "植物";
animal[0] = "キリン";
animal[1] = "ライオン";
animal[2] = "ゴリラ";
animal[3] = "ひまわり";
tousuu[0] = "7頭";
tousuu[1] = "3頭";
tousuu[2] = "5頭";
tousuu[3] = "1本";
boolean found = false;
for (int i = 0; i < 4; i++) {
if (hensuunitsusite.equals(syubetsu[i])) {
System.out.println(animal[i]);
System.out.println(tousuu[i]);
found = true;
}
}
scan.close(); // ここで、Scanner の close をしておく
if (!found) {
System.out.println("種別がありません。再度入力してください。");
livingthing();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.