我正在编写一个程序,您可以在其中输入两个除数,并且您要检查的数字列表是否可以被您输入的两个数字整除。
以下是输出的示例:
Please input a command: A
A [Create a new divisible test]
[Type two divisors]: 2 6
[Input a list of numbers in one single line]: 2 4 6 9 15 18 19 25 30
Please input a command: D
D [Display the information]
( 2 4 6 9 15 18 19 25 30 ) are numbers to check
( 2 4 6 18 30 ): are divisible by 2
( 6 18 30 ): are divisible by 6
( 6 18 30 ): are divisible by both 2 and 6
我的问题是它不会检查哪个数字可以被两个除数整除,而是打印出来
( 2 4 6 9 15 18 19 25 30 ) are numbers to check
( 2 4 6 9 15 18 19 25 30 ): are divisible by 2
( 2 4 6 9 15 18 19 25 30 ): are divisible by 6
( 2 4 6 9 15 18 19 25 30 ): are divisible by both 2 and 6.
这是我的代码:
class Divisible {
private int divisor1;
private int divisor2;
public String numbers;
public Divisible() {
divisor1 = (Integer) null;
divisor2 = (Integer) null;
numbers = " ";
}
public Divisible(int div1, int div2, String num) {
this.divisor1 = div1;
this.divisor2 = div2;
this.numbers = num;
}
private boolean isDivisible1(int input) {
boolean isDivisible1 = true;
return (input % divisor1 == 0);
}
private boolean isDivisible2(int input) {
boolean isDivisible2 = true;
return (input % divisor2 == 0);
}
public String printDivisible1() {
if (this.isDivisible1(divisor1)) {
System.out.println();
System.out.println("are divisible by " + divisor1);
}
return numbers;
}
public String printDivisible2() {
if (this.isDivisible2(divisor2)) {
System.out.println(" are divisible by " + divisor2);
}
return numbers;
}
public String printDivisibleBoth() {
if (this.isDivisible1(divisor1) && this.isDivisible2(divisor2))
System.out.println(" are divisible by " + divisor1 + " and " + divisor2);
return numbers;
}
}
最佳答案
在我看来,您的代码中缺少一些步骤!
要解决这个问题,我们需要:
将数字字符串分解,将它们转换为整数,然后将它们输入到列表或数组中。
打印时迭代此列表,应用
isDivisible()
函数来确定它是否应输出。
首先我想也许我们可以让你的程序更加模块化。特别是您的 isDivisible(int input)
函数可以更改为:
private boolean isDivisible(int input, int divisor) {
return (input%divisor==0);
}
这意味着我们可以对两个除数使用相同的函数!就像 isDivisble(30, 6)
和 isDivisble(30, 2)
现在我们需要重点检查您的一串数字。您会注意到我们的新函数需要一个整数作为输入,但我们目前有一个包含所有数字的巨大字符串。我们或许可以尝试这样的函数:
String[] numArray = num.split(" ");
将字符串“num”分割为空格(“”)的部分,并将这些部分放入“numArray”数组的元素中。
好的,现在我们有了输入数组。剩下的就是将这些元素转换为整数,以便它们可以用作 isDivisible()
函数的输入。我们可以使用 Integer.valueOf(str s)
函数来做到这一点!
这就是我们解决这个问题所需的所有工具!放在一起,一个粗略的解决方案如下所示:
String[] numArray = num.split(" ");
for (int i = 0; i < numArray.length; i++) {
if (isDivisible(Integer.valueOf(numArray[i]), div1)) {
System.out.print(numArray[i] + " ");
}
}
System.out.println("): are divisible by " + div1);
更新
没有数组!?好的,在这种情况下,我认为你的老师希望你迭代输入,检查它们是否可整除,如果是,则连接一些输出字符串。
因此,让我们首先添加几个字符串,这些字符串稍后将成为代码顶部的输出:
class Divisible {
private String outputDiv1 = "( ";
private String outputDiv2 = "( ";
private String outputBoth = "( ";
现在,对于每个输出,我们只想连接可整除的数字。我们可以在没有数组的情况下通过循环遍历 String num 的字符来做到这一点,并在发现空格时将数字分开,例如:
//Since we're checking for spaces, we should probably add one to the end of our string, so we can find the last number!
num += " ";
//We need to keep a record of the previous space!
int lastSpace = -1;
for (int i = 0; i < num.length(); i++) {
if (num.charAt(i) == ' ') {
//If the current character is a space, we know everything before it and the last space was a number
//Our logic will go here!
//Currently converts our string into an Integer and prints it out
int currentNumber = Integer.parseInt(num.substring(lastSpace, i));
System.out.println(currentNumber);
//Update where the last space we found was
//The '+ 1' is so we skip the spaces!
lastSpace = i + 1;
}
}
好吧,不,我们正在迭代字符串并将它们分开,而不使用任何数组!剩下要做的就是应用我们之前制作的工具 isDivisible()
。
类似于:
if (isDivisible(currentNum, div1)) {
outputDiv1 += currentNum;
}
可以放入//我们的逻辑在这里
部分来确定是否应该将数字添加到我们的输出列表中!
最后,我们打印出完成的列表:
System.out.println(outputDiv1 + " ): are divisble by " + div1);
关于java - 检查数字列表是否可以被两个输入整除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29069995/