简体   繁体   English

定义用于多种方法的变量

[英]Defining Variables for use in multiple methods

I've been at my wits end with this problem. 我一直在尽力解决这个问题。 In a class right now and for this assignment I'm needed to use the Scanner to input data and use this same data within my nested methods to calculate two things; 在当前的班级中,对于此作业,我需要使用扫描器输入数据,并在嵌套方法中使用相同的数据来计算两件事。 Temperature Conversion and Currency Conversion. 温度换算和货币换算。

Problem I'm having is I have no idea how to define my variables throughout my whole piece of code, as I get compiler errors saying that certain variables aren't defined; 我遇到的问题是我不知道如何在整个代码中定义变量,因为我得到编译器错误,指出某些变量未定义。 eg tC , and euro . 例如tCeuro

How do I assign those two variables to be able to compile this correctly? 如何分配这两个变量才能正确编译?

import java.util.Scanner;

public class MethodsMS {

    // required main method of the class
    public static void main(String [] args) {

        performTemperatureConversion();
        System.out.println();  // blank line
        performCurrencyConversion();

        double numDouble;


    } // end main

    // (no changes needed above this point)
    //--------------------------------------------------------------
    // (write the following methods)

    //--------------------------------------------------------------

    // write method to perform temperature conversion...
    public static void performTemperatureConversion() 
    {
        double numDouble;
        readDouble();
        degreeConversion();
        display();

    }

    //--------------------------------------------------------------

    // write method to perform currency conversion...
    public static void performCurrencyConversion() 
    {
        readDouble();
        currencyConversion();
        display();

    }

    //--------------------------------------------------------------

    // write method to obtain an input double value (use Scanner)...
    public static double readDouble()
    {
        double numDouble;
        Scanner input = new Scanner(System.in); 
        System.out.print("Enter a double number: ");
        numDouble = input.nextDouble();

        return numDouble;

    }

    //--------------------------------------------------------------

    // write method to print an input string inside a formatted box...
    public static void display()
    {
        System.out.print("Conversion number is now: " + numDouble);
    }

    //--------------------------------------------------------------

    // write method to convert F to C...
    public static void degreeConversion()
    {
        double numDouble, tC;
        tC = (numDouble - 32) * 5/9;

        return;

    }
    //--------------------------------------------------------------

    // write method to convert $ to Euros...
    public static void currencyConversion()
    {
        double euro;
        euro = numDouble * 0.894614;

        return euro;
    }
} // end class

You do not need any class-level variables for this particular example, as you can utilize method return types and parameters. 对于此特定示例,您不需要任何类级变量,因为您可以利用方法返回类型和参数。

The currencyConversion method should be defined like this: currencyConversion方法的定义应如下所示:

public static double currencyConversion(double num)
{
    return num * 0.894614;
}

And used like this in performCurrencyConversion : 并在performCurrencyConversion这样使用:

display(currencyConversion(readDouble()));

The same applies for display : 同样适用于display

public static void display(double num)
{
    System.out.print("Conversion number is now: " + num);
}

And for the sake of completeness, your degreeConversion : 为了完整起见,您的degreeConversion

public static double degreeConversion(double num)
{
    return (num - 32) * 5 / 9;
}

And in performTemperatureConversion : 并在performTemperatureConversion

display(degreeConversion(readDouble()));

If you do want to define a class-level variable, you can add a static variable to the body of your class: 如果确实要定义类级别的变量,则可以在类的主体中添加static变量:

static double euro;

And use it in your methods. 并在您的方法中使用它。

Instead of declaring (eg: double euro; ) inside the method, you can declare it as a class member: 无需在方法内部声明(例如: double euro; ),您可以将其声明为类成员:

static double euro;

Outside of the method, at the class level. 在方法之外,在类级别。

This should work for both euro and tC , but I need to advise that this is not the best way to use java, as it is an Object-Oriented language . 这对于eurotC都应该起作用,但是我需要建议这不是使用Java的最佳方法,因为它是一种面向对象的语言 But I'm sure you'll get there :). 但是我相信你会到达的。

In the readDouble() method you are returning a double value but you are not getting back that value anywhere in your performTemperatureConversion() and performCurrencyConversion() methods. 在readDouble()方法中,您将返回一个双精度值,但您并没有在performTemperatureConversion()和performCurrencyConversion()方法中的任何位置取回该值。 If you want to use the same value throughout the class declare a static variable and get the scanner value in it instead of using the local variable in methods. 如果要在整个类中使用相同的值,请声明一个静态变量并在其中获取扫描器值,而不要在方法中使用局部变量。 Pass the same value in methods degreeConversion() and currencyConversion(). 在degreeConversion()和currencyConversion()方法中传递相同的值。

You need your numDouble variable to be a Class Member Variable. 您需要将numDouble变量设为类成员变量。

This means that any method in your class can "see" it (ie can use the value or update the value. 这意味着您的类中的任何方法都可以“看到”它(即可以使用值或更新值)。

To do this just define the variable outside of the methods, but still within the class itself. 为此,只需在方法外部定义变量,但仍在类本身内部即可。

By convention, and in most cases, you'll want this to be a private member variable, so you'll want to do something like this: 按照惯例,在大多数情况下,您希望将其作为私有成员变量,因此您需要执行以下操作:

// Defined here as a private member variable
private double numdouble;

public static double readDouble()
{
  // numDouble definition removed here, no need.
  Scanner input = new Scanner(System.in); 
  System.out.print("Enter a double number: ");
  numDouble = input.nextDouble();
  return numDouble;
}

Then you just need to make sure that this readDouble() is called before numDouble is used. 然后,您只需要确保在使用numDouble之前已调用此readDouble()。

As I understand you want to be numDouble and tC two variables visible in all of those methods. 据我了解,您希望在所有这些方法中都可以看到numDouble和tC两个变量。 Obviously you use the keyword "double" more like a variable import similar to the keyword "global" in PHP. 显然,您使用关键字“ double”更像是变量导入,类似于PHP中的关键字“ global”。

But your code does in fact declare the variable method local and throw it away at the end of the method. 但是实际上,您的代码确实将变量方法声明为局部方法,并将其丢弃在方法末尾。 This way you can't share the variables over several method calls. 这样,您将无法在多个方法调用之间共享变量。 The simplest way to fix this is to define two static methods at class level: 解决此问题的最简单方法是在类级别定义两个静态方法:

package stackoverflow;

import java.util.Scanner;

public class MethodsMS {

private static double numDouble, tC, euro;

// required main method of the class
public static void main(String[] args) {

    performTemperatureConversion();
    System.out.println(); // blank line
    performCurrencyConversion();
} // end main

// (no changes needed above this point)
// --------------------------------------------------------------  
// (write the following methods)

// --------------------------------------------------------------

// write method to perform temperature conversion...
public static void performTemperatureConversion() {
    readDouble();
    degreeConversion();
    display();
}

// --------------------------------------------------------------

// write method to perform currency conversion...
public static void performCurrencyConversion() {
    readDouble();
    currencyConversion();
    display();
}

// --------------------------------------------------------------

// write method to obtain an input double value (use Scanner)...
public static double readDouble() {
    try (Scanner input = new Scanner(System.in)) {
        System.out.print("Enter a double number: ");
        numDouble = input.nextDouble();
    }

    return numDouble;

}

// --------------------------------------------------------------

// write method to print an input string inside a formatted box...
public static void display() {
    System.out.print("Conversion number is now: " + numDouble);
    System.out.print("tC is now: " + tC);
    System.out.print("Euro is now: " + euro);
}

// --------------------------------------------------------------

// write method to convert F to C...
public static void degreeConversion() {
    tC = (numDouble - 32) * 5 / 9;
}

// --------------------------------------------------------------

// write method to convert $ to Euros...
public static void currencyConversion() {
    euro = numDouble * 0.894614;
}

} // end class

BTW: You also forgot to close the input variable. 顺便说一句:您还忘记了关闭输入变量。 I fixed this. 我修好了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM