vendredi 9 octobre 2015

Working on Loop-iterations. Is there a way to call these methods and not have several "do" declarations

        //Setting my variables.

      Scanner keyboard = new Scanner(System.in); // Initalizing keyboard to scanner input
      int quarter;
      double balance, intrestRate;
      boolean correct = false;

        //Loop iterations to resolve error, and determine value for each input.

      do{ //Beginning loop for number of quarters, must be between 1-10
     System.out.println("Enter a number between 1 and 10 for the amount of quarters");
     quarter = keyboard.nextInt();

     if(quarter >= 1 && quarter <= 10) //If quarter is between 1 and 10
     {
        System.out.println("You have " + quarter + " quarters.");
        correct = true;
     }
     else
     {
        System.out.println("Number of quarters must be between 1 and 10");
        correct = false;
     }
     }while(!correct);

    do{  //Second loop for rate of intrest.
     System.out.println("Enter intrest rate without percent a sign. Must be greaters than 5% and less than 25%.");
     intrestRate = keyboard.nextDouble();

     if (intrestRate >= 5 && intrestRate <=25)
     {
        System.out.println("You have selected a " + intrestRate + "% rate of intrest.");
        correct = true;
     }
     else
     {
        correct = false;
     }
  }while(!correct);
 }
}

So, this is my code. I'm experimenting with loops, and if-else type statements. I feel like, MY approach is way to complicated and could be easily summarized or revised period. Does anyone know how to approach with, with only declaring one do statement? This code is just in the beginning phases of my experiment and has much work to do. But before I move on, I was hoping for another set of eyes!

Aucun commentaire:

Enregistrer un commentaire