Я ранее сделал калькулятор, который вычисляет сумму последовательных чисел и сумму любых чисел, которые вы вводите. После этого я заставил его вычислять произведение любых чисел, которые вы вводите, и частное любых двух чисел, которые вы вводите.
Однако две последние части кода того, что я сказал, не читаются и не отображают текст. Как это исправить?
Пока что у меня есть:
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);
Scanner d = new Scanner(System.in);
System.out.println("Please enter how many numbers you want to multiply:");
}
//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 code I am having issues with.
public static int multiplyNumbers(int number)
{
int Area = 0;
int a[] = new int[number];
for(int l = 0; l < number; l++)
{
Scanner s= new Scanner(System.in);
System.out.println("enter number "+(l+1)+":");
a[l] = s.nextInt();
System.out.println("The array of a[" + l + "] = " + a[l]);
}
for(int j = 1;j < number ; j++)
{
Area = a[0];
Area *= a[j];
}
return Area;
}
public static int divideNumbers()
{
int n;
int m;
Scanner s= new Scanner(System.in);
System.out.println("Enter the first number:");
n = s.nextInt();
System.out.println("Enter the second number:");
m = s.nextInt();
int quotient;
quotient = n/m;
System.out.println("The quotient is:" + quotient);
return quotient;
}
}