简体   繁体   中英

Find a value out of 3 java

I have seach in this site and have found some things that talk about this topic but can't seem to fix what I'm searching, I have tried and after some hours of seaching and of trial and error I decided to post the question since I can't find a way to do what I want to do. Here is my code:

import java.io.*;
public class intermedio
{public static void main(String[] args) 
{InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
int a, b, c; 
int max, min, mid;
try
{
    System.out.println("first value");
    a = Integer.parseInt(br.readLine());
    System.out.println("second vaule");
    b = Integer.parseInt(br.readLine());
    System.out.println("third value");
    c = Integer.parseInt(br.readLine());
    if(a >= b && a >= c && b >= c)
    {
    max = a;
    mid = b;
    min = c;
    System.out.println("middle value is: " + mid);
    }else if(a <= b && a <= c && b <= c)
    {
    min = a;
    mid = b;
    max = c;
    System.out.println("middle value is: " + mid);
    }else if(a >= b && a <= c && b <= c)
    {
    mid = a;
    min = b;
    max = c;
    System.out.println("middle value is: " + mid);
    }else if(a >= b && a >= c && b <= c)
    {
    max = a;
    min = b;
    mid = c;
    System.out.println("middle value is: " + mid);
    }else if(a <= b && a <= c && b >= c)
    {
    min = a;
    max = b;
    mid = c;
    System.out.println("middle value is: " + mid);
    }else if(a <= b && a >= c && b >= c)
    {
    mid = a;
    max = b;
    min = c;
    System.out.println("middle value is: " + mid);
    }
}catch(IOException e){
e.printStackTrace();
}
}
}

I have test it with 1, 2 and 3 and it always gives me what I ask like if ask for the min it gives me 1, if I ask for the mid it always gives me 2 and if I ask for the max it gives me the 3. What I want my code to do is that instead of putting the System.out.println("middle value is: " + mid); at the end of each if I want to put the whole line at the end of my code. Basically what I want is a way to simplify my code.

What I want my code to do is that instead of putting the System.out.println("middle value is: " + mid); at the end of each if I want to put the whole line at the end of my code

So... just do that? Take the line out of the multiple places it is now, and put it right after the last else if block.

If the compiler complains that it might not be initialized, perhaps you should initialize mid to 0 when you declare it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM