简体   繁体   English

UserInput方法在Java中不断分配其他值

[英]UserInput Method keeps assigning a different value in java

this is my first question on the site. 这是我在网站上的第一个问题。 I am a fresh CS student needing some help with something that is probably really simple. 我是应届毕业生,需要一些可能非常简单的帮助。 The code as is will compile. 该代码将按原样编译。 When I enter in the values as the program asks, it stores the values wrong. 当我按程序要求输入值时,它存储了错误的值。 It will store the right values for gross pay and savings rate but the IRA rate comes back as 100% even when entered at 6.9 and it seems it stores the IRA rate in saveAmount. 它将为总薪水和储蓄率存储正确的值,但是即使将IRA率输入为6.9,IRA率也会恢复为100%,并且它似乎将IRA率存储在saveAmount中。 Please halp me figure out what I am doing wrong here. 请阻止我找出我在这里做错了什么。

import java.text.DecimalFormat;
import java.util.*;

public class CollinDunn_1_05 {

   static Scanner console = new Scanner(System.in);
   static DecimalFormat formatCash = new DecimalFormat("#,###.00");
   static double iraTotal = 0.0;
   static double saveAmount = 0.0;
   static double totalSave = 0.0;
   static String line = "";

   public static void main (String [] args) {

      // Input variables
      double grossPay = 0.0;  // The gross pay from a users paycheck
      double saveRate = 0.0;  // This is the user entered savings rate
      double iraRate= 0.0;    // The IRA investment rate
      String whichOne = "";   // A temp variable to pass a string type into UserInput

      printInfo();                      
      grossPay = userInput("gross pay");
      saveRate = userInput("savings rate");
      iraRate = userInput("IRA rate");
      iraTotal = iraAmount(grossPay, iraRate);
      saveAmount = savingsAmount(grossPay,  saveRate);
      outputResults(grossPay, saveRate, saveAmount, iraRate, iraTotal);     

      return;      

   } // End Main

   public  static void printInfo() {

     System.out.println ("This program uses methods to calculate \n"
                                           + "savings amounts and IRA investment amounts \n"
                                           + "from user input consisiting of their gross pay, \n"
                                           + "their desired savings rate and IRA rate, made by "
                                           + " Collin Dunn");
      return;

   } // End ProgramInfo

   public static double userInput(String whichOne) {

      double saveMe = 0.0;
      System.out.print("Please enter your " + whichOne + ":    ");
      saveMe = console.nextDouble(); 

      return saveMe;

   } // End userInput

    public static double iraAmount(double grossPay, double iraRate) {                                                                                                
       iraTotal = grossPay * (iraRate / 100.0);

      return iraTotal;

   } // End iraAmount

   public static double savingsAmount(double grossPay, double saveRate) {       
      saveAmount = grossPay * (saveRate / 100.0);

      return saveAmount;

   } // End savingsAmount


   public static void outputResults(double grossPay, double saveRate, double iraRate, 
                                                                      double saveAmount, double iraTotal) {


       totalSave = saveAmount + iraTotal;

       System.out.print ("With a gross pay of $" + formatCash.format(grossPay)
                                         + ", a savings rate of %" + formatCash.format(saveRate)
                                         + " and a IRA rate of %" +formatCash.format(iraRate)
                                         + ".\n Your savings amount will be $" + formatCash.format(saveAmount)
                                         + ", with a investment amount of $" + formatCash.format(iraTotal)
                                         + ".\n Which leaves you with a total savings of $" +
                                         +  totalSave + ". Way to go for paying yourself!" );

      return;

   } // End outputResults

} //End Class

Your only issue is the order of arguments you pass to or have set on the outputResults() method. 唯一的问题是传递给或在outputResults()方法上设置的参数的顺序。

Change the signature of the method to: 将方法的签名更改为:

   public static void outputResults(double grossPay, double saveRate, double saveAmount, double iraRate, double iraTotal) {

Which now matches how you call the method: 现在与您如何调用该方法相匹配:

outputResults(grossPay, saveRate, saveAmount, iraRate, iraTotal); 

Let me make a couple of additonal suggestions: 让我提出一些其他建议:

1) You are consistently naming arguments in your method signatures the same names as global variables, which makes it confusing which is which when accessing the variable in the method. 1)您始终在方法签名中为自变量命名与全局变量相同的名称,这使得在访问方法中的变量时感到困惑。 Either avoid using the same names for the method input variables, or use something like this.amount = amount to make it more obvious of your intention. 要么避免对方法输入变量使用相同的名称,要么避免使用类似this.amount = amount ,以使其更明显地体现您的意图。

2) Avoid static unless you have a valid reason to use it (which is pretty rare). 2)避免使用静态,除非您有正当理由使用它(这种情况很少见)。 Instead, take advantage of Java's Object oriented nature and create an instance of your class in the main method and call methods on that instance. 相反,请利用Java的面向对象的性质,并在main方法中创建类的实例,然后在该实例上调用方法。 This will make your code more readable, reliable, and reusable. 这将使您的代码更具可读性,可靠性和可重用性。

3) In a method that returns type 'void', you don't need to add the empty return; 3)在返回类型为“ void”的方法中,您不需要添加空return; statement. 声明。

To correct your issue and also demonstrate the points I listed, I have refactored your code and provided it below. 为了更正您的问题并演示我列出的要点,我对您的代码进行了重构,并在下面提供了它。 By the way, you have a lot of potential. 顺便说一下,您有很多潜力。 Despite the fact there are a few details you can improve, for being a first year CS student, your code is well written and thought out. 尽管事实上您可以改进一些细节,但是对于成为CS一年级的学生来说,您的代码编写得很周到,而且考虑周全。 Good job! 做得好!

import java.text.DecimalFormat;
import java.util.*;

public class CollinDunn_1_05 {

   DecimalFormat formatCash = new DecimalFormat("#,###.00");
   double iraTotal = 0.0;
   double saveAmount = 0.0;
   double totalSave = 0.0;
   double grossPay = 0.0;  // The gross pay from a users paycheck
   double saveRate = 0.0;  // This is the user entered savings rate
   double iraRate= 0.0;    // The IRA investment rate

    public CollinDunn_1_05(double gross, double saveRt, double iraRt){
      this.grossPay = gross;
      this.saveRate = saveRt;
      this.iraRate = iraRt;
    }

    public void calculate(){
      calcIraAmount();
      calcSavingsAmount();
    }

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

      CollinDunn_1_05 program = new CollinDunn_1_05(
          userInput("gross pay", scanner),
          userInput("savings rate", scanner),
          userInput("IRA rate", scanner)
        );

      program.calculate();
      program.outputResults();

   } // End Main

   public static void printInfo() {

     System.out.println ("This program uses methods to calculate \n"
                                           + "savings amounts and IRA investment amounts \n"
                                           + "from user input consisiting of their gross pay, \n"
                                           + "their desired savings rate and IRA rate, made by "
                                           + " Collin Dunn");
      return;

   } // End ProgramInfo

   public static double userInput(String whichOne, Scanner console) {
      double saveMe = 0.0;
      System.out.print("Please enter your " + whichOne + ":    ");
      saveMe = console.nextDouble(); 

      return saveMe;

   } // End userInput

    public void calcIraAmount() {    
      iraTotal = grossPay * (iraRate / 100.0);
   } // End iraAmount

   public void calcSavingsAmount() {       
      saveAmount = grossPay * (saveRate / 100.0);
   } // End savingsAmount


   public void outputResults() {
      totalSave = saveAmount + iraTotal;
      System.out.print ("With a gross pay of \$" + formatCash.format(grossPay)
                                         + ", a savings rate of %" + formatCash.format(saveRate)
                                         + " and a IRA rate of %" +formatCash.format(iraRate)
                                         + ".\n Your savings amount will be \$" + formatCash.format(saveAmount)
                                         + ", with a investment amount of \$" + formatCash.format(iraTotal)
                                         + ".\n Which leaves you with a total savings of \$" +
                                         +  totalSave + ". Way to go for paying yourself!" );

   } // End outputResults

} //End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM