アプリ版:「スタンプのみでお礼する」機能のリリースについて

ソースコード上のa,b,c,d,eのなかから最大数と最小数を見つけることができましたが、どうしたら最小と最大を足すことができるのでしょうか?よかったら教えていただけたらありがたいです
ソースコード↓
import java.util.Scanner;

class No6{
public static void main(String []args){
int num1,num2,num3,product;
System.out.println("Enter 3 integer");
Scanner in = new Scanner(System.in);
num1=in.nextInt();
num2=in.nextInt();
num3=in.nextInt();

product=num1*num2*num3;

// quotient=/;

System.out.println("Product of 3 number is:"+product);

System.out.println("Enter another 2 integer:");
int num4,num5,sum,quotient,product2;

num4=in.nextInt();
num5=in.nextInt();
sum=num4+num5;
quotient=num4/num5;
product2=num4*num5;

System.out.println("Sum of 2 number is:"+sum);
System.out.println("product of 2 number is:"+product2);
System.out.println("quotient of 2 number is:"+quotient);
System.out.println("");
System.out.println("Enter onother 5 integer:");

int a,b,c,d,e;
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
d=in.nextInt();
e=in.nextInt();

if ( a > b && a > c && a > e && a > d ){
System.out.println("First number is largest.");
}

else if ( b > a && b > c && b>d && b>e ){
System.out.println("Second number is largest.");
}
else if ( c > a && c > b && c>d && c>e ){
System.out.println("Third number is largest.");
}
else if (d>a && d>b && d>c && d>e){
System.out.println("Four number is largest.");
}
else if (e>a && e>b && e>c && e>d){
System.out.println("Fifth number is largest.");
}

else {
System.out.println("Entered numbers are not distinct.");
}
System.out.println("");
if ( a <b && a<c&&a<d && a<d ){
System.out.println("First number is smallest.");
}

else if ( b<a &&b<c && b<d && b<e ){
System.out.println("Second number is Smallest.");
}
else if ( c<a && c<b &&c<e && c<d ){
System.out.println("Third number is Small.");
}
else if ( d<a&&d<b &&d<c && d<e){
System.out.println("Four number is Small.");
}
else if (e<a &&e<b && e<c &&e<d){
System.out.println("Fifth number is Small.");
}

else {
System.out.println("Entered numbers are not distinct.");
}


}
}

A 回答 (3件)

最大最小を求める部分は



int min=a;
int max=a;

if (b < min) min=b;
if(b > max) max=b;

if (c < min) min=c;
if(c > max) max=c;

if (d < min) min=d;
if(d > max) max=d;

がシンプルでしょう。
    • good
    • 0

No1の方のアドバイスに従うと、以下のようになります。


int min,max; //・・・追加
if ( a > b && a > c && a > e && a > d ){
System.out.println("First number is largest.");
max = a; //・・・追加
}

else if ( b > a && b > c && b>d && b>e ){
System.out.println("Second number is largest.");
max = b; //・・・追加
}
else if ( c > a && c > b && c>d && c>e ){
System.out.println("Third number is largest.");
max = c; //・・・追加
}
else if (d>a && d>b && d>c && d>e){
System.out.println("Four number is largest.");
max = d; //・・・追加
}
else if (e>a && e>b && e>c && e>d){
System.out.println("Fifth number is largest.");
/max = e; //・・・追加
}
else {
System.out.println("Entered numbers are not distinct.");
max = a;// ・・・追加(とりあえずa)
}

if ( a <b && a<c&&a<d && a<d ){
System.out.println("First number is smallest.");
min = a; //・・・追加
}

else if ( b<a &&b<c && b<d && b<e ){
System.out.println("Second number is Smallest.");
min = b; //・・・追加
}
else if ( c<a && c<b &&c<e && c<d ){
System.out.println("Third number is Small.");
min = c; //・・・追加
}
else if ( d<a&&d<b &&d<c && d<e){
System.out.println("Four number is Small.");
min = d; //・・・追加
}
else if (e<a &&e<b && e<c &&e<d){
System.out.println("Fifth number is Small.");
min = e; //・・・追加
}
else {
System.out.println("Entered numbers are not distinct.");
min = a;// ・・・追加(とりあえずa)
}

min+max・・・これが求める結果
    • good
    • 0

最大値と最小値を覚えておけばいい.

    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!