結對程式設計——關於Fault、Error、Failure程式設計
阿新 • • 發佈:2019-02-09
1 package com.sicnu.findnumber; 2 3 public class FindLastNumber { 4 5 public static void main(String[] args) { 6 //找出目標值aim在陣列最後出現的位置,若在陣列中不存在則表示-1 7 int array[] = {2}; 8 int aim = 2; 9 int pos = -1; 10 11 if(array.length <= 1){ 12 if(aim == array[0]){ 13 pos = 1; 14 }else{ 15 pos = -1; 16 } 17 18 }else{ 19 pos = findLast(aim,array); 20 21 } 22 23 for(int i = 0; i < array.length; i++){ 24 System.out.print(array[i] + " ");25 } 26 System.out.println(); 27 System.out.println("find :"+ aim +" The last position:"+pos); 28 29 30 } 31 32 33 /** 34 * @param aim 35 * @param array 36 * @return 37 */ 38 public static int findLast(int aim, int array[]){39 int position = -1; 40 for(int i = 1; i < array.length ;i++){//wrong! 41 if(array[i] == aim){ 42 position = i; 43 } 44 } 45 46 return position + 1; 47 48 } 49 50 }