gpt4 book ai didi

java - 我可以在字符串上使用 mod 吗?

转载 作者:行者123 更新时间:2023-11-30 05:39:29 25 4
gpt4 key购买 nike

所以我得到这个任务来完成一个名为“remove5List”的方法。此方法必须删除给定 String 类型的 LinkedList 中 mod5==0 的每个对象。

我的方法是使用 mod 运算符,但我猜它只适用于整数。现在我已经到了不知道如何继续的地步。

这是我在这里发表的第一篇文章,我是一个完全的初学者,所以如果我做错了什么,请纠正我。

//编辑:列表中填充了数字


static void remove5List(List<String> list)
{
ListIterator<String> iter = list.listIterator();


while(iter.hasNext()) {
//here I would like to check if I can divide the obj by 5
}
}

最佳答案

请将整数保留为整数

static void remove5List(List<String> list) {
ListIterator<String> iter = list.listIterator();

while (iter.hasNext()) {
boolean mod = false;
try {
int number = Integer.parseInt(iter.next());
if (number % 5 == 0) {
mod = true;
}
} catch (Exception e) {
}
//if mod == true ,you can divide the obj by 5
}
}

关于java - 我可以在字符串上使用 mod 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55951922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com