我的问题是基于在我的主代码中返回一个Void方法,这是我的代码。我在哪里做错了?
if isbn not in already_written_isbn:
write_it_in_the_file
编译失败并显示错误:
错误:(34,78)java:' void'这里不允许输入
答案 0 :(得分:1)
您的方法FutureInvestmentValue
没有返回任何值,但您正在尝试从此方法打印(缺少的)返回值:
System.out.printf("Investment Value is $%2f", m.FutureInvestmentValue(...));
查看您的代码,并不十分清楚方法FutureInvestmentValue
应该如何表现 - 它似乎打印了计算出的信息本身。
可能的解决方案是:
System.out.println("Investment Value is:");
m.FutureInvestmentValue(...); // Prints the calculated data itself
保持System.out.printf
方法中的main
行不变,并将FutureInvestmentValue
修改为return
一些值而不是打印它。
答案 1 :(得分:0)
您需要执行以下两项操作之一:从方法返回结果或返回值,打印出用于存储结果的方法参数。
所以要么像这样改变你的FutureInvestmentValue方法:
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);
答案 2 :(得分:0)
如Alex FutureInvestmentValue方法所述,该方法无效,因此您会收到错误。如果要在print语句中打印ininvestmentValue,则将方法的返回类型更改为double并返回InvestmentAmount变量值。
答案 3 :(得分:0)
我设法以问题的方式回答了这个问题,但我也考虑了你们所说的话,最后我设法在我的代码中看到了我的错误,所以以下是我对这个问题的回答
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);
}
}
}