整数を 10 回入力し、最大値と最小値を求めるプログラムを作成しなさい。
BufferedReader br = new BufferedReader(
new InputStreamReader( System.in ) );
int max_value = 0, min_value = 0;
for( int i = 0 ; i < 10 ; i++ )
{
int value = Integer.parseInt( br.readLine() );
if( i == 0 )
{
max_value = value;
min_value = value;
}
else
{
if( value > max_value )
max_value = value;
if( value < min_value )
min_value = value;
}
}
System.out.println( "最大値=" + max_value + " 最小値=" + min_value );
または、
BufferedReader br = new BufferedReader(
new InputStreamReader( System.in ) );
int max_value = 0, min_value = 0;
for( int i = 0 ; i < 10 ; i++ )
{
int value = Integer.parseInt( br.readLine() );
if( i == 0 || value > max_value )
max_value = value;
if( i == 0 || value < min_value )
min_value = value;
}
System.out.println( "最大値=" + max_value + " 最小値=" + min_value );
または、
BufferedReader br = new BufferedReader(
new InputStreamReader( System.in ) );
int max_value = Integer.parseInt( br.readLine() );
int min_value = max_value;
for( int i = 1 ; i < 10 ; i++ )
{
int value = Integer.parseInt( br.readLine() );
if( value > max_value )
max_value = value;
if( value < min_value )
min_value = value;
}
System.out.println( "最大値=" + max_value + " 最小値=" + min_value );