Java 多重nCr计划
好的,所以我必须创建一个nCr程序 我需要允许用户输入他们希望计算的nCr值的数量(介于2和12之间)。 然后相应地输入n和r值,确保n大于r(n>r) 之后,我需要计算nCr值并将其存储在数组中,然后输出这些值 我希望以2d数组的格式输出值,格式如下: n | c | nCr 到目前为止,我的代码如下:Java 多重nCr计划,java,arrays,ncr,Java,Arrays,Ncr,好的,所以我必须创建一个nCr程序 我需要允许用户输入他们希望计算的nCr值的数量(介于2和12之间)。 然后相应地输入n和r值,确保n大于r(n>r) 之后,我需要计算nCr值并将其存储在数组中,然后输出这些值 我希望以2d数组的格式输出值,格式如下: n | c | nCr 到目前为止,我的代码如下: public class nCr { public static void main (String[] args) { int nCrValues;
public class nCr {
public static void main (String[] args) {
int nCrValues;
System.out.println("Enter amount of nCr values you wish to calculate: ");
nCrValues = input();
while ((nCrValues < 2) || (nCrValues > 10)){
System.out.println("Must be between 2 and 12 inclusive");
nCrValues = input();
}//END while
values(nCrValues);
}//END main
public static void values(int nCrValues){
//I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
int n, r;
System.out.println("Enter n value: ");
n = input();
System.out.println("Enter r value: ");
r = input();
calculatenCr(n,r);
}//END values
public static int calculatenCr(int n, int r){
int nCr;
nCr = fact(n) / (fact(n-r) * fact(t));
//store nCr values in array
}//END calculatenCr
public static int fact(int num){
int count;
int factor = 1;
for (count = 1; count <= num; count++){
factor *= count;
}//END for
return factor;
}//END fact
public static int input(){
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
while (input < 0){
System.out.println("Must be a positive number.")'
input = sc.nextInt();
}//END while
return input;
}//END input
}//END CLASS
公共类nCr{
公共静态void main(字符串[]args){
int值;
System.out.println(“输入您希望计算的nCr值的数量:”;
nCrValues=input();
而((NCR值<2)| |(NCR值>10)){
System.out.println(“必须介于2和12之间,包括2和12”);
nCrValues=input();
}//结束时
价值观(NCR价值观);
}//端干管
公共静态无效值(int值){
//我知道我需要一个循环来输入n和r的值作为数组,但不知道如何输入
int n,r;
System.out.println(“输入n值:”);
n=输入();
System.out.println(“输入r值:”);
r=输入();
计算r(n,r);
}//最终值
公共静态整数计算器(整数n,整数r){
int nCr;
nCr=事实(n)/(事实(n-r)*事实(t));
//在数组中存储nCr值
}//末端计算器
公共静态int事实(int num){
整数计数;
整数因子=1;
对于(count=1;count不完全理解您是否只想打印组合的数量,而不是实际的组合本身,但这是您的代码所指示的,所以这是我完成它的方式:
public class Ncr {
public static void main (String[] args) {
int nCrValues;
System.out.println("Enter amount of nCr values you wish to calculate: ");
nCrValues = input();
while ((nCrValues < 2) || (nCrValues > 10)){
System.out.println("Must be between 2 and 12 inclusive");
nCrValues = input();
}//END while
for (int[] res : values(nCrValues)) {
System.out.println(String.format("%d | %d | %d", res[0], res[1], res[2]));
}
}//END main
public static int[][] values(int nCrValues){
//I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
int[][] res = new int[nCrValues][3];
for (int i = 0; i < nCrValues; i++) {
int n, r;
System.out.println("Enter n value: ");
n = input();
System.out.println("Enter r value: ");
r = input();
res[i] = new int[]{n, r, calculatenCr(n, r)};
}
return res;
}//END values
public static int calculatenCr(int n, int r){
return fact(n) / (fact(n-r) * fact(r));
}//END calculatenCr
public static int fact(int num){
int count;
int factor = 1;
for (count = 1; count <= num; count++){
factor *= count;
}//END for
return factor;
}//END fact
public static int input(){
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
while (input < 0){
System.out.println("Must be a positive number.");
input = sc.nextInt();
}//END while
return input;
}//END input
}//END CLASS
公共类Ncr{
公共静态void main(字符串[]args){
int值;
System.out.println(“输入您希望计算的nCr值的数量:”;
nCrValues=input();
而((NCR值<2)| |(NCR值>10)){
System.out.println(“必须介于2和12之间,包括2和12”);
nCrValues=input();
}//结束时
对于(int[]res:values(nCrValues)){
System.out.println(String.format(“%d |%d |%d”,res[0],res[1],res[2]);
}
}//端干管
公共静态int[][]值(int值){
//我知道我需要一个循环来输入n和r的值作为数组,但不知道如何输入
int[]res=新的int[ncr值][3];
对于(int i=0;i for(count=1;count上的问题从来都不是紧急的。你不能强迫我们快点。但是我如何输入x个数量的'n'和'r'以及检查n>r?@GhostCat我想你在标记为重复时就跳了枪(就像这里的mods经常做的那样)。OP问题的核心似乎是另一个问题。@kaqqao正确,我想输入x个nCr值来计算。从那里输入n和r,根据我的x是什么(例如,我想要4个nCr值,我必须输入4次n和r)从那里,我将检查n>r,然后计算实际的nCr值。我存储nCr值,并以array@kaqqao记录在案:A)我不是主持人……你“只是”需要一个金徽章来接近DUP,因为你得到了徽章B)这个问题的质量很低,实际上应该得到更多的否决票和接近的请求……剩下的作业基本上是由其他人来做的;而且“很好”看到你就这么做了。是的,可能的组合的数字哦,谢谢你,但它只显示了我想以这种格式输出的nCr值:n | c | nCr | | | | | | | | | | | | | | | | | | | | | | | | | |。
public class Ncr {
public static void main (String[] args) {
int nCrValues;
System.out.println("Enter amount of nCr values you wish to calculate: ");
nCrValues = input();
while ((nCrValues < 2) || (nCrValues > 10)){
System.out.println("Must be between 2 and 12 inclusive");
nCrValues = input();
}//END while
for (int[] res : values(nCrValues)) {
System.out.println(String.format("%d | %d | %d", res[0], res[1], res[2]));
}
}//END main
public static int[][] values(int nCrValues){
//I know I need a loop to nCrValues to input 'n' and 'r' as an array, not sure how
int[][] res = new int[nCrValues][3];
for (int i = 0; i < nCrValues; i++) {
int n, r;
System.out.println("Enter n value: ");
n = input();
System.out.println("Enter r value: ");
r = input();
res[i] = new int[]{n, r, calculatenCr(n, r)};
}
return res;
}//END values
public static int calculatenCr(int n, int r){
return fact(n) / (fact(n-r) * fact(r));
}//END calculatenCr
public static int fact(int num){
int count;
int factor = 1;
for (count = 1; count <= num; count++){
factor *= count;
}//END for
return factor;
}//END fact
public static int input(){
int input;
Scanner sc = new Scanner(System.in);
input = sc.nextInt();
while (input < 0){
System.out.println("Must be a positive number.");
input = sc.nextInt();
}//END while
return input;
}//END input
}//END CLASS