ソースコード上の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件)
- 最新から表示
- 回答順に表示
No.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;
がシンプルでしょう。
No.2
- 回答日時:
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・・・これが求める結果
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- Ruby vscode 文字化け 1 2022/05/21 19:17
- ボランティア ウクライナ人からどういうこと? 6 2022/03/28 07:37
- UNIX・Linux 次の要件を満たすにはどのように修正したらよろしいでしょうか 1 2022/11/24 20:57
- 英語 英文の添削お願いします。【長文です。】 マッチングアプリで相手を言い負かしている時のやつです。 色々 1 2023/07/01 02:12
- C言語・C++・C# [至急]Project Euler:#17Number letter countsコード入力出力解説 2 2022/09/24 02:46
- 英語 下記の英文の日本語訳をお願いします。 1 2023/03/02 10:01
- 英語 下記の英文を日本語に訳して、その意味を教えてください。 1 2023/03/09 14:13
- 英語 この英文の意味を教えてください 3 2023/07/07 20:36
- 英語 できるだけ直訳で英語の翻訳をお願いします。(英語→日本語) 1 2022/10/15 20:59
- C言語・C++・C# C# DatagridviewにExcelシートを反映するとエラーが出る 2 2023/05/06 17:12
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
C言語のポインターに関する警告
-
[JAVA]try 内の変数を外で!?
-
JSPやサーブレットでSystem.out...
-
JAVA 文字色変更
-
JAVAでの占い
-
JAVAのDouble型の小数点以下の...
-
JAVAのエラーの意味が分かりま...
-
有効数字以下を四捨五入(Java)
-
構文解析中にファイルの終わり...
-
vb6 オブジェクトライブラリは...
-
NaNではなく、0と表示させたい
-
文字列の引き算
-
文字列の置換について。
-
javaで画像をランダムに表示さ...
-
EclipseでJava
-
コマンドプロンプトに不正な文...
-
改行の判定方法
-
Javaについて質問です Javaで 1...
-
並行サーバの導入
-
LC発振回路-ループ利得
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
C言語のポインターに関する警告
-
[JAVA]try 内の変数を外で!?
-
JSPやサーブレットでSystem.out...
-
JAVAのDouble型の小数点以下の...
-
EclipseでJava
-
「続行するには何かキーを押し...
-
文字列の引き算
-
java シンボルが見つかりません...
-
JAVA 文字色変更
-
改行の判定方法
-
JAVAのエラーの意味が分かりま...
-
Javaのコマンドライン引数を使...
-
java教えてください。
-
曜日の取得方法を教えて下さい!
-
JavaにはなぜGO TO ...
-
Javaでカレントディレクトリを...
-
構文解析中にファイルの終わり...
-
JAVA 三角形の判定
-
複数行キーボード入力した後に...
-
printfの%fにおいて教本どおり...
おすすめ情報