№1.import java.util.Scanner;
public class problem1{
    public void save(double savingAmount,double interestRate)
      {Scanner in=new Scanner(System.in);             
       System.out.println("Your saving balance?");
       savingAmount=in.nextDouble();                      
       System.out.println("Interest rate?");     
       interestRate=in.nextDouble();}
    public int calculateYears(double savingAmount,double interestRate)                                     
     { int years=0;                              
       double amount=0;
       while(amount<=2000)                                         
        {amount=savingAmount+interestRate*savingAmount;
         amount=amount*interestRate+amount; 
         years++;}
       System.out.println("It takes at least"+years+"years to double your savings.");   
       return (int)amount;}
    
    public static void main(String[] args){            
      problem1 myproblem=new problem1();
      myproblem.calculateYears(1.0,2.0);          //这行(1.0,2.0)是问别人的,不知怎么来的
     }
}
/*这个程序不知什么错误cmd上不出结果*/№2.public class Account { 
    protected double balance; // Constructor to initialize balance  
    public Account( double amount ) 
    { balance = amount; } // Overloaded constructor for empty balance
    public Account()
    { balance = 0.0;  } 
    public void deposit( double amount ) 
    { balance += amount; } 
    public double withdraw( double amount ) 
    {  if (balance >= amount) // See if amount can be withdrawn 
      { balance -= amount;
        return amount;   } 
       else // Withdrawal not allowed      
       return 0.0;    } 
    public double getbalance() 
    { return balance; }
    public static void main(String[] args)
     { Account myAccount=new Account();
       myAccount.Account(0.0);  //cmd说这找不到符号 myAccount.Account(0.0)
                                                           ^
       myAccount.Account();     //错误同上   myAccount.Account()
                                                     ^
       myAccount.deposit(0.0);
       myAccount.withdraw(0.0);
       myAccount.getbalance();
     }
}
我总是感觉2个程序  错误都是main()引起的而且都是程序中
含public int/public double  某某()
java高手请指点一下 这样程序的main()怎么写啊??  

解决方案 »

  1.   

    首先说第一个问题,问题在于while循环当中,它是一个死循环,因为savingAmout和interestRate是来自于参数,每次进入循环体当中,这两个值不变,也就是说amount的值始终是不会改变的,改变的仅是years的值。
    解决方法:将  amount=savingAmount+interestRate*savingAmount; 
    这条语句放在while循环之前,问题得到解决。
    第二个问题有时间再给你研究吧!
      

  2.   

    myproblem.calculateYears(1.0,2.0);          //这行(1.0,2.0)是问别人的,不知怎么来的 1.0和2.0是参数 对应你上面的calculateYears(double savingAmount,double interestRate)方法.
      

  3.   

    1.你的第一个程序的while循环里面,每做一次循环,amount的值都会被两个参数的算式重新赋值,而这两个参数又是不变的,所以你的amount也不会变,那就成了死循环了···
    2.第二个程序中Accomut()等是构造方法,怎能通过对象来调用呢,你应该在创建对象的时候就给方法赋值
      

  4.   

    你们能不能正面回答我的问题  嗯?
    1.0和2.0是参数 对应上面的calculateYears(double savingAmount,double interestRate)方法. 我知道
     
    它是算出来的,还是随便举得2个数,如果是随便举的,可不可以写成  (0.0,0.0)?