xPink
xPink

Reputation: 27

Variable result isn't found

Program is pretty simple, and it's self-assigned so no, it's not homework.

User is supposed to input three numbers, show the squared numbers and also show the sum of the numbers.

import java.util.Scanner;
public class CompSquares {
    public static void main (String args[]) {
        Scanner s= new Scanner( System.in); 
        int num1;
        System.out.println(" Enter a number please.");
        num1 = s.nextInt();
        int num2;
        System.out.println("One more."); 
        num2 = s.nextInt();
        int num3;
        System.out.println("Last one.");
        num3 = s.nextInt();

        System.out.println( "The numbers squared are " + Square( num1, num2, num3));
        System.out.println("The sum of these numbers is " + Sum(result)); 
    }

    private static double Square ( int num1, int num2, int num3) {

        if ( num1 > 0) {
            num1 *= num1;
            System.out.println (num1);} 
            else {
                System.out.println( "Enter a correct number, please.");
            }
            if ( num2 > 0){ 
                 num2 *= num2;
                System.out.println (num2); }
                else {
                    System.out.println("Really? Again. Do the right thing this time, jeez.");
                }
            if ( num3 > 0){
                num3*=num3;
                System.out.println (num3);}
                else {
                    System.out.println("Just make it more then one!!");
                }
            }

    public static int Sum ( int num1, int num2, int num3, int result) {
        result = num1 + num2 + num3;
        System.out.println(result);
    }
}            

Upvotes: 0

Views: 50

Answers (2)

C Prasoon
C Prasoon

Reputation: 61

1) Variable result isn't found.

It is due to your main method not declaring what result is? you have passed result to method sum. here: System.out.println("The sum of these numbers is " + Sum(result));

2) method sum defined with four parameters but passing just one during the call.

3) Either method should return something based on computation done, or declare it void. Both method square and sum are defined to return something but not doing so.

Here is the link to Java tutorials https://docs.oracle.com/javase/tutorial/ also would suggest going through sample programs and examples to enrich your​ learning.

Happy coding.

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359966

In the main method:

 System.out.println("The sum of these numbers is " + Sum(result)); 

result has not been declared in any scope visible to main().

Instead of Sum taking an int result parameter, I think that you meant for Sum to return the result. Without giving you the entire answer, start here:

public static int Sum (int num1, int num2, int num3) {
    int result = num1 + num2 + num3;
    System.out.println(result);
    return result;
}

This will also solve another compiler error (the fact that Sum in the original code does not return anything, but it's declared return type is int).

Upvotes: 1

Related Questions