我正在编写一个程序,您输入两个除数,并且要检查的数字列表可以被您输入的两个数字整除。
以下是输出结果的示例:
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;
}
}
答案 0 :(得分:1)
在我看来,您似乎错过了代码中的几个步骤!
要解决这个问题,我们需要:
打破数字串,并将它们转换为整数,然后将它们输入列表或数组。
在打印时重复此列表,应用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(" ");
拿走我们的String' num'并在有空间的地方将它分成几块(""),并将这些部分放入“数字”的元素中。阵列。
好的,现在我们有了一系列输入。剩下的就是将这些元素转换为整数,以便它们可以用作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;
}
可以放入//Our logic goes here
部分,以确定是否应将数字添加到我们的输出列表中!
最后,我们打印完成的清单:
System.out.println(outputDiv1 + " ): are divisble by " + div1);