java程序-带变数的除数测试-我该如何在文本键盘中编写 [英] java program - divisibility test with varification - how do i write this in textpad
问题描述
我已经编写了程序,但是我有两个问题,需要帮助来更正.问题是
i have the program written however I have two problems and need assistance to correct. the problems are
1)我不希望计数器i从1到x,因为这样它将尝试对小于实际用户输入的每个数字进行除数测试.我需要它从2开始到12,直到您尝试仅2-12的测试. 2)可除性测试是正确的,它适用于每个数字,但是那不是程序说明中要求的内容.我需要为每个可除性测试实现上述算法. (我已经研究过但不确定如何做到)
1) i do not want the counter i to go from 1 to x because then it would try the divisibility test for every number less than the actual user input. I need it to start i from 2 and go until 12 so that you try the test for 2-12 only. 2) The divisibility test is correct and it works for every number but thats not whats asked in the program description. I need to implement the mentioned algorithms for each divisibility test. (which i have researched but am not sure how to do it)
这是我的代码,下面是作业:
here is my code and below is the assignment:
import java.io.PrintStream;
import java.util.Scanner;
public class rwsFinalExam
{
public static void main(String [] args)
{
Scanner scanner = new Scanner( System.in ); //allows input from concole
PrintStream out = System.out; //assigning System.out to PrintStream
out.print( "Input a valid whole number: " ); //ouput directions for end user to enter a whole number
String input = scanner.next(); //holds end user input
int number; //data type and variable
try
{
number = Integer.parseInt(input); //assinging value to number //integer.parseInt method converts string to int
}
catch (Exception e)
{
out.println(input + " is not a valid number");
return;
}
if (number < 0)
{
out.println(number + " is not a valid number");
return;
}
printDivisors(number);
}
private static void printDivisors(int x)
{
PrintStream out = System.out;
for (int i=1; i<x; i++)
{
if (isDivisibleBy(x, i)) //checking divisibility
{
out.println(x + " is divisible by " + i); //output when value is divisible
}
else
{
out.println(x + " is not divisible by " + i); //output when value not divisible
}//end if else
}//end for
}//end private static
private static Boolean isDivisibleBy(int x, int divisor)
{
while (x > 0)
{
x -= divisor;
if (x == 0)
{
return true;
}
}
return false;
}//end
}//end class rwsFinalExam
我需要我的输出看起来像这样.不能使用%模运算符
i need my output to look like this.. cannot use % modulus operator
输入有效的整数:ABC
Input a valid whole number: ABC
输出:ABC不是有效的数字.按任意键继续. .
Otuput: ABC is not a valid number Press any key to continue . . .
输入有效的整数:-1
Input a valid whole number: -1
输出:-1不是有效数字按任意键继续. .
Output: -1 is not a valid number Press any key to continue . . .
输入有效的整数:15
Input a valid whole number: 15
输出:15不可被2整除.15不可被3整除.15不可被4整除.15不可被6整除.15不可被6整除.15不可被7整除.15不可被7整除8. 15不能被9.整除.15不能被10整除.15不能被11整除.15不能被12整除.按任意键继续. .
Output: 15 is not divisible by 2. 15 is divisible by 3. 15 is not divisible by 4. 15 is divisible by 5. 15 is not divisible by 6. 15 is not divisible by 7. 15 is not divisible by 8. 15 is not divisible by 9. 15 is not divisible by 10. 15 is not divisible by 11. 15 is not divisible by 12. Press any key to continue . . .
推荐答案
要测试a
是否可以在不使用a % b
的情况下被b
整除,可以执行以下操作:
To test whether a
is divisible by b
without using a % b
, you could do the following:
boolean divisible = (a / b * b == a);
a / b
是整数(即截断)除法的原因.
The reason this works that that a / b
is the integer (that is, truncating) division.
这篇关于java程序-带变数的除数测试-我该如何在文本键盘中编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!