本文介绍了如何使用java中的tostring()方法打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 创建一个名为AccountSavings的类。这个类有一个静态双变量 ,它存储了所有账户持有人的年利率。变量的名称是 annualInterestRate。该类还有另一个名为savingsBalance 的双变量,它存储当前账户的余额。 a。编写构造函数以创建具有指定余额的帐户。添加 验证余额是否大于0.0。如果它小于0.0则抛出 例外。 b。编写一个非静态calculateMonthlyInterest方法,通过将savingsBalance乘以annualInterestRate除以12 来计算每月 利息 - 应将利息添加到savingsBalance。 c。编写一个名为modifyInterestRate的静态方法来设置年利率。 添加验证费率是否大于等于0.0且小于或 等于1.0。否则,抛出异常。 d。编写一个toString方法,以字符串格式返回savingsBalance。 之后,创建AccountSavingsTest类。从类中创建两个对象 AccountSavings,余额为$ 2000.00和$ 3000.00。然后,将利率设置为4%,然后计算每个对象每12个月的月利率,并使用toString方法为每个对象打印 新余额。 UPLOAD AccountSavings.java AND AccountSavingsTest.java。 我尝试过: 我想用toString()方法打印输出。我试过Account1.toString()但它不起作用,我也尝试格式化超类和子类中的输出。我可以使用get方法来显示输出,但它不是它应该的方式。你能指出我的错误吗?非常感谢你! // AccountSavings.java public class AccountSavings { private static double annualInterestRate; private double savingsBalance; // 构造函数 public AccountSavings( double savingsBalance){ // 验证savingsBalance if (savingsBalance< 0 。 0 ){ throw new IllegalArgumentException( 余额必须为> 0.0\" ); } 此 .savingsBalance = savingsBalance; } // 结束构造函数 public double getSavingsBalance(){ return savingsBalance; } public double calculateMonthlyInterest(){ double 结果; result =( double )(savingsBalance * annualInterestRate / 12 ); return savingsBalance = savingsBalance + result; } // 结束方法 public static void modifyInterestRate( double newRate){ if (newRate< = 0 。 0 || newRate> = 1 。 0 ){ throw new IllegalArgumentException( 年费率必须>> 0.0且); } annualInterestRate = newRate; } // 结束方法 @ Override public String toString(){ return String .format( %。2f%n,getSavingsBalance()); } // 结束方法 } // end class // AccountSavingsTest public class AccountSavingsTest { public static void main( String [] args){ AccountSavings Account1 = new AccountSavings( 2000 。 00 ); AccountSavings Account2 = new AccountSavings( 3000 。 00 ); Account1.modifyInterestRate( 0 。 04 ); Account2.modifyInterestRate( 0 。 04 ); System.out.println( Account1的每月余额为4%); System.out.println( 余额:); for ( int i = 0 ; i< 12; i ++){ Account1.calculateMonthlyInterest(); System.out.println( 月 +(i + 1)+ : + String .format( $%。2f,Account1.getSavingsBalance())); } System.out.println(); System.out.println( Account2的每月余额为4%); System.out.println( 余额:); for ( int i = 0 ; i< 12; i ++){ Account2.calculateMonthlyInterest(); System.out.println( 月 +(i + 1)+ : + String .format( $%。2f,Account2.getSavingsBalance())); } } } 解决方案 3000.00。然后,将利率设置为4%,然后计算每个对象每12个月的月利率,并使用toString方法为每个对象打印 新余额。 UPLOAD AccountSavings.java AND AccountSavingsTest.java。 我尝试过: 我想用toString()方法打印输出。我试过Account1.toString()但它不起作用,我也尝试格式化超类和子类中的输出。我可以使用get方法来显示输出,但它不是它应该的方式。你能指出我的错误吗?非常感谢你! // AccountSavings.java public class AccountSavings { private static double annualInterestRate; private double savingsBalance; // 构造函数 public AccountSavings( double savingsBalance){ // 验证savingsBalance if (savingsBalance< 0 。 0 ){ throw new IllegalArgumentException( 余额必须为> 0.0\" ); } 此 .savingsBalance = savingsBalance; } // 结束构造函数 public double getSavingsBalance(){ return savingsBalance; } public double calculateMonthlyInterest(){ double 结果; result =( double )(savingsBalance * annualInterestRate / 12 ); return savingsBalance = savingsBalance + result; } // 结束方法 public static void modifyInterestRate( double newRate){ if (newRate< = 0 。 0 || newRate> = 1 。 0 ){ throw new IllegalArgumentException( 年费率必须>> 0.0且); } annualInterestRate = newRate; } // 结束方法 @ Override public String toString(){ return String .format( %。2f%n,getSavingsBalance()); } // 结束方法 } // end class // AccountSavingsTest public class AccountSavingsTest { public static void main( String [] args){ AccountSavings Account1 = new AccountSavings( 2000 。 00 ); AccountSavings Account2 = new AccountSavings( 3000 。 00 ); Account1.modifyInterestRate( 0 。 04 ); Account2.modifyInterestRate( 0 。 04 ); System.out.println( Account1的每月余额为4%); System.out.println( 余额:); for ( int i = 0 ; i< 12; i ++){ Account1.calculateMonthlyInterest(); System.out.println( 月 +(i + 1)+ : + String .format( Create a class named AccountSavings. This class has a static double variablewhich stores the annual interest rate for all account holders. The name of variable isannualInterestRate. The class also has another double variable named savingsBalancewhich stores balance for current account.a. Write a constructor to create an account with specified balance. Add avalidation whether the balance is greater than 0.0. If it is less than 0.0 then throwan exception.b. Write a non-static calculateMonthlyInterest method to calculate the monthlyinterest by multiplying the savingsBalance by annualInterestRate divided by 12– the interest should be added to savingsBalance.c. Write a static method named modifyInterestRate to set the annual interest rate.Add a validation whether the rate is greater than equal to 0.0 and less than orequal 1.0. Otherwise, throw an exception.d. Write a toString method which returns savingsBalance in a string format.After that, create AccountSavingsTest class. Create two objects from the classAccountSavings with balances $2000.00 and $3000.00. Then, set the interest rate to 4%,then calculate the monthly interest rate for each 12 months for each object and print thenew balances with toString method for each object. UPLOAD AccountSavings.javaAND AccountSavingsTest.java.What I have tried:I want to print the output using toString() method. I have tried "Account1.toString() but it won't work, I also tried to format the output in the superclass and subclass. I can use the get method to display the output, but it's not the way it's supposed to be. Can you point out my errors? Thank you so much!!!//AccountSavings.javapublic class AccountSavings {private static double annualInterestRate;private double savingsBalance;//constructorpublic AccountSavings (double savingsBalance) {//validate savingsBalanceif (savingsBalance < 0.0) {throw new IllegalArgumentException ("The balance must be > 0.0");}this.savingsBalance = savingsBalance;}//end constructorpublic double getSavingsBalance() {return savingsBalance;}public double calculateMonthlyInterest() {double result;result = (double) (savingsBalance * annualInterestRate / 12);return savingsBalance = savingsBalance + result;}//end methodpublic static void modifyInterestRate (double newRate){if (newRate <= 0.0 || newRate >= 1.0) {throw new IllegalArgumentException ("Annual rate must > 0.0 and < 1.0");}annualInterestRate = newRate;}//end method@Overridepublic String toString () {return String.format ("%.2f%n",getSavingsBalance());}//end method}//end class//AccountSavingsTestpublic class AccountSavingsTest {public static void main (String [] args) {AccountSavings Account1 = new AccountSavings (2000.00);AccountSavings Account2 = new AccountSavings (3000.00);Account1.modifyInterestRate(0.04);Account2.modifyInterestRate(0.04);System.out.println("Monthly balances for Account1 at 4%");System.out.println("Balances: ");for (int i = 0; i <12; i++){Account1.calculateMonthlyInterest();System.out.println("Month " + (i+1) + ": " + String.format ("$%.2f", Account1.getSavingsBalance()));}System.out.println();System.out.println("Monthly balances for Account2 at 4%");System.out.println("Balances: ");for (int i = 0; i <12; i++){Account2.calculateMonthlyInterest();System.out.println("Month " + (i+1) + ": " + String.format ("$%.2f", Account2.getSavingsBalance()));}}} 解决方案 这篇关于如何使用java中的tostring()方法打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-11 20:11