在groovy中使用list.each打印最后的值而不是所有的值 [英] Last value is printed instead of all values using list.each in groovy

查看:677
本文介绍了在groovy中使用list.each打印最后的值而不是所有的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我纠正错误。请帮助理解错误并指导我解决。

资产名称:
笔记本电脑
资产编号:
1
资产模型:
Pavilion笔记本
指定日期:
28/08/2017
价格:
62000
是否继续?(是/否)


资产名称:
演讲者
资产编号:
2
资产模型:
5.1
指定日期:
12/07/2017
价格:
12000
是否继续?(是/否)



Asset.No:1 |名称:笔记本电脑|型号:Pavilion笔记本|上次分配日期:28-08-2017 |价格:62000.0



Asset.No:2 |名称:演讲者|型号:5.1 |上次分配日期:12-07-2017 |价格:12000.0

  class Main 
{
static void main(String [] args)
{
Asset asset = new Asset()
List list = new ArrayList()
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
String userInput =是
while(userInput.equalsIgnoreCase(Yes))
{
println资产名称:
asset.name = br.readLine()
println 资产编号:
asset.assetNumber = Integer.parseInt(br.readLine())
println资产模型:
asset.model = br.readLine()
println分配日期:
asset.lastAssignedDate = Date.parse(dd / MM / yyyy,br.readLine())
printlnPrice:
asset.price = Double .parseDouble(br.readLine())
list.add(资产)
println是否继续?(是/否)
userInput = br.readLine()
}
list.each
{
println it
}
}
}

class资产
{
def名称
def资产编号
def模型
def lastAssignedDate
def价格

字符串toString ()
{
sprintfAsset.No:+ assetNumber +|名称:+ name +|型号:+ model +| Last Last Assigned Date:+ lastAssignedDate.format(dd-MM-yyyy)+|价格:+ price.round(2)

}
}


解决方案

您的错误描述存在误导性,您将您的值存储在同一个资产中,并将其重复添加到您的列表中。所以你的列表中包含了最后一笔资产的输入时间,将 def asset = new Asset()移到你的循环中去修正。


Please help me to correct error. Please help to understand the error and guide me to resolve.

Asset name: Laptop Asset number: 1 Asset model: Pavilion Notebook Assigned date: 28/08/2017 Price: 62000 Do you want to continue?(Yes/No) Yes

Asset name: Speakers Asset number: 2 Asset model: 5.1 Assigned date: 12/07/2017 Price: 12000 Do you want to continue?(Yes/No) No

Asset.No : 1 | Name : Laptop | Model : Pavilion Notebook | Last Assigned Date : 28-08-2017 | Price : 62000.0

Asset.No : 2 | Name : Speakers | Model : 5.1 | Last Assigned Date : 12-07-2017 | Price : 12000.0

class Main
{
    static void main(String[] args)
    {
        Asset asset = new Asset()
        List list = new ArrayList()
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
        String userInput = "Yes"
        while(userInput.equalsIgnoreCase("Yes"))
        {
            println "Asset name:"
            asset.name  = br.readLine()
            println "Asset number:"
            asset.assetNumber  = Integer.parseInt(br.readLine())
            println "Asset model:"
            asset.model  = br.readLine()
            println "Assigned date:"
            asset.lastAssignedDate  = Date.parse("dd/MM/yyyy", br.readLine())
            println "Price:"
            asset.price = Double.parseDouble(br.readLine())
            list.add(asset)
            println "Do you want to continue?(Yes/No)"
            userInput = br.readLine()
        }
        list.each
        {
            println it
        }
    }
}

class Asset
{
    def name
    def assetNumber
    def model
    def lastAssignedDate
    def price

    String toString()
    {
        sprintf "Asset.No : "+assetNumber+" | Name : "+name+"| Model : "+model+" | Last Assigned Date : "+lastAssignedDate.format("dd-MM-yyyy")+" | Price : "+price.round(2)

    }
}

解决方案

Your error description is missleading. You store your values in the same asset and add that again and again in your list. So your list contains "input times" the last asset. Move the def asset = new Asset() into your loop to fix that.

这篇关于在groovy中使用list.each打印最后的值而不是所有的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆