gpt4 book ai didi

Java Loop - 删除具有 4 个或更多相同数字的数字

转载 作者:行者123 更新时间:2023-12-01 11:45:00 25 4
gpt4 key购买 nike

我想知道如何创建一个循环,打印 200000 - 900000 之间的所有数字并删除所有数字,例如 222244。

到目前为止,这就是我所拥有的:

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class alg {

public void algorithm(){
}

public static void main(String []args){
PrintWriter file = null;
try {
file = new PrintWriter("output.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int x;
for (x = 200000; x < 900000; x++){
file.println(x);
}
}
file.close();
}

最佳答案

// return true if integer has 4 or more of same digit, otherwise false
static boolean check4(int i) {
int[] charCounts = new int[10];
for (char c : String.valueOf(i).toCharArray()) {
if (++charCounts[c-'0'] == 4) return true;
}
return false;
}

关于Java Loop - 删除具有 4 个或更多相同数字的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29220346/

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