C# нужна помощь для получения переменной в метод - PullRequest
0 голосов
/ 04 мая 2020

У меня есть 3 разные переменные, которые я пытаюсь использовать в других методах. Я хочу использовать «деньги на пенсию» из модуля RunSpendinginRetirementModule в модуле RunSavingsRightNowModule. Может кто-нибудь, пожалуйста, покажите мне, как это сделать? Спасибо!

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");
    }
  }

1 Ответ

0 голосов
/ 04 мая 2020

В данный момент ваши методы возвращают не значение, поскольку они объявлены как '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();
    } 
    ...

Вы можете сделать то же самое для других методов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...