iT邦幫忙

0

請問如何將我的程式修改成能持續輸入數字直到0停止,然後輸出自己所輸入的最大數字?



int main() {
	
	float a;
	
	printf("Enter a number (enter 0 to end): ");
	scanf("%f", &a);
	while(a!=0){
	
			
	printf("Enter a number (enter 0 to end): ");
	scanf("%f", a);
	
}
	printf("The largest number entered was %f", a);
	return 0;
}
米歐 iT邦新手 3 級 ‧ 2020-10-10 21:08:07 檢舉
弄個陣列把所有輸入的值存起來,離開迴圈時從裡面找最大的
ktro12 iT邦新手 5 級 ‧ 2020-10-10 23:02:07 檢舉
怎麼找最大的啊?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
海綿寶寶
iT邦大神 1 級 ‧ 2020-10-11 09:55:34
最佳解答

其實你已經寫得差不多了...

想像你走進一間有幾十個學生的教室
要問出身高最高學生的身高
你拿出紙筆
接著怎麼做?
你叫學生一個一個報出自己的身高(就是原程式裡的 a)
然後跟你手上記錄的答案(就是程式裡的 Max)做比較
是否比你手上記錄的答案要高?
是:就把手上記錄的答案改成新身高
否:沒事
接著叫下一個學生報身高直至所有學生都報完就可以了

#include<stdlib.h>
#include<stdio.h>

int main() {
	
	float a;
	float Max = 0;
	
	printf("Enter a number (enter 0 to end): ");
	scanf("%f", &a);
	while(a!=0){
		if (a > Max) {
			Max = a;
		}
		printf("Enter a number (enter 0 to end): ");
		scanf("%f", &a);	
	}
	printf("The largest number entered was %f\n", Max);
	return 0;
}

另外,點這裡是我這次鐵人賽唯一的一篇文章,喜歡的話左上角點 Like

用 do while

1
一級屠豬士
iT邦大師 1 級 ‧ 2020-10-10 21:17:48

若你真的有心想學會程式設計,應該自己設法逐步解決問題.
這個問題是很重要的基礎,自己想出來了,就容易突破了.
首先你先不管那個最大數字.就先做基本的輸入數字,搭配 while 迴圈的,做出輸入0時,離開while
迴圈,然後列印出一個離開迴圈的訊息出來.
加油!

1
BackONE
iT邦新手 5 級 ‧ 2020-10-11 09:07:26

先宣告一個為0的變數,在while迴圈中判斷比對使用者輸入的數值有沒有大於變數中的值,如果有就存入變數中。

我要發表回答

立即登入回答