1

I've been looking for different solutions out here but on my code they won't work. I'm getting an input from a user but then it doesn't display what's inside the if-else statement. I tried to do a different code for the if statement but it doesn't work. The int input works but it doesn't work in the string input.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    int numbers [] = new int[10];
    int temp;
    String sc = " ";
    Scanner scan = new Scanner(System.in);
    
    // Loop through array
    // Change value of each array per iteration
    
    System.out.print("Enter 10 integers: ");
    
    for(int i = 0; i < numbers.length; i++){
        numbers[i] = scan.nextInt();
    }
    
    System.out.print("\nPlease Choose Among The Following:");
    System.out.println("\n A - Display the numbers \t B - Display the values of even indexes \n C - Display the values of odd indexes \t D - Display the values in ascending order \n E - Display the values in descending order");
    
    System.out.print("\nEnter The Letter of Your Choice: ");
    sc = scan.nextLine();
    
    if (sc.equals('A') || sc.equals('a')){
    System.out.print("Display each item in array");
    for(int number: numbers){
        System.out.println(number);
    }}
    
    else if (sc.equals('B') || sc.equals('b')){
    System.out.println("Display odd indexes");
    for(int i = 0; i < numbers.length; i++){
        if(i % 2 == 0){
            System.out.println(numbers[i]);
        }
    }}
    
    else if (sc.equals('C') || sc.equals('c')){
    System.out.println("Display even indexes");
    for(int i = 0; i < numbers.length; i++){
        if(i % 2 == 1){
            System.out.println(numbers[i]);
        }
    }} 
 
    else if (sc.equals('D') || sc.equals('d')){
    int [] ascendingList = numbers.clone();
    System.out.println("Display in ascending order using bubble sort");
    for(int i = 0; i < ascendingList.length - 1 ; i++){
        for(int j = 0; j < (ascendingList.length - i - 1); j++){
           if(ascendingList[j] > ascendingList[j+1]){
                temp = ascendingList[j];
                ascendingList[j] = ascendingList[j+1];
                ascendingList[j+1] = temp;
            }
        }
    }   
    for(int number: ascendingList){
        System.out.println(number);
    }}
    
    else if (sc.equals('E') || sc.equals('e')){
    int [] descendingList = numbers.clone();
    System.out.println("Display in descending order using bubble sort");
    for(int i = 0; i < descendingList.length - 1 ; i++){
        for(int j = 0; j < (descendingList.length - i - 1); j++){
            if(descendingList[j] < descendingList[j+1]){
                temp = descendingList[j];
                descendingList[j] = descendingList[j+1];
                descendingList[j+1] = temp;
            }
        }
    }   
    
    for(int number: descendingList){
        System.out.println(number);
    }}
  }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Tina
  • 11
  • 1

0 Answers0