fork download
  1. /* public */ class LinearSearch {
  2. public static void main(String[] args) {
  3. int[] array = {3, 6, 5, 8, 4, 8};
  4. search(array, 8);
  5. search(array, 7);
  6. }
  7. public static void search(int[] array, int n) {
  8. boolean matchFlag = false;
  9. for (int i = 0; i < array.length; i++) {
  10. if (array[i] == n) {
  11. System.out.println("指定された整数" + n + "は" + i + "番目の要素と一致しました");
  12. if (!matchFlag) {
  13. matchFlag = true;
  14. }
  15. }
  16. }
  17. if (!matchFlag) {
  18. System.out.println("指定された整数" + n + "は見つかりませんでした");
  19. }
  20. }
  21. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
指定された整数8は3番目の要素と一致しました
指定された整数8は5番目の要素と一致しました
指定された整数7は見つかりませんでした