我只會從用戶那裏獲得3個值,所以只有3個可能的答案。您還可以假設用戶不會輸入0三次,並且三次輸入相同的值也無關緊要。我可以爲這些場景創建自己的東西(而且我已經擁有)。如何比較三個雙打併找到最大值,最小值和中等值? (儘可能最短)
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class Triangles
{
public static void main (String[]args) throws IOException
{
String a = "You have an";
String b = "triangle";
double l;
Scanner lengthinput = new Scanner(System.in);
double h;
Scanner heightinput = new Scanner(System.in);
double w;
Scanner widthinput = new Scanner(System.in);
System.out.println("Enter the length.");
l = lengthinput.nextDouble();
System.out.println("Enter the height.");
h = heightinput.nextDouble();
System.out.println("Enter the width.");
w = widthinput.nextDouble();
double max = l;
if (w > max) max = w;
if (h > max) {max = h;
我掙扎的原因是因爲以下幾點:
我知道最大的,現在我應該只是複製的最大代碼來創建一個最小的(因爲這就是我所學到,只是還沒有(介紹CS類))。但是,我怎麼知道如何排除我隨機選擇的最小值?然後對於中等價值也是如此。
我與你,直到最後兩句話。 「排除最小值」等? 「隨機選擇」?我沒有看到任何隨機選擇。請澄清。 –
所以我基本上做雙最大= l,開始我的比較。但是這是「隨機」選擇的,因爲我不知道實際的最大值是多少。 – Tastybot
要計算最大值,請將初始值設置爲Integer.MIN_VALUE。要計算最小值,請將初始值設置爲Integer.MAX_VALUE。這應該可以解決這個問題。 –