gpt4 book ai didi

java - 如何检查整数中有多少个除数以及​​如何将阶乘方程表示为奇数?

转载 作者:行者123 更新时间:2023-11-30 11:54:28 27 4
gpt4 key购买 nike

所以我的作业告诉我写一个奇数的方程式,当且仅当数字是奇数时,方程式必须是阶乘。

在我的脑海里,结构化是这样的(直到我不知道如何使用阶乘)

import java.util.*;

public class apple {
public static void main(String args []) {
Scanner var = new Scanner(System.in);
int m;
System.out.println("Type in your first number: ");
m = var.nextInt();
if (m==0){ //i don't know if m==0 express the condition to be whole numbers, please tell me which is.
//here I need to check how many divisors there is for my statement
}else if //Again, i don't know how to proceed here, i need to place the condition if M is ODD, how?
//here i need to state (what i guess) the equation of factorial number (in which case, if and only if is odd)

// and than print the results out. That is all the job it needs to be done.
}
}
}

最佳答案

所以如果你有一个奇数,你似乎想打印出一个数的阶乘,如果它是偶数,你想打印出该数的约数。您尚未指定一种表示除数的方法,因此您可以采用以下一种方法:

Scanner var = new Scanner(System.in);
int m;
long x=1; //for the factorial, we want to store in a long to combat data overflow
System.out.println("Type in your first number: ");
m = var.nextInt();
//if the input is odd we calculate its factorial
if (m%2==1){
for (int i = 1;i<=m;i++)
x*=i;
System.out.println(m+"!: "+x);
}
else{
System.out.println("1 is a divisor for "+m);
System.out.println("2 is a divisor for "+m);
if (m%3==0)
System.out.println("3 is a divisor for "+m);
//and so on for more divisors of m
}

关于java - 如何检查整数中有多少个除数以及​​如何将阶乘方程表示为奇数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5706489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com