-1

I previously made a calculator that calculates the sum of consecutive numbers and the sum of any numbers you put in. Afterwards, I made it calculate the product of any numbers you put in and the quotient of any two numbers you put in.

However, the last two parts of code of what I said are not being read and not displaying the text. How do I fix this?

I have this so far:

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;
}
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    It would help us help you if your [mre] was properly indented and you provided sample inputs and outputs. – Slaw Jul 11 '20 at 01:20

1 Answers1

0

You'll want to make sure to declare the function before invoking it. (Move the definition closer to the top of the file.)

[More info answered here][1]

[1]: https://stackoverflow.com/questions/52137515/order-of-declaring-methods-in-java#:~:text=2%20Answers&text=In%20java%20functions%2Fprocedures%20are,is%20that%20function%20returns%20value.&text=So%20they%20are%20attached%20to,%2C%20everything%20is%20virtual%3A)%20).