-6
public class investment {   
    public static void main(String args[]){

        int i=0;

        Scanner Pay = new Scanner(System.in);
        System.out.print("how many years do u want to put in the money for? ");
        int years = Pay.nextInt();

        Scanner Py = new Scanner(System.in);
        System.out.print("how much do u want to invest? ");
        double money = Py.nextDouble();


        while (i<=years){
            i++;            
            double pr=.10;       
            double finall=(money*pr)+money;           
            System.out.print(finall);
        }    
    }
}

为什么这会输出一个 IP 地址而不是一个合法数字?

4

3 回答 3

4

I think this looks like an IP-Adress because you print in the loop, and you are printing comma values(double values).

Try printing it at the end of the while loop or use System.out.println() instead.

于 2013-06-04T15:30:16.907 回答
1
while (i<=years){
        i++;

    double pr=.10;

        double finall=(money*pr)+money;


        System.out.print(finall);

You are printing in a loop, and you print each double unadorned - no spaces or newlines to separate them. Use System.out.println to have each on its own line.

于 2013-06-04T15:30:08.410 回答
0

I think you want to save the total back to the same variable:

money=(money*pr)+money;
System.out.print(money);

Btw you can also express it as:

money += money*pr;

or also

money *= 1 + pr;
于 2013-06-04T15:29:31.637 回答