Java 在我的主方法中返回一个void方法
我的问题是基于在java的主方法上返回一个Void方法,下面是我的代码。我哪里做错了Java 在我的主方法中返回一个void方法,java,Java,我的问题是基于在java的主方法上返回一个Void方法,下面是我的代码。我哪里做错了 package practiceq3; import java.util.Scanner; public class Practiceq3 { public void FutureInvestmentValue(double InvestmentAmount, double MontlyInvestmentRate, int Years) { System.out.print
package practiceq3;
import java.util.Scanner;
public class Practiceq3 {
public void FutureInvestmentValue(double InvestmentAmount, double MontlyInvestmentRate, int Years)
{
System.out.printf("%-5s %s \n", "Years", "Investment Value");
//For loop
int i;
int y;
for(i = 1; i <= Years; i++)
{
for (y = 1; y <= 12; y++)
{
InvestmentAmount += (InvestmentAmount * MontlyInvestmentRate);
System.out.printf("%-5d %2f \n", i , InvestmentAmount);
}
}
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Investment Amount: ");
double InvestmentAmount = input.nextDouble();
System.out.print("Enter the Montly investment rate: ");
double MontlyInvestmentRate = input.nextDouble();
System.out.print("Enter the Years: " );
int Years = input.nextInt();
Practiceq3 m = new Practiceq3();
// Compilation error here:
System.out.printf("Investment Value is $%2f", m.FutureInvestmentValue(input.nextDouble(),(input.nextDouble()/1200), Years));
input.close();
}
}
package-practiceq3;
导入java.util.Scanner;
公开课实习Q3{
公开作废未来投资价值(双倍投资金额,双月投资率,整数年)
{
System.out.printf(“%-5s%s\n”、“年”、“投资价值”);
//For循环
int i;
int-y;
对于(i=1;i您的方法FutureInvestmentValue
不返回任何值,但您正试图打印此方法的(缺少的)返回值:
System.out.printf("Investment Value is $%2f", m.FutureInvestmentValue(...));
查看您的代码,不太清楚该方法应该如何准确地运行FutureInvestmentValue
它似乎会打印计算出的信息本身
可能的解决方案是:
保持main
方法中的System.out.printf
行不变,并将FutureInvestmentValue
修改为return
一些值,而不是打印它
您需要做两件事之一:从方法返回结果,或者打印出用于存储结果的方法参数,而不是返回值
因此,要么改变你的未来投资价值方法,如下所示:
public double FutureInvestmentValue(double InvestmentAmount, double MontlyInvestmentRate, int Years)
{
// skip other stuff
return InvestmentAmount;
}
double value = input.nextDouble();
m.FutureInvestmentValue(value, value/1200, Years);
System.out.printf("Investment Value is $%2f", value);
或者将主要方法更改为以下内容:
public double FutureInvestmentValue(double InvestmentAmount, double MontlyInvestmentRate, int Years)
{
// skip other stuff
return InvestmentAmount;
}
double value = input.nextDouble();
m.FutureInvestmentValue(value, value/1200, Years);
System.out.printf("Investment Value is $%2f", value);
正如Alex提到的,FutureInvestmentValue方法无效,因此会出现错误。如果要在print语句中打印InvestmentValue,请将方法的返回类型更改为double,并返回InvestmentAmount变量值。我设法按照问题要求的方式回答了这个问题,但我也考虑了您的要求uys说,最后我设法发现了代码中的错误,所以下面是我对这个问题的答案
import java.util.Scanner;
public class ClassA {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
ClassB con = new ClassB();//this is the object of the class
System.out.print("Enter the Investment Amount: ");
double investmentAmount = input.nextDouble();
System.out.println();
System.out.print("Enter the Montly investment rate: ");
double montlyInvestmentRate = input.nextDouble();
System.out.println();
System.out.print("Enter the Years: ");
int years = input.nextInt();
System.out.println();
//this is where we call our void method FutureInvestmentValue(double InvestmentAmount, double MontlyInvestmentRate, int Years)
con.FutureInvestmentValue(investmentAmount ,(montlyInvestmentRate/1200), years);
}
}
}
public class ClassB {
public void FutureInvestmentValue(double InvestmentAmount, double MontlyInvestmentRate, int Years)
{
System.out.printf("%-5s %s \n", "Years", "Future Value");
//For loop
int i;
double Fv;
for(i = 1; i <= Years; i++)
{
//Future value formular A=P(1+i/m)^n*m
Fv = (InvestmentAmount * Math.pow(1+MontlyInvestmentRate,i*12));
System.out.printf("%-5d %.2f \n", i , Fv);
}
}
}
import java.util.Scanner;
甲级公共课{
公共静态void main(字符串[]args){
try(扫描仪输入=新扫描仪(System.in)){
ClassB con=new ClassB();//这是类的对象
系统输出打印(“输入投资金额:”);
double investmentAmount=input.nextDouble();
System.out.println();
系统输出打印(“输入每月投资率:”);
double MonthlyInvestmentRate=input.nextDouble();
System.out.println();
System.out.print(“输入年份:”);
int years=input.nextInt();
System.out.println();
//这就是我们称之为未来投资价值的无效方法(双倍投资金额,双月投资率,整数年)
con.未来投资价值(投资金额(月投资率/1200),年);
}
}
}
公共类B{
公开作废未来投资价值(双倍投资金额,双月投资率,整数年)
{
System.out.printf(“%-5s%s\n”、“年”、“未来值”);
//For循环
int i;
双Fv;
对于(i=1;我您至少需要提示我们问题可能在哪里。从结构上讲,您的代码看起来很好。好的,我的问题在下面一行:System.out.printf(“投资价值为$%2f”,m.FutureInvestmentValue(input.nextDouble(),(input.nextDouble()/1200),Years));问题是,具体来说,什么?是的,我该怎么做?因为我知道void不返回任何内容,但我意识到我可以访问void中的值。因此,在这种情况下,我超级尝试所有内容,但什么都不返回working@MendyMercy,请尝试建议的1。
解决方案,如果您的作业要求void
方法(基于您的评论).好的,我的无效方法需要有投资金额、每月投资金额和年数的值。但同样,当我计算未来投资价值时,必须调用所有这些。任务的问题主要需要我调用无效方法method@MendyMercy然后您需要更改主方法,如sugge受激发射损耗