多个 nCr 程序
Multiple nCr program
好的,所以我必须创建一个 nCr 程序。
我需要允许用户输入他们希望计算的 nCr 值的数量(介于 2 和 12 之间)。
然后相应地输入 n 和 r 值,确保 n 大于 r (n > r)
之后我需要计算 nCr 值并将其存储在数组中并输出值
我希望以二维数组的形式输出值:
n | c | nCr
(without the grids)
目前我的代码如下:
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
我需要一种方法以
形式输出数组
n | c |镍铬合金 |
我该怎么做?到目前为止我的所有编码是否正确?
非常感谢
如果你只是想打印组合的数量,而不是实际的组合本身,不完全理解,但这就是你的代码所指示的,所以这就是我完成它的方式:
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 程序。
我需要允许用户输入他们希望计算的 nCr 值的数量(介于 2 和 12 之间)。 然后相应地输入 n 和 r 值,确保 n 大于 r (n > r) 之后我需要计算 nCr 值并将其存储在数组中并输出值
我希望以二维数组的形式输出值:
n | c | nCr
(without the grids)
目前我的代码如下:
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
我需要一种方法以
形式输出数组n | c |镍铬合金 |
我该怎么做?到目前为止我的所有编码是否正确?
非常感谢
如果你只是想打印组合的数量,而不是实际的组合本身,不完全理解,但这就是你的代码所指示的,所以这就是我完成它的方式:
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