lundi 3 juin 2019

Java how do you shorten this code? My code is too long

Write a program that allows user to input two integers in the range of (between) 10 to 50, inclusive and print the product as an output. The program must call a method/function, named computeProduct, which takes in the two numbers and returns the product. The method/function should also check that the two integers are within the 10 to 50 range, if not, it should print a message that says “Number is not in range, please try again.”.

 import java.util.Scanner;
 public class MyClass {
public static int computeproduct(int a, int b) 
{
    if ((a <10) || (a > 50) || (b<10) || (b>50))
    {
        System.out.println("0");
        System.out.println("Number is not in range, please try again");
    }

    int Product = a * b;

    return Product;
}

public static void main(String args[])
{

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter First Number");
    int a = scanner.nextInt();
    System.out.println("Enter Second Number");
    int b = scanner.nextInt();

     if ((a <10) || (a > 50) || (b<10) || (b>50))
    {
        System.out.println("0");
        System.out.println("Number is not in range, please try again");
    }
    else
    {
        System.out.println(computeproduct(a, b));
    }


}

}

Everything works fine , but i feel like it can be executed with a few codes less...I don't mind using some suggestions!

Aucun commentaire:

Enregistrer un commentaire