![](/img/trans.png)
[英]In my calculator project, how to do operations for more than just two numbers?
[英]How do I make a calculator that multiplies more than two numbers?
我做了一個計算器,它可以做多種事情(添加連續數字,添加多個數字等),但我無法制作它,以便計算器可以乘以多個數字。 到目前為止,我基本上復制了將多個數字相加的代碼,但我不知道如何讓它相乘而不是相加。
這是我的代碼:
import java.util.Scanner;
public class SumOfNumbers {
public static void main(String arg[])
{
int n;
int sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Please enter how many numbers you want to add up to: ");
n = s.nextInt();
System.out.println("you entered: " + n + "");
sum = addConsecutiveNumbers(n);
System.out.println("sum of 1 to "+n+" = "+sum);
//following code is sum of any numbers you entered from console
//store the numbers into an array
int num;
int sumOfNums=0;
System.out.print("Please enter how many numbers you want to sum: ");
num=s.nextInt();
System.out.println("you want to sum "+num+" numbers ");
sumOfNums = addNumbers(num);
System.out.println("sum of "+num+" numbers = "+sumOfNums);
}
//Define a method which add consecutive numbers based on user's input and return the sum of the numbers
private static int addConsecutiveNumbers (int number)
{
int sum = 0;
for (int i = 1; i <= number; i++)
{
sum = sum + i;
}
return sum;
}
//Define a method which add numbers based on user's input and return the sum of the numbers
private static int addNumbers (int num)
{
Scanner s = new Scanner(System.in);
int a[] = new int[num];
int sumOfNums = 0;
for(int k = 0; k < num; k++)
{
System.out.println("enter number "+(k+1)+":");
a[k] = s.nextInt();
System.out.println("The array of a[" + k + "] = " + a[k]);
}
for(int j = 1;j < num ; j++)
{
sumOfNums += a[j];
}
return sumOfNums;
}
//below is the part of code that I am having trouble with.
public static int multiplyNumbers(int num)
{
int Area = 0;
Scanner s = new Scanner(System.in);
int a[] = new int[num];
System.out.println("Please enter how many numbers you want to multiply:");
num=s.nextInt();
for(int l = 0; l < num; l++)
{
System.out.println("enter number "+(l+1)+":");
a[l] = s.nextInt();
System.out.println("The array of a[" + l + "] = " + a[l]);
}
return Area;
}
}
您不需要將值存儲在數組中進行乘法類似的加法,您可以直接更新最終結果
public static int multiplyNumbers(int num) {
int Area = 1;
Scanner s = new Scanner(System.in);
System.out.println("Please enter how many numbers you want to multiply:");
num = s.nextInt();
for (int l = 0; l < num; l++) {
System.out.println("enter number " + (l + 1) + ":");
int temp = s.nextInt();
Area *= temp;
System.out.println("The array of a[" + l + "] = " + temp);
}
return Area;
}
我馬上看到一件事; 我看到你的代碼的方式,以下兩行是多余的:
System.out.println("Please enter how many numbers you want to multiply:");
num=s.nextInt();
您應該已經詢問用戶他們想要乘以多少個數字,因為它是作為參數傳入的。 至於您的實際問題,請查看addNumbers()
方法中的這些行:
for(int j = 1;j < num ; j++)
{
sumOfNums += a[j];
}
您所要做的就是在您的退貨聲明( return Area;
)之前復制該代碼。 您需要對其進行一些調整,以便它不使用sumOfNums
變量,而是使用Area
變量,而不是相加,而是相乘。 這可以這樣做:
for(int j = 0;j < num ; j++) //j also needs to start at 0, I think you may have made a mistake when writing the summing method
{
Area *= a[j];
}
你會注意到這個算法有一個問題(我自己幾乎沒有發現)。 Area
以 0 值開始,因此 0 乘以任何數字將始終為 0。簡單修復; 只需手動將Area
設置為循環前的第一個值。 像這樣的東西:
Area = a[0];
for(int j = 1; j < num; j++)
{
sumOfNums += a[j];
}
另請注意,這次我在j = 1
處開始了 for 循環。 這是因為我已經開始Area
作為a[0]
,所以我們不想將該數字乘以兩次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.