Question
Modify the program to print 1st, 2nd, 3rd, 4th and 5th rather than 1th, 2th, etc., without introducing redundant statements (Hint: Precede the if-else statement
Modify the program to print "1st", "2nd", "3rd", "4th" and "5th" rather than "1th", "2th", etc., without introducing redundant statements (Hint: Precede the "if-else" statement with a separate if-else statement that determines the appropriate ending based on the number).
import java.util.ArrayList; import java.util.Scanner;
public class MostPopularOS { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); ArrayList
// Source: Wikipedia.org, 2013 operatingSystems.add(new String("Windows 7")); operatingSystems.add(new String("Windows XP")); operatingSystems.add(new String("OS X")); operatingSystems.add(new String("Windows Vista")); operatingSystems.add(new String("Windows 8")); operatingSystems.add(new String("Linux")); operatingSystems.add(new String("Other"));
System.out.print("Enter N (1-7): "); nthOS = scnr.nextInt();
if ((nthOS >= 1) && (nthOS <= 7)) { System.out.print("The " + nthOS + "th most popular OS is "); System.out.println(operatingSystems.get(nthOS - 1)); }
return; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started