开发者

Taking a users input in a library class and making sure it is positive

Here are my two java files that I am working with. The problem I am having is getting the getPosNum method (highlighted below) to take the number the user inputs and see if that number is positive rath开发者_如何学运维er than doing what is doing right now, which is:

jGRASP exec: java Prog12

OUTPUT: First Investment

INPUT: Please enter a positive number: -5000.00

INPUT: ERROR: -5000.0 is not positive; try again: -3000.00

Rather than getting it to say something like this:

jGRASP exec: java Prog12

OUTPUT: First Investment

INPUT: Enter the first principal amount: -5000.00

INPUT: ERROR: -5000.0 is not positive; try again: -3000.00

How can I fix this problem and make it read correctly? Am I making sense?

public class Finance
{
   public static double getPosNum (String prompt)
   {
      double num;
      num = Input.readDouble("Please enter a positive number: ");
      while (num <= 0.0)
         num = Input.readDouble("ERROR: " + num + 
            " is not positive; try again: ");
      Output.showValue("You entered ", num);

      return num;

   } // method getPosNum

   public static void outFinances (double prin, double rate, double years, double fv)
   {
      Output.showMessage("The original amount invested was $" + prin + ",\nand the annual interest rate was set at " + rate + "%.\nIt has been " + years + " years since the investment was made,\nand the future value of the investment after that many years is $" + fv + ".");

   } // method outFinances

   public static double futureValue (double prin, double rate, double years)
   {
      double FV, P, r, n;
      P = prin;
      r = rate;
      n = years;
      FV = P * Math.pow((1 + (r / 100)),(n));
      return FV;

   } // method outFinances

} // class Finance

// File: Prog12.java
// CS200 Lab 12 Main
// Author: Ryan Pech
// Created: February 19, 2011

public class Prog12
{
   public static void main (String [] args)
   {
      double p, r, y, fv;

      Output.showMessage("First Investment");
      p = Finance.getPosNum("Enter the first principal amount: ");
      r = Finance.getPosNum("Enter the first interest rate: ");
      y = Finance.getPosNum("Enter the first number of years: ");
      fv = Finance.futureValue(p, r, y);
      Finance.outFinances(p, r, y, fv);

      Output.showMessage("Second Investment");
      p = Finance.getPosNum("Enter the second principal amount: ");
      r = Finance.getPosNum("Enter the second interest rate: ");
      y = Finance.getPosNum("Enter the second number of years: ");
      fv = Finance.futureValue(p, r, y);
      Finance.outFinances(p, r, y, fv);

  } // method main

} // class Prog12


// File: Finance.java
// CS200 Lab 12
// Author: Ryan Pech
// Created: February 19, 2011


Your getPostNum() accepts a parameter called prompt, but you never use that variable in that method at all. Instead of hardcoding Please enter a positive number:, substitute that string with that prompt variable, like this:-

Change...

num = Input.readDouble("Please enter a positive number: ");

... to...

num = Input.readDouble(prompt);


It should be obvious that it's printing "Please enter a positive number: " because you explicitly told it to in the getPosNum() method.

Using

num = Input.readDouble(prompt)

will solve your problem. A better way to go about this is to restrict printing to your main() method and use getPosNum() for input only. This way you know explicitly what you are printing every time. Something like

System.out.println("Enter the first principle amount.");
num = Input.readDouble();

and then perform some negative number checks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜