Method that takes user input

淺唱寂寞╮ 提交于 2019-12-11 07:41:52

问题


Hello everyone my name is Fyree, and I'm having problems with a school assignment where I need to create a method that takes the values from the user, and puts then through the computeRate() method to print out a line that shows the computed Rate. Since the program is taking the user input values as Strings, I am unable to use that in the compute rate formula since they are not ints.

My problem is being able to convert the Strings into ints, and having the computeRate() be able to correctly take two of the six values input by the user (the intev5 / inbv part...). The rest of the values are only to be used for a bar graph that I need to make after this which is a problem for another question. For some reason, it is unable to find those two variables listed above, even though before it was able to find the String versions of the user input value (but of course could not correctly compute the rate because they are not ints). So here is my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rate_Graph extends JApplet implements ActionListener
{ 
JLabel bv, ev1, ev2, ev3, ev4, ev5;
JTextField bv1, ev_1, ev_2, ev_3, ev_4, ev_5;
JButton go, add1, add2, add3, add4, add5;
public void init()
{
    setLayout(new FlowLayout());

    bv = new JLabel("Enter beginning value:");
    bv1 = new JTextField(5);


    ev1 = new JLabel("Enter year 1 value:");
    ev_1 = new JTextField(5);

    ev2 = new JLabel("Enter year 2 value:");
    ev_2 = new JTextField(5);

    ev3 = new JLabel("Enter year 3 value:");
    ev_3 = new JTextField(5);

    ev4 = new JLabel("Enter year 4 value:");
    ev_4 = new JTextField(5);

    ev5 = new JLabel("Enter year 5 value:");
    ev_5 = new JTextField(5);

    int intbv = Integer.parseInt(bv1.getText());
    int intev1 = Integer.parseInt(ev_1.getText());
    int intev2 = Integer.parseInt(ev_2.getText());
    int intev3 = Integer.parseInt(ev_3.getText());
    int intev4 = Integer.parseInt(ev_4.getText());
    int intev5 = Integer.parseInt(ev_5.getText());

    go = new JButton("Add!");
    go.addActionListener(this);
    add(bv); add(bv1);
    add(ev1); add(ev_1);
    add(ev2); add(ev_2);
    add(ev3); add(ev_3);
    add(ev4); add(ev_4);
    add(ev5); add(ev_5);
    add(go);
}
public void actionPerformed(ActionEvent event)
{
        Object src = event.getSource(); 
        if(src==go){
            String strbv = bv1.getText();
            String strev1 = ev_1.getText();
            String strev2 = ev_2.getText();
            String strev3 = ev_3.getText();
            String strev4 = ev_4.getText();
            String strev5 = ev_5.getText();
        }
}

public double computeRate()
{

    double rate = (Math.pow(intev5 / intbv, 1.0 / 5.0) - 1);
    return rate;
    System.out.println(rate);
}
}

Any help would be greatly appreciated.


回答1:


You have System.out.println(rate) after the return statement... this is an unreachable statement, so you must remove it or put it before the return.

intev5 and inbv are not visible because they are declared inside of the init() method, so they are only visible within the scope of that method.

If you want to use those variables within computeRate(), one way would be to declare them outside of init():

JLabel bv, ev1, ev2, ev3, ev4, ev5;
JTextField bv1, ev_1, ev_2, ev_3, ev_4, ev_5;
JButton go, add1, add2, add3, add4, add5;
int intbv, intev1, intev2, intev3, intev4, intev5; //Declare outside of init()
public void init()
{
    ...

    intbv = Integer.parseInt(bv1.getText());
    intev1 = Integer.parseInt(ev_1.getText());
    intev2 = Integer.parseInt(ev_2.getText());
    intev3 = Integer.parseInt(ev_3.getText());
    intev4 = Integer.parseInt(ev_4.getText());
    intev5 = Integer.parseInt(ev_5.getText());

    ...
}



回答2:


When you declare a variable in a block of code or in a method, then that variable is only visible in that block of code.

E.g.

public void init()
{
...

    int intbv = Integer.parseInt(bv1.getText());

intbv is only visible in init

likewise

if(src==go){
   String strbv = bv1.getText();

strbv in only visible in this if statement.

If you require variables to be used in other methods then make them fields (class variables) like you have done with your JLabel and other J* variables.

Note As other have mentioned, putting code after a return statement will result in an error.




回答3:


Try this ..It can help you;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rate_Graph extends JApplet implements ActionListener
{ 
JLabel bv, ev1, ev2, ev3, ev4, ev5;
JTextField bv1, ev_1, ev_2, ev_3, ev_4, ev_5;
JButton go, add1, add2, add3, add4, add5;
 int intbv, intev1,intev2, intev3, intev4, intev5;

@Override
public void init()
{
    setLayout(new FlowLayout());

    bv = new JLabel("Enter beginning value:");
    bv1 = new JTextField(5);
     bv1.setText("5");
    ev1 = new JLabel("Enter year 1 value:");
    ev_1 = new JTextField(5);
 ev_1.setText("5");
    ev2 = new JLabel("Enter year 2 value:");
    ev_2 = new JTextField(5);
 ev_2.setText("5");
    ev3 = new JLabel("Enter year 3 value:");
    ev_3 = new JTextField(5);
 ev_3.setText("5");
    ev4 = new JLabel("Enter year 4 value:");
    ev_4 = new JTextField(5);
 ev_4.setText("5");
    ev5 = new JLabel("Enter year 5 value:");
    ev_5 = new JTextField(5);
 ev_5.setText("5");
   intbv = Integer.parseInt(bv1.getText());
     intev1 = Integer.parseInt(ev_1.getText());
     intev2 = Integer.parseInt(ev_2.getText());
    intev3 = Integer.parseInt(ev_3.getText());
     intev4 = Integer.parseInt(ev_4.getText());
    intev5 = Integer.parseInt(ev_5.getText());

    go = new JButton("Add!");
    go.addActionListener(this);
    add(bv); add(bv1);
    add(ev1); add(ev_1);
    add(ev2); add(ev_2);
    add(ev3); add(ev_3);
    add(ev4); add(ev_4);
    add(ev5); add(ev_5);
    add(go);
}
public void actionPerformed(ActionEvent event)
{
        Object src = event.getSource(); 
        if(src==go){
            String strbv = bv1.getText();
            String strev1 = ev_1.getText();
            String strev2 = ev_2.getText();
            String strev3 = ev_3.getText();
            String strev4 = ev_4.getText();
            String strev5 = ev_5.getText();
        }
}

public double computeRate()
{


    double rate = (Math.pow(intev5 / intbv, 1.0 / 5.0) - 1);
    return rate;

}
}


来源:https://stackoverflow.com/questions/26880927/method-that-takes-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!