-5

I'm trying to print a table showing the value of an account with simple interest added quarterly.  This method also needs to ask the user to enter the original value of the account, the annual interest rate, and the number of years that should be calculated.

I keep getting an errors dealing with the scanner and the doubles and ints I use.

import java.util.*;

public class Tables {

    public static final Scanner CONSOLE = new Scanner(System.in);

    double v;
    double p;
    int y;
    int q;
    double r;

    public static void main(String[] args) {

        System.out.println("Lab 4 written by Leonardo Riojas");

        promptString();
        outputMethod();
    }

    public static void promptString() {

        System.out.println("Enter orginial amount");
        double p = CONSOLE.nextDouble();
        System.out.println("Enter annual interest rate");
        double r = CONSOLE.nextDouble();
        System.out.println("Enter years");
        double y = CONSOLE.nextDouble();
    }

    //dont know where to put this v
    public static void outputMethod() {
        for (int i = 1; i <= 4; i++)
            v = p * (1 + (y - 1 + q / 4.0) * r / 100);

        System.out.println(p + "\t");
    }
}
ntalbs
  • 28,700
  • 8
  • 66
  • 83
L.Riojas
  • 1
  • 1

2 Answers2

1

outputMethod is a static method. Those variable members aren't static, which means they are not held by the class but by instances of the class. So you can't access them without instantiating an object of the class Tables.

Alternatively you can make them static:

class MyClass {
    public static double v;
    //...
    public static void outputMethod() {
        // You can access v now from here
    }
}
germanfr
  • 547
  • 5
  • 19
0

Just quick fix would be declaring all those variables as static.

static double v;
static double p;
static int y; 
static int q; 
static double r; 

But this is not usually good idea. Usually you may want to pass parameters your method. e.g. Create class which accurately defines the relationship of those variables and pass that object to your methods and manipulate that way. But if you just want to make it work do as above.