mercredi 20 septembre 2017

Method returns negative value

I am trying to create a method that checks if an input is a double that is greater than zero. Checking if it is indeed a value is working fine with my code, but, even though when entering negatives its prompts to reenter the value, in the end, after entering a positive value it returns the negative entered earlier

 public static double isValid(double v){
    Scanner scan = new Scanner(System.in);
    if (scan.hasNextDouble()){
     v = scan.nextDouble();
     if (v<=0) {
        System.out.println("You have to use a positive number");
        isValid(v);
     }
     else {
        return v;
     }
  }
  else {
     System.out.println("Thats not how it works");
     isValid(v);
  }
  return v;
}

Aucun commentaire:

Enregistrer un commentaire