import java.util.ArrayList;
import java.util.Scanner;
class Loan
{
double loan;
public String toString()
{
return "Loan: " + loan;
}
}
class Frame
{
String framename;
public String toString()
{
return "Frame: " + framename;
}
}
class Circle
{
double radius;
public String toString()
{
return "Circle: " + radius;
}
}
class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Loan loan = new Loan();
Frame frame = new Frame();
Circle circle = new Circle();
loan.loan = input.nextDouble();
frame.framename = input.next();
circle.radius = input.nextDouble();
ArrayList<Object> mylist = new ArrayList<Object>();
mylist.add(loan);
mylist.add(frame);
mylist.add(circle);
System.out.println(mylist.get(0));
System.out.println(mylist.get(1));
System.out.println(mylist.get(2));
}
}
I want to make the loan and the circle return two decimal places using the toString method how can i achieve that.... the objects should be in an Arraylist of objects.I tried using System.out.printf("%.2f",mylist.get(2)) but it didnt work.
我想使贷款和圆圈使用toString方法返回两个小数位,我怎么能实现....对象应该在对象的Arraylist中。我尝试使用System.out.printf(“%。2f” ,mylist.get(2))但它没有用。
4 个解决方案
#1
0
You have to convert the double type fields to String so that the return type is correct and in the process of converting you specify the which format of the string you want in this case specify 2 decimal places.
您必须将double类型字段转换为String以使返回类型正确,并且在转换过程中指定所需字符串的格式,在这种情况下指定2个小数位。
class Loan
{
double loan;
public String toString()
{
String loanstring = String.format ("%.2f", loan);
return "Loan: " + loanstring;
}
}
class Circle
{
double radius;
public String toString()
{
String radiusstring = String.format ("%.2f", radius);
return "Circle: " + radiusstring;
}
}
#2
2
Declare your list as ArrayList<Loan> mylist = new ArrayList<Loan>();
instead of ArrayList<Object> mylist = new ArrayList<Object>();
then you can get the expected result by doing:
将列表声明为ArrayList
System.out.printf("%.2f",mylist.get(0).loan);
As an aside, ideally, all fields of a class should be private and create getters and setters where necessary. So, in this case, you can make loan
private in the Loan
class and then create a getter for it.
顺便说一句,理想情况下,一个类的所有字段都应该是私有的,并在必要时创建getter和setter。因此,在这种情况下,您可以在贷款类中私有贷款,然后为其创建一个getter。
#3
0
You are using a ArrayList of type "Object". So you can put everthing into these list.
您正在使用“对象”类型的ArrayList。所以你可以把它们放到这些列表中。
Set the type of the ArrayList to "Loan"
将ArrayList的类型设置为“Loan”
ArrayList<Loan> mylist = new ArrayList<Loan>();
Than you can get the loanvalue from listitems.
你可以从listitems获得贷款价值。
printf("%.2f",mylist.get(0).loan);
#4
0
as far as i see mylist.get(0) is an object of type Loan and not a double, thats probably why it doesn‘t work, because the printf approach is the right approach to this.
据我所知mylist.get(0)是一个Loan类型的对象而不是double,这可能就是为什么它不起作用,因为printf方法是正确的方法。
you should also make your ArrayList an ArrayList instead, it‘s cleaner and might have helped you to avoid confusing the type from the begining.
你也应该让你的ArrayList成为一个ArrayList,它更干净,可能帮助你避免混淆类型从开始。
#1
0
You have to convert the double type fields to String so that the return type is correct and in the process of converting you specify the which format of the string you want in this case specify 2 decimal places.
您必须将double类型字段转换为String以使返回类型正确,并且在转换过程中指定所需字符串的格式,在这种情况下指定2个小数位。
class Loan
{
double loan;
public String toString()
{
String loanstring = String.format ("%.2f", loan);
return "Loan: " + loanstring;
}
}
class Circle
{
double radius;
public String toString()
{
String radiusstring = String.format ("%.2f", radius);
return "Circle: " + radiusstring;
}
}
#2
2
Declare your list as ArrayList<Loan> mylist = new ArrayList<Loan>();
instead of ArrayList<Object> mylist = new ArrayList<Object>();
then you can get the expected result by doing:
将列表声明为ArrayList
System.out.printf("%.2f",mylist.get(0).loan);
As an aside, ideally, all fields of a class should be private and create getters and setters where necessary. So, in this case, you can make loan
private in the Loan
class and then create a getter for it.
顺便说一句,理想情况下,一个类的所有字段都应该是私有的,并在必要时创建getter和setter。因此,在这种情况下,您可以在贷款类中私有贷款,然后为其创建一个getter。
#3
0
You are using a ArrayList of type "Object". So you can put everthing into these list.
您正在使用“对象”类型的ArrayList。所以你可以把它们放到这些列表中。
Set the type of the ArrayList to "Loan"
将ArrayList的类型设置为“Loan”
ArrayList<Loan> mylist = new ArrayList<Loan>();
Than you can get the loanvalue from listitems.
你可以从listitems获得贷款价值。
printf("%.2f",mylist.get(0).loan);
#4
0
as far as i see mylist.get(0) is an object of type Loan and not a double, thats probably why it doesn‘t work, because the printf approach is the right approach to this.
据我所知mylist.get(0)是一个Loan类型的对象而不是double,这可能就是为什么它不起作用,因为printf方法是正确的方法。
you should also make your ArrayList an ArrayList instead, it‘s cleaner and might have helped you to avoid confusing the type from the begining.
你也应该让你的ArrayList成为一个ArrayList,它更干净,可能帮助你避免混淆类型从开始。