这里我需要检查数字中的每个数字,对于一个范围应该可以被3整除,这意味着当我输入20和40时,代码需要验证数字的每个数字在20到40之间,并且它应该显示30,33,36,39
我尝试做的是获取代码的最后一位并检查它是否可以整除,我记住了方式
class Main
{
public static void main(String[] args) {
System.out.println("Enter the range for shipment numbers :");
int n;
int Duplicate = 0;
int count = 0;
Scanner sc = new Scanner(System.in);
int range1 = sc.nextInt();
int range2 = sc.nextInt();
for(int i = range1; i <= range2; i++){
int num = i;
int length = String.valueOf(i).length();
Duplicate = 0;
for(int j = 1; j <= length; j++)
{
n = num % 10;
num = num / 10;
if(n % 3 == 0 && i % 3 == 0 ){
Duplicate = Duplicate+1;
count = count+1;
System.out.println(rep);
if (Duplicate <= 1)
System.out.println(i);
}
}
if (count <= 0)
System.out.println("Shipment numbers unavailable");
if (count <= 0)
break;
}
}
}
答案 0 :(得分:1)
为了检查每个数字是否可以被3整除,您可以用以下代码替换您的循环。它只是从右到左的数字,并使用布尔值来记住到目前为止遇到的所有数字是否都可以使用逻辑AND整除3。
for (int i = range1; i <= range2; i++) {
int num = i;
boolean isDivisible = true;
while (num > 0) {
// get last (rightmost) digit to inspect
int n = num % 10;
isDivisible = isDivisible && (n % 3 == 0);
// integer-divide by 10 to prepare for next round
num = num / 10;
}
if (isDivisible) {
System.out.println(i);
}
}
所显示代码的弱点在于,就像你的原始代码一样,它会查看整个数字,只要3被整除的标准没有得到满足就可以拯救它,但是这样做了容易修复。
答案 1 :(得分:0)
这是一种方法。将数字转换为String。将字符串拆分为String []然后将每个字符转换回int并检查它是否可以被3整除。
getrlimit(RLIMIT_NOFILE)
答案 2 :(得分:0)
你的主要问题是你使用模运算符做奇怪的事情。我们不在乎i
是否可以除以3。
您还使用了很多我不理解的变量,例如Duplicate
。
或者
for(int j = 1; j <= length; j++) {
num = num / 10;
}
我喜欢写作
while (num > 0) {
num = num / 10;
}
它只是使用较少的变量。
无论如何,这里是完整的代码,我添加了一些布尔值来使它看起来像请求。
class Main
{
public static void main(String[] args) {
System.out.println("Enter the range for shipment numbers :");
int n;
boolean first = true;
Scanner sc = new Scanner(System.in);
int range1 = sc.nextInt();
int range2 = sc.nextInt();
for(int i = range1; i <= range2; i++){
int num = i;
boolean numberIsAwesome = true;
while (num > 0)
{
n = num % 10;
if(n % 3 != 0){
numberIsAwesome = false;
}
num = num / 10;
}
if (numberIsAwesome) {
if (!first) {
System.out.print(", ");
}
System.out.print(i);
first = false;
}
}
System.out.println();
}
}
答案 3 :(得分:0)
我建议使用String.valueOf(i)
来接收数字位数:
public static Set<Integer> getDivisibleBy(int from, int to, int div) {
IntPredicate isDivisibleBy = val -> {
if (val % div != 0)
return false;
for (char ch : String.valueOf(val).toCharArray())
if ((ch - '0') % div != 0)
return false;
return true;
};
return IntStream.rangeClosed(from, to).filter(isDivisibleBy).boxed().collect(Collectors.toCollection(TreeSet::new));
}
答案 4 :(得分:0)
我跳过了扫描器部分,但是利用一些Java 8流和lambda的代码可以大大减少代码并提高可读性和可维护性;
IntStream.rangeClosed(20, 40)
.boxed()
.filter(digit -> {
boolean wholeDivisable = digit % 3 == 0;
boolean partlyDivisable = Arrays.stream(digit.toString().split(""))
.allMatch(strDigit -> Integer.valueOf(strDigit) % 3 == 0);
return wholeDivisable && partlyDivisable;
})
.forEach(System.out::println);
IntStream.rangeClosed:
创建一个介于20到40之间的整数流
boxed():
将int int更改为Integer,因为我们希望在partialDivisable检查中将其拆分为字符串。 toString()
的读数优于(digit+"").split("")
。
filter:
过滤整数流,同时检查可分割的整体+每个数字
forEach:
,打印到sout