你好,,根据你的需求,,我是写了一个实体类,,然后用list遍历输出,,代码如下:
public class User {
private String name;
private int age;
private int pencilNum;
private int totalPrice;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPencilNum() {
return pencilNum;
}
public void setPencilNum(int pencilNum) {
this.pencilNum = pencilNum;
}
public int getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(int totalPrice) {
this.totalPrice = totalPrice;
}
public String toString(){
return name + " " + age + " " +pencilNum + " " +totalPrice+"元";
}
}
public class TestPencil {
public static void main(String[] args) {
List users=new ArrayList();
String info="";
String name="";
int age=0;
int pencilNum=0;
int totalPrice=0;
Scanner in=new Scanner(System.in);
while(in.hasNextLine()){
User u=new User();
info=in.nextLine();
if(info.equals("退出")){
in.close();
break;
}
String message[]=info.split(" ");
name=message[0];
age=Integer.parseInt(message[1]);
pencilNum=Integer.parseInt(message[2]);
totalPrice=pencilNum*2;
u.setName(name);
u.setAge(age);
u.setPencilNum(pencilNum);
u.setTotalPrice(totalPrice);
users.add(u);
}
in.close();
for(User u:users){
System.out.println(u);
}
}
}
最后输入退出的时候就可以看到结果了