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

為了賬號安全,請及時綁定郵箱和手機立即綁定

程序哪里有問題,輸出結果不正確。min 5和max10(10,5,19,3)

#include <stdlib.h>

#include <iostream>

using namespace std;


int getMaxOrMin(int *arr, int count, bool isMax)

{

int temp = arr[0];

for (int i = 1; i < count; i++)

{

if (isMax)

{

if (temp < arr[i])

{

temp = arr[i];

}

}

else

{

if (temp > arr[i])

{

temp = arr[i];

}

}

return temp;

}

}

int main(void)

{

bool isMax=false;

int arr1[4] = {10,5,19,3 };

cin >> isMax;

cout << getMaxOrMin(arr1,4, isMax)<<endl;

system("pause");

return 0;


}


正在回答

4 回答

return temp;應該放在for循環之后返回值!你的這個位置只進行一次循環就被終止啦!for循環中的return中斷了循環!下次寫代碼時將中括號對應起來就好看多啦!

0 回復 有任何疑惑可以回復我~
#1

幺怪 提問者

非常感謝!
2016-04-08 回復 有任何疑惑可以回復我~
#2

幺怪 提問者

寫的時候是對齊的,粘貼過來的時候沒注意就成這樣了。
2016-04-08 回復 有任何疑惑可以回復我~

return temp;應該放在for循環之外,這個位置只進行一次循環就被終止啦!for循環中的return中斷了循環!下次寫寫程序的時候要注意層次感,養成一個好的習慣,對以后的學習和編程過程是有百利而無一害的。

0 回復 有任何疑惑可以回復我~

只有一句代碼出錯,就是return temp;此句不應該出現在循環內,而應該出現在循環外,不知道你注意到了沒有?

修改之后程序運行無錯誤。



1 回復 有任何疑惑可以回復我~

// Project01.cpp : 定義控制臺應用程序的入口點。

//

#include <StdAfx.h>

#include <stdlib.h>

#include <iostream>

using namespace std;


int getMaxOrMin(int *arr, int count, bool isMax)

{

int temp = arr[0];

for (int i = 1; i < count; i++)

{

if (isMax)

{

if (temp < arr[i])

{

temp = arr[i];

}

}

else

{

if (temp > arr[i])

{

temp = arr[i];

}

}

}return temp;

}

int main(void)

{

bool isMax=false;

int arr1[4] = {10,5,19,3 };

cin >> isMax;

cout << getMaxOrMin(arr1,4, isMax)<<endl;

system("pause");

return 0;


}


1 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
C++遠征之起航篇
  • 參與學習       290962    人
  • 解答問題       817    個

C++亮點盡在其中,本課程是在C語言基礎上的一個延伸,得以升華

進入課程

程序哪里有問題,輸出結果不正確。min 5和max10(10,5,19,3)

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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