簡體   English   中英

C# 需要幫助將變量放入方法中

[英]C# need help getting variable into a method

我嘗試在其他方法中使用 3 個不同的變量。 我想使用 RunSavingsRightNowModule 中 RunSpendinginRetirementModule 中的“retirementmoney”。 有人可以告訴我如何做到這一點嗎? 謝謝!

using System;

class MainClass {
  public static void Main (string[] args) {
    RunMenu();
    string choice = Console.ReadLine();

    if (choice == "1"){
      RunSpendinginRetirementModule();
    } 
    if (choice == "2"){
      RunSavingsRightNowModule();
    }
      else if (choice == "3"){
      RunSavingsNeededModule();
    }
  }

    public static void RunSpendinginRetirementModule() {
      Console.WriteLine("How much do you anuually spend");
      int annualspent = int.Parse(Console.ReadLine());
      int retirementmoney = annualspent * 25;
      double retirementspent = retirementmoney * 0.03;
      Console.WriteLine("You will need {0} for retirement",retirementmoney);
      Console.WriteLine("You can spend {0} each year during retirement", retirementspent);
    }
    public static void RunSavingsRightNowModule(){
      Console.WriteLine("how much you are currently saving");
      Console.WriteLine("how much are you saving a month");
      double savingmonth = double.Parse(Console.ReadLine());
      double savingyear = savingmonth * 12;
      Console.WriteLine("how old are you");
      int age = int.Parse(Console.ReadLine());
      Console.WriteLine("at what age do you plan to retire");
      int retirementage = int.Parse(Console.ReadLine());
      int savingsyears = retirementage - age;
      double retirementsavings = savingyear * savingsyears;
      Console.WriteLine("at the rate you are saving you will have {0} when you retire at age {1}", retirementsavings, retirementage);
    }

    public static void RunSavingsNeededModule(){

    }
    public static void RunMenu(){
      Console.WriteLine("Retirement Calculator");
      Console.WriteLine("1) Spending in Retirement");
      Console.WriteLine("2) Savings Right Now");
      Console.WriteLine("3) Savings needed");
    }
  }

目前,您的方法沒有返回值,因為它們被聲明為“void”。 您需要更改這些以返回一個值,然后將其存儲在您的調用方法中。

我將展示一種方法的示例:

//Change the return type from void to double
 public static double RunSpendinginRetirementModule() 
 {
      Console.WriteLine("How much do you anuually spend");
      int annualspent = int.Parse(Console.ReadLine());
      int retirementmoney = annualspent * 25;
      double retirementspent = retirementmoney * 0.03;
      Console.WriteLine("You will need {0} for retirement",retirementmoney);
      Console.WriteLine("You can spend {0} each year during retirement", retirementspent);

     //now return the value
     return retirementspent;
    }

然后更新您的 MainClass。 同樣,我只針對一種方法展示這一點。

class MainClass {
  public static void Main (string[] args) {
    RunMenu();
    string choice = Console.ReadLine();

    //Declare new variable here, set default value of 0.0
    double retirementAmount = 0.0;

    if (choice == "1")
    {
      //Update call to receive return value
      retirementAmount = RunSpendinginRetirementModule();
    } 
    ...

您可以對其他方法執行相同的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM