fork download
  1. // list内の最大値・最小値を求める(3)
  2. // list内に最大値・最小値が複数ある場合にも対応
  3.  
  4. class Kadai3_5ch {
  5. public static void main(String[] args) {
  6. int[] list1 = {1, 10, 60, 20, 60}; // 変えました
  7. int[] list2 = {1, -10, 60, 2, 5, 2, -10, -10}; // 変えました
  8.  
  9. // print("はい、まずはここにlist1の最大値(つまり60)を出す処理を作ってね")
  10. int max1 = Integer.MIN_VALUE;
  11. int count1 = 0;
  12. for (int i = 0; i < list1.length; i++) {
  13. max1 = Math.max(list1[i],max1);
  14. if(max1==list1[i]) count1=i;
  15. }
  16. System.out.println("list1の最大値は" + max1 + "、それは" + count1 + "番目");
  17.  
  18. // print("次に、list2の最小値(つまり-10)を出す処理はここにお願いします。")
  19. int min2 = Integer.MAX_VALUE;
  20. int count2 = 0;
  21. for (int i = 0; i < list2.length; i++) {
  22. min2 = Math.min(list2[i],min2);
  23. if(min2==list2[i]) count2=i;
  24. }
  25.  
  26. System.out.println("list2の最小値は" + min2 + "、それは" + count2 + "番目");
  27.  
  28. }
  29. }
Success #stdin #stdout 0.08s 27872KB
stdin
Standard input is empty
stdout
list1の最大値は60、それは4番目
list2の最小値は-10、それは7番目