mardi 22 octobre 2019

How to condense these large if statements

I've been working on this code for a while now and finally completed it, but is there some way that I could possibly condense the large amount of if statements? I've tried everything within my knowledge of coding and nothing has seemed to work.

import java.util.Scanner;
public class AidanMRN{
  public static void main(String[] args){
    //Creating Variable "Number"
    int number;
   Scanner keyboard = new Scanner(System.in);
   //Storing Number
   System.out.println("Pick a number, 1 - 10");
   number = keyboard.nextInt();
   //If statements and Outputs
   if (number > 10)
   //Error message
   System.out.print("Error, " + number + " is higher than 10.\nPlease try again");
   if (number == 1)
   System.out.println("Roman Numeral: I");
   if (number == 2)
   System.out.println("Roman Numeral: II");
   if (number == 3)
   System.out.println("Roman Numeral: III");
   if (number == 4)
   System.out.println("Roman Numeral: IV");
   if (number == 5)
   System.out.println("Roman Numeral: V");
   if (number == 6)
   System.out.println("Roman Numeral: VI");
   if (number == 7)
   System.out.println("Roman Numeral: VII");
   if (number == 8)
   System.out.println("Roman Numeral: VIII");
   if (number == 9)
   System.out.println("Roman Numeral: IX");
   if (number == 10)
   System.out.println("Roman Numeral: X");
  }
}

Aucun commentaire:

Enregistrer un commentaire