1

ここに新しい男。私はこれに1か月ほど携わっています(CからObj C、Cocoa、iOSアプリの進行)

いくつかの基本的な C に戻ると、明らかに一般的な「scanf が次の scanf をスキップする」という問題に困惑しています。#c を 2 番目の scanf に追加しようとしましたが、そこにもスペースを追加しようとしましたが、それでも 2 番目の scanf をスキップし、平均として常に 0 を返しています。scanf よりも優れた入力コマンドがあることは知っています。しかし、今のところ、このような単純なものを機能させる方法が必要でしょうか?

ありがとう!〜スティーブ

int x;
int y;

printf("Enter first number:");
scanf("#i", &x);

printf("Enter second number:");
scanf("#i", &y);

printf("\nAverage of the two are %d", ((x+y)/2));
4

2 に答える 2

1

%d入力を読み取るには、フォーマット指定子を使用する必要がありintegerます。

scanf("%d", &x); 
scanf("%d", &y);  

そして、フロートにキャストして平均を出力します

printf("\nAverage of the two are %6.2f", ((float)(x+y)/2));

テストコード:

#include<stdio.h>
int main()
{

int x;
int y;

printf("Enter first number:");
scanf("%d", &x);

printf("Enter second number:");
scanf("%d", &y);

printf("\nAverage of the two are %6.3f\n", ((float)(x+y)/3));
return 0;
}
于 2013-08-31T15:53:28.093 に答える