亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

bug調試問題

bug調試問題

月亮島Superman 2018-01-13 15:59:59
寫了一個小程序,功能大概是:輸入兩行參數,例如18850 42 9 15 105 63 14 30將下面的50 42 9 15 105 63 14 30按從小到大排序后,從下標為0的元素開始相加,直到相加的和最接近于188返回和。貼上源代碼~~public class InterviewQuestion {?public static void main(String[] args) {??Scanner sc = new Scanner(System.in);??Integer str = Integer.valueOf(sc.nextLine());//第一行參數???String secondStr = sc.nextLine();//第二行參數??String[] split = secondStr.split(" ");????int[] prices = new int[split.length];//將第二次輸入的參數存在prices數組中????for (int i=0;i<split.length;i++) {???Integer temp = Integer.valueOf(split[i]);???prices[i] = temp;??}????//將數組按從小到大排序??Arrays.sort(prices);??int sum = 0;??for(int i=0;i<prices.length;i++) {???if(sum >= str) {????sum = sum - prices[i-1];????break;???}else{???sum = sum + prices[i];???}??}??System.out.println(sum);??sc.close();?}?本來沒什么事情,但是詭異的是,隨手輸入了一組數據185 5 5 5按道理說,應該返回15才對,但是返回了20,代碼我覺得沒什么問題,然后debug了一下詭異的事情發生了for(int i=0;i<prices.length;i++) ? prices.length為4i=3 的情況下 居然沒有進入for循環進行判斷?這是怎么回事,怎么也想不出來問題,求大神幫忙看看。。。
查看完整描述

目前暫無任何回答

  • 0 回答
  • 0 關注
  • 1233 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號