英文:
Divide integer elements in an array
问题
如何将数组中的所有输入整数进行分割?
我有这个名为division的方法。它只计算我输入的整数的和。如何将它们进行分割?
public class Arithmetic {
public int division(int[] n) {
int quotient = 0;
for (int num : n) {
quotient = quotient / num;
}
return quotient;
}
public static void main(String[] args) throws IOException {
Arithmetic a = new Arithmetic();
Scanner sc = new Scanner(System.in);
System.out.println("1: Division");
System.out.print("Enter selection here: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter no. of elements you want in array: ");
int numOfElements = sc.nextInt();
System.out.println("Enter elements in the array ~ ");
if (sc.hasNextInt()) {
int arraysInput[] = new int[numOfElements];
for (int i = 0; i < numOfElements; i++) {
arraysInput[i] = sc.nextInt();
}
System.out.println("Quotient: " + a.division(arraysInput));
}
}
}
}
英文:
How do you divide all the input integers in an array?
I have this method called division. It only gets the sum of the integers I've input. How do divide them?
public class Arithmetic {
public int division(int[] n) {
int quotient = 0;
for (int num : n) {
quotient = quotient / num;
}
return sum;
}
public static void main(String[] args) throws IOException {
Arithmetic a = new Arithmetic();
Scanner sc = new Scanner(System.in);
System.out.println("1: Division");
System.out.print("Enter selection here: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter no. of elements you want in array: ");
int numOfElements = sc.nextInt();
System.out.println("Enter elements in the array ~ ");
if (sc.hasNextInt()) {
int arraysInput[] = new int[numOfElements];
for (int i = 0; i < numOfElements; i++) {
arraysInput[i] = sc.nextInt();
}
System.out.println("Quotient: " + a.division(arraysInput));
}
}
}
}
答案1
得分: 0
被接受的答案不可行。遗憾的是,我没有足够的“声望”来发表评论。
参考:
public int division(int[] n) {
int quotient = n[0];
for (int i = 1; i < n.length; i++) {
quotient = quotient / n[i];
}
return quotient;
}
- 如果值数组为空或为null - 抛出异常。
- 如果数组包含零(DivisionByZeroException)
- 如果数组只有一个值,则返回的值不是商(除非假设是value/1?)
-
- 吹毛求疵...但返回值应该是double或float吗?
英文:
The accepted answer will not work. Unfortunately I don't have enough "rep" to post comments.
for reference:
public int division(int[] n) {
int quotient = n[0] ;
for (int i = 1; i < n.length ; i++) {
quotient = quotient / n[i];
}
return quotient;
}
- if the array of values is empty or null - exception thrown.
- if the array contains a zero (DivisionByZeroException)
- if the array has only one value, the returned value is not the quotient (unless the assumption is value/1?)
-
- nitpicking... but shouldn't the return be a double or a float?
答案2
得分: 0
你需要使用以下方式更改你的除法方法:
public int division(int[] n) {
int quotient = n[0];
for (int i = 1; i < n.length; i++) {
quotient = quotient / n[i];
}
return quotient;
}
整个类的外观类似于以下测试代码:
public class Arithmetic {
public int division(int[] n) {
int quotient = n[0];
for (int i = 1; i < n.length; i++) {
quotient = quotient / n[i];
}
return quotient;
}
public static void main(String[] args) throws IOException {
Arithmetic a = new Arithmetic();
Scanner sc = new Scanner(System.in);
// 测试除法
// System.out.println("Divison of {10,5,1} : " + a.division(new int[]{10,5,1}));
System.out.println("1: Division");
System.out.print("在此输入选择:");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("输入你想要的数组元素数量:");
int numOfElements = sc.nextInt();
System.out.println("输入数组元素 ~ ");
if (sc.hasNextInt()) {
int arraysInput[] = new int[numOfElements];
for (int i = 0; i < numOfElements; i++) {
arraysInput[i] = sc.nextInt();
}
System.out.println("商:" + a.division(arraysInput));
}
}
}
}
英文:
You need to change your division method with this
public int division(int[] n) {
int quotient = n[0] ;
for (int i = 1; i < n.length ; i++) {
quotient = quotient / n[i];
}
return quotient;
}
The whole class looks like with some test code :
public class Arithmetic {
public int division(int[] n) {
int quotient = n[0] ;
for (int i = 1; i < n.length ; i++) {
quotient = quotient / n[i];
}
return quotient;
}
public static void main(String[] args) throws IOException {
Arithmetic a = new Arithmetic();
Scanner sc = new Scanner(System.in);
// Test Division
// System.out.println( "Divison of {10,5,1} : " + a.division(new int[]{10,5,1}));
System.out.println("1: Division");
System.out.print("Enter selection here: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter no. of elements you want in array: ");
int numOfElements = sc.nextInt();
System.out.println("Enter elements in the array ~ ");
if (sc.hasNextInt()) {
int arraysInput[] = new int[numOfElements];
for (int i = 0; i < numOfElements; i++) {
arraysInput[i] = sc.nextInt();
}
System.out.println("Quotient: " + a.division(arraysInput));
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论