基本上,我正在嘗試創建一個程式,您可以在其中從鍵盤讀取多達 100 個輸入。輸入必須是 [1,100] 之間的數字,當輸入“0”時,程式退出回圈并顯示除數字零之外的所有輸入數字的陣列。我正在嘗試使用“while,for,if”陳述句,但理解邏輯有點困難。你們能幫助我并給我一些提示和反饋嗎?對不起我的無知,但我是第一次編程。
#include <stdio.h>
int main(){
int long ship;
int array[100];
for(ship = 0; ship < 100; ship ){
printf("Repaired ship: ");
scanf("%li", &ship[array]);
while(ship[array] == 0){
printf("\n\t");
for(ship = 0; ship < 100; ship )
printf("%li ", ship[array]);
printf("\n\nRepaired ships: %li", ship);
}
}
if(ship == 100){
printf("\n\t");
for(ship = 0; ship < 100; ship)
printf("%li ", ship[array]);
printf("\n\nRepaired ships: %li", ship);
}
return 0;
}
uj5u.com熱心網友回復:
下面,我重新編輯了您的代碼,并進行了少量更正。使用代碼下方提到的注釋來理解這些更正。希望這可以幫助!
代碼
#include <stdio.h>
int main(){
int ship;
int long array[100];
for(ship = 0; ship < 100; ship )
{
printf("Repaired ship: ");
scanf("%ld", &array[ship]);
if(array[ship]==0)
break;
}
ship=0;
while(array[ship] != 0)
{
printf("\n\t");
printf("%ld ", array[ship]);
ship ;
}
printf("\n\nRepaired ships: %d", ship);
return 0;
}
船不需要 long int,因為 100 不是那么大的價值
long int(如果需要)將用于上面使用的陣列
%ld是 long int 的格式說明符
上面的 for 回圈處理輸入
while 回圈中不需要 for 回圈,因為 while 回圈已經處理了所需的輸出
使用 array[ship] 型別表示法而不是 ship[array],因為 array[ship] 表示法更常見
**//這部分 if 陳述句不需要,因為 while 回圈已經處理了它
if(ship == 100){
printf("\n\t");
for(ship = 0; ship < 100; ship)
printf("%li ", ship[array]);
printf("\n\nRepaired ships: %li", ship);
}**
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/523662.html
