Answer to Question #259143 in Java | JSP | JSF for joy

Question #259143

2. Where's the Biggest One?


by CodeChum Admin



We've already tried comparing 3 numbers to see the largest among all, so let's try a more complicated mission where we locate the position of the largest among 5 numbers. There are only 4 possible cases for this mission:



- if the largest digit is the first digit, print "Leftmost"



- if the largest digit is the third digit, print "Middle"



- if the largest digit is the last digit, print "Rightmost"



- if none of the above is correct, print "Unknown"





Now, show me how far you've understood your lessons!



Input



A line containing a five-digit integer.



1·4·6·3·2


Output



A line containing a string.



Middle

1
Expert's answer
2021-10-31T12:00:48-0400


import java.util.Scanner;


public class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		String numbers[] = keyBoard.nextLine().trim().split(" ");
		int number = Integer.parseInt(numbers[0]);
		int max = number;
		int maxIndex = 0;
		for (int j = 0; j < numbers.length; j++) {
			number = Integer.parseInt(numbers[j]);
			if (number > max) {
				max=number;
				maxIndex = j;
			}
		}
		// - if the largest digit is the first digit, print "Leftmost"
		if (maxIndex <= 1) {
			System.out.println("Leftmost");
		}else
		// - if the largest digit is the third digit, print "Middle"
		if (maxIndex == 2) {
			System.out.println("Middle");
		}else
		// - if the largest digit is the last digit, print "Rightmost"
		if (maxIndex >2) {
			System.out.println("Rightmost");
		}else
		// - if none of the above is correct, print "Unknown"
		{
			System.out.println("Unknown");
		}
		keyBoard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS