Java 如何将信息传输到一个方法,然后返回到主方法进行打印?

Java 如何将信息传输到一个方法,然后返回到主方法进行打印?,java,while-loop,Java,While Loop,我以前在另一个项目上做过一次,但我不知道如何在这个项目上做。我只需要用一个单独的方法对输出进行计算,然后将它们从main方法打印到控制台。我知道这对你们大多数人来说可能真的很容易,但我昨天才开始编码,所以我从来没有学过这个 我的代码: import java.util.Scanner; // This allows for the use of the scanner in the class public class SavingsAccount // Start of class {

我以前在另一个项目上做过一次,但我不知道如何在这个项目上做。我只需要用一个单独的方法对输出进行计算,然后将它们从main方法打印到控制台。我知道这对你们大多数人来说可能真的很容易,但我昨天才开始编码,所以我从来没有学过这个

我的代码:

import java.util.Scanner; // This allows for the use of the scanner in the class

public class SavingsAccount // Start of class
{
    public static void main(String[]args) // Start of main
    {
        double P; // These store the amounts that will be used in the accruing interest formula
        double i;
        double n;
        double S = 0;
        int timesLooped = 0;
        Scanner readConsole = new Scanner(System.in); // This is the scanner

        System.out.println("I am a savings account interest calculator."); // Prompts the user for input
        System.out.println("How much money have you deposited?");
        P = readConsole.nextDouble();
        S = P;
        System.out.println("Now, what is the annual interest rate? (i.e. .05)");
        i = readConsole.nextDouble();
        System.out.println("Finally, how long do you plan on having the money in the account?");
        n = readConsole.nextDouble();
        while (timesLooped <= n)
        {
            S = S + (P * i);
            timesLooped += 1;
        }
        System.out.println("Your balance in that time span is " + S + "."); // Tells you your ending balance
    }
}
import java.util.Scanner;//这允许在类中使用扫描仪
公共类SavingsAccount//类的开始
{
公共静态void main(字符串[]args)//main的开始
{
double P;//这些存储将在应计利息公式中使用的金额
双i;
双n;
双S=0;
int timesLooped=0;
Scanner readConsole=new Scanner(System.in);//这是扫描仪
System.out.println(“我是一个储蓄账户利息计算器”);//提示用户输入
System.out.println(“您存了多少钱?”);
P=readConsole.nextDouble();
S=P;
System.out.println(“现在,年利率是多少?(即,.05)”;
i=readConsole.nextDouble();
最后,你打算把钱存入账户多久;
n=readConsole.nextDouble();

while(timesLooped)
return
关键字是一个基本概念。我试图做的确切描述是“在单独的方法中进行计算和转换。将所需信息从主方法传递到方法中。计算完成后,将结果返回到主方法中进行打印。”是的,我记得另一个例子,但我不知道如何在这个项目上使用它。想想看。你把参数传递给一个方法,然后把结果或任何东西返回给被调用方。如果你昨天开始编码,那么你可能应该从更简单的事情开始。
private static double doyourstuff(double d1,double d2,double d3)
{  
  write your entire logic here as above written in main method.
  return calculated_balance;
}


 public static void main(String[]args)
  {
     --read values via scanner as you read above.
    double S= doyourstuff(d1,d2,d3);
    System.out.println("Your balance in that time span is " + S + ".");-- your final call
  }