lundi 19 mars 2018

JUnit Testing - Decimal to hexadecimal code

I need to do JUnit tests for a project in my school, but i don't reall know how to do a test for the following code (It just converts a decimal number into a hexadecimal one).

Any help would be appreaciated! Thanks!

import java.util.Scanner;

public class DecToHexa {

public static void main (String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a decimal number: ");
    int decimal = input.nextInt();

    String hex = "";

    while (decimal != 0) {
        int hexValue = decimal % 16;

        char hexDigit = (hexValue <= 9 && hexValue > 0) ? (char) (hexValue + '0')
            : (char) (hexValue - 10 + 'A');

        hex = hexDigit + hex;

        decimal = decimal / 16;
    }

    System.out.println("The hex number is " + hex);
}

}

Aucun commentaire:

Enregistrer un commentaire