需要用户输入的方法 [英] Method that takes user input

查看:151
本文介绍了需要用户输入的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我叫Fyree,我在做学校作业时遇到问题,我需要创建一个方法来获取用户的值,然后通过 computeRate()方法以打印出显示计算出的汇率的行。由于该程序将用户输入值作为字符串,因此我无法在计算速率公式中使用该值,因为它们不是整数。

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.

我的问题是将Strings转换为int,并使 computeRate()能够正确地接受用户输入的六个值中的两个(intev5 / inbv部分...)。其余的值仅用于在此之后我需要制作的条形图,这是另一个问题。由于某种原因,即使它无法找到用户输入值的字符串版本,也无法找到上面列出的两个变量(但是,由于它们不是整数,因此无法正确计算比率)。所以这是我的代码:

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);
}
}

任何帮助将不胜感激。

推荐答案

在代码块或方法中声明变量时,该变量仅可见在该代码块中。

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.

例如

public void init()
{
...

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

intbv 仅在<$ c中可见$ c> init

同样

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

strbv 仅在此<$中可见c $ c> if 语句。

如果您需要在其他方法中使用变量,则将它们设为字段(类变量)用您的 JLabel 和其他 J * 变量完成。

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.

注意
正如其他人提到的那样,将代码放在return语句之后将导致错误。

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

这篇关于需要用户输入的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆