Java 如何使用循环打印输入的因子?
这是我的乱七八糟的代码。我必须写一个输入大于3的正整数的程序。验证整数是否实际大于3。然后打印大于其乘积小于或等于输入数字的所有可能的正整数对Java 如何使用循环打印输入的因子?,java,loops,factors,Java,Loops,Factors,这是我的乱七八糟的代码。我必须写一个输入大于3的正整数的程序。验证整数是否实际大于3。然后打印大于其乘积小于或等于输入数字的所有可能的正整数对 ex. If 24 is the input. It would print: 4 = 2 x 2 6 = 2 x 3 8 = 2 x 4 10 = 2 x 5 12 = 2 x 6 14 = 2 x 7 16 = 2 x 8.... 9 = 3 x 3 12 = 3 x 4.. 24 = 3 x 8... all the way to 24 = 4
ex. If 24 is the input.
It would print:
4 = 2 x 2
6 = 2 x 3
8 = 2 x 4
10 = 2 x 5
12 = 2 x 6
14 = 2 x 7
16 = 2 x 8....
9 = 3 x 3
12 = 3 x 4..
24 = 3 x 8...
all the way to
24 = 4 x 6
import java.util.Scanner;
public class Factors {
public static void main(String[] args) {
// Define Variables
Scanner input = new Scanner(System.in);
int i = 0;
int j = 0;
int k = 2;
int product = 0;
// Ask for input/loop
while (i < 3) {
System.out.println("Please enter an integer greater than 3");
i = input.nextInt();
}
while (product < i) {
if (product == i) { j++; k = 2;
for (j = 2; product < i; k++) {
product = j * k;
System.out.println(product + " = " + j + " x " + k);
if (product == i) { j++; k = 2;
}
}
}
}
}
}
例如,如果24是输入。
它将打印:
4=2x2
6=2 x 3
8=2 x 4
10=2 x 5
12=2 x 6
14=2 x 7
16=2 x 8。。。。
9=3 x 3
12=3 x 4。。
24=3 x 8。。。
一直到
24=4 x 6
导入java.util.Scanner;
公共阶级因素{
公共静态void main(字符串[]args){
//定义变量
扫描仪输入=新扫描仪(System.in);
int i=0;
int j=0;
int k=2;
int乘积=0;
//请求输入/循环
而(i<3){
System.out.println(“请输入大于3的整数”);
i=input.nextInt();
}
while(产品
公共类因素{
公共静态void main(字符串[]args){
//定义变量
扫描仪输入=新扫描仪(System.in);
int i=0;
int乘积=0;
//请求输入/循环
而(i<3){
System.out.println(“请输入大于3的整数”);
i=input.nextInt();
}
对于(int j=2;j如果(j欢迎来到SO。而你的问题恰恰是…?看起来你想要一个inputValue,并列出所有小于此inputValue的整数乘积。我说的对吗?如果是,我确实有一段代码可以做到这一点。但首先,你应该问自己:我想做什么?我当前的代码做什么?对不起,我想我可以做得更多清楚。有了上面的代码,我可以得到所有因子的输出,以2so2x=4…整个过程直到我的输入值是什么。但是,我不知道如何做其余的,所以我可以做3,4等。我对下一步去哪里,甚至做什么都很困惑。我需要它打印出所有可能的因子,而不重复任何,比如2x3=6和3x2=6。这太完美了,我欠你一条命。这很简单,但嵌套循环让我很困惑。非常感谢。
public class Factors {
public static void main(String[] args) {
// Define Variables
Scanner input = new Scanner(System.in);
int i = 0;
int product = 0;
// Ask for input/loop
while (i < 3) {
System.out.println("Please enter an integer greater than 3");
i = input.nextInt();
}
for (int j = 2; j < i / 2; j++) {
for (int k = 2; k < i / 2; k++) {
if (j <= k && j * k <= i)
System.out.println(j * k + " = " + j + "*" + k);
}
}
// while (product < i) {
// if (product == i) {
// j++;
// k = 2;
// for (j = 2; product < i; k++) {
// product = j * k;
// System.out.println(product + " = " + j + " x " + k);
// if (product == i) {
// j++;
// k = 2;
// }
// }
// }
// }
}