-2
import java.util.Scanner;

public class ExponentExperiment
{
    public static void main(String [] args)
    {
        int numberOne;
        int B;
        int C;
        Scanner keyBoard = new Scanner(System.in);
        System.out.println("Enter an Integer");
        numberOne = keyBoard.nextInt();
        jordan(numberOne, B);
        carter(numberOne, B, C);
        System.out.println(numberOne + " squared is " + B);
        System.out.println(numberOne + " cubed is " + C);
    }
    public static int jordan(int numberOne, int B)
    {
        B = numberOne * numberOne;
    return B;

    }
    public static int carter(int numberOne, int B, int C)
    {
            C = B * numberOne;
    return C;

    }

}

Errors:

H:\ExponentExperiment.java:13: variable B might not have been initialized jordan(numberOne, B); ^ H:\ExponentExperiment.java:14: variable C might not have been initialized carter(numberOne, B, C); ^ 2 errors

Tool completed with exit code 1

JavaNovice
  • 11
  • 2

3 Answers3

1

Why don't you just use your return values?

import java.util.Scanner;

public class ExponentExperiment
{
    public static void main(String [] args)
    {
        int numberOne;
        int b = 0;
        int c = 0;
        Scanner keyBoard = new Scanner(System.in);
        System.out.println("Enter an Integer");
        numberOne = keyBoard.nextInt();
        b = jordan(numberOne);
        c = carter(numberOne, b);
        System.out.println(numberOne + " squared is " + b);
        System.out.println(numberOne + " cubed is " + c);
    }
    public static int jordan(int numberOne)
    {
        return (numberOne * numberOne);
    }

    public static int carter(int numberOne, int b)
    {
            return (b * numberOne);
    }
}
MrJ
  • 123
  • 6
0

In Java simple types' variables are passed by value, so it is not going to work, ever. See: Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Keepsake
  • 21
  • 1
  • _In Java simple types' variables are passed by value_ Not only _simple types_. Java passes everything by value. The only thing which is different between primitives and objects is that, for primitives, java passes primitives by value and, for objects, it passes object _references_ by value. – BackSlash Feb 10 '14 at 16:30
  • That's true, I just simplified my answer. – Keepsake Feb 11 '14 at 12:57
-1

where you have int B; and int C; in the top of your main method, do

  int B = 0; 
  int C = 0; 

so they are initialized.

user2277872
  • 2,963
  • 1
  • 21
  • 22
  • this appears to work. but would declaring the variables as 0 not screw up the program? – JavaNovice Feb 10 '14 at 16:30
  • no because when you call the methods, you are going to be using the B and C variables, so it is going to be updating the value of them – user2277872 Feb 10 '14 at 16:36