2013-03-07 85 views
-1

对前一个程序的响应,只需放置新的余额();公共静态无效的主要是,不起作用,没有该程序运行,但nerver打印出用户的问题!如何调用方法到主要方法(程序运行但不打印问题)(作业)

import java.io.*; 

public class cInterest { 

    public static void main(String[] args) throws IOException 
    { 
     //new balance ; 
    } 
    public static double balance(double principal, double rate, double years) throws IOException{ 

     double amount = 0; 

     String input; 
     BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); 

     System.out.print("How much would you like to take out? "); 
     input = myInput.readLine(); 
     principal = Double.parseDouble (input);   

     System.out.print("Enter the interest rate: "); 
     input = myInput.readLine(); 
     rate = Double.parseDouble (input); 

     for (int i = 1; i < years; i++) { 
      amount = principal * rate * years; 
      amount += principal; 
     } 
     return amount; //- principal; 
    } 
} 
+0

只是称之为平衡(...); – Dan 2013-03-07 16:43:53

+0

题外话,你几乎肯定希望'我'从0开始。否则,如果你想运行一年(例如),你甚至不会进入循环。 – 2013-03-07 16:52:28

回答

2

balance是一种方法,而不是一个类,所以你不能使用new关键字。你想呼叫的方法,而不是像这样:

public static void main(String[] args) throws IOException 
{ 
    double balance = balance(0.0, 0.0, 1.0); // Awkward hard-coded variables! Ew! 
    System.out.println(balance); 
} 

但是你为什么要通过这些变量的方法,当用户只是将覆盖它们?您的balance方法只应负责计算余额,而不是收集用户输入。你可以做的是,在main方法:

public static void main(String[] args) throws IOException 
{ 
    // Gather user input. 
    String input; 
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in)); 

    System.out.print("How much would you like to take out? "); 
    input = myInput.readLine(); 
    double principal = Double.parseDouble (input);   

    System.out.print("Enter the interest rate: "); 
    input = myInput.readLine(); 
    double rate = Double.parseDouble (input); 

    System.out.print("Enter the number of years: "); 
    input = myInput.readLine(); 
    double years = Double.parseDouble (input); 

    // Now do the calculations... 
    double balance = balance(principal, rate, years); // Much clearer! 
    System.out.println(balance); 
} 

public static double balance(double principal, double rate, double years) { 
    // Calculate the end balance based on the parameters, and return it. 
} 

更妙将是把用户输入的聚会变成自己的专用方法,但我远远不够题外话,因为它是。

+0

这有效,但天平中的参数究竟是干什么的?输出总是等于0 – user2133068 2013-03-07 16:45:12

+0

我已经更新了有关参数的更多详细信息。 – 2013-03-07 16:49:50

+0

非常感谢! – user2133068 2013-03-07 16:53:00

0

你应该在某处明确地调用balance()方法。

0

使用System.out.println而不是System.out.print,否则,您的流不会被刷新。你可以参考PrintStream javadoc了解更多信息。

相关问题