简体   繁体   English

用不同的方法访问变量Java

[英]Accessing a variable in different methods Java

I am studying methods and have been given an exercise to do. 我正在研究方法,并已做练习。 I am a bit unsure as to what to do with this particular question. 对于这个特定的问题,我不确定。

The Question we have been given is: 我们得到的问题是:

    Modify the above program so the conversion is done in a method.

This is the code I have so far and my problem is when I run the code I get as far as when I enter the letter and it stops. 这是我到目前为止的代码,我的问题是,当我运行该代码时,我得到的输入代码就会停止。

   //Exercise 3 Brian Sheet 5

//Modify the above program so that the conversion is done in a method //修改上面的程序,以便在一个方法中完成转换

import java.util.Scanner;

public class Exercise3 {

public static void main(String[] args) {
    double temp;
    String c = "c";
    String f = "f";
    String a;
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the temperature: ");
    temp = input.nextDouble();

    input = new Scanner(System.in);

    System.out
            .println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
    a = input.nextLine();
    if (a.equals(c)) {
        celsiusEq(temp);
    } else {
        Fahren(temp);
    }

}

private static double celsiusEq(double celsius) {
    double temp;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double fahrenheit) {
    double temp;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

} I don't know what I am doing wrong and it is probably something very simple. 我不知道我在做什么错,这可能很简单。 If anyone could help me, it would be graciously appreciated as I have been looking at this for the past 30 minutes! 如果有人可以帮助我,在过去的30分钟里,我一直很乐意为您服务!

Here you need to interchange the temp and celsius varables to work properly 在这里,您需要互换温度和摄氏变量才能正常工作

private static double celsiusEq(double celsius) {
        double temp; //here is the problem
        celsius = (temp - 32) * 5 / 9;
        return celsius;

    }

Here you need to interchange the temp and fahrenheit varables to work properly 在这里,您需要互换温度和华氏变量才能正常工作

 private static double Fahren(double fahrenheit) {
        double temp; //here is the problem
        fahrenheit = temp * 9 / 5 + 32;
        return fahrenheit;
    }

Correction here 在这里更正

private static double celsiusEq(double temp){
    double celsius;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double temp){
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

Update request 更新要求

returntype functionname(argumenttype argument2 ,argumenttype argument2,....argumenttype argumentN  ){
// local variable declaration
variableype variable1;
variableype variable2;
----------------------
variableype variableN;
 Your calculation

   return your value based on the return type; 

}

see more details here 在这里查看更多详细信息

http://www.tutorialspoint.com/java/java_variable_types.htm http://www.tutorialspoint.com/java/java_variable_types.htm

http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

Its pretty simple, actually. 实际上,它非常简单。 Here's the code : 这是代码:

Celsius To Fahrenheit 摄氏到华氏度

private static double celsiusToFahrenheit(double celsius)
{
   double fahrenheit;
   fahrenheit = ((celsius * 9) / 5) + 32;
   return fahrenheit;
}

Fahrenheit To Celsius 华氏度到摄氏温度

private static double fahrenheitToCelsius(double fahrenheit)
{
   double celsius;
   celsius = ((fahrenheit - 32) * 5) / 9;
   return celsius;
}

Always use brackets while performing number operations. 执行数字操作时,请务必使用方括号。 Its a good programming habit. 这是一个良好的编程习惯。

Here's what was wrong with your code : 这是您的代码出了什么问题:

private static double celsiusEq(double celsius) 
{
    double temp; //TEMP HAS NO VALUE
    celsius = (temp - 32) * 5 / 9; //STILL YOU ARE USING IT
    return celsius;    
}

private static double Fahren(double fahrenheit) 
{
    double temp; //TEMP HAS NO VALUE
    fahrenheit = temp * 9 / 5 + 32; //STILL YOU ARE USING IT
    return fahrenheit;
}

Instead of using the celsius and fahrenheit variables, you were using the temp ones. 您没有使用摄氏和华氏变量,而是使用了临时变量。

This is your piece of code: 这是您的代码:

private static double celsiusEq(double temp){
    double celsius;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double temp){
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

There are several corrections that need to be done: 需要进行一些更正

1.) Since you are passing temp as the parameter , why don't you simply use that in your function like this: 1.)由于您将temp作为参数传递,为什么不在函数中像这样简单地使用它

private static double Fahren(double temp){
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

2.) Try to make such methods public instead of private . 2.)尝试使此类方法public而不是private Provide easier accessibility and manipulation. 提供更轻松的可访问性和操作。

3.)Since your function returns a double , you will need to capture the result in order to print it/modify it . 3.)由于您的函数返回了double ,因此您需要捕获结果才能打印/修改它 Like this: 像这样:

double answerInCelcius = celsiusEq(temp);  
System.out.println("Answer in Celsius is :" +answerInCelsius);

Hope it helps :) 希望能帮助到你 :)

You need to change your methods to 您需要将方法更改为

private static double celsiusEq(double temp) {
    double celsius;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

private static double Fahren(double temp) {
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

Your variable names got interchanged. 您的变量名互换了。

try with this following code 试试下面的代码

import java.util.Scanner;
    public class Exercise3 {

    public static void main(String[] args) {
      double temp;
      String c = "c";
      String f = "f";
      String a;
      Scanner input = new Scanner(System.in);

       System.out.println("Please enter the temperature: ");
       temp = input.nextDouble();

       input = new Scanner(System.in);

    System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
    a = input.nextLine();
    if(a.equals(c)){
        System.out.println(celsiusEq(temp));
    }else{
        Fahren(temp);
    }


    }


    private static double celsiusEq(double celsius){
    double temp = 0;
    celsius = (temp - 32) * 5 / 9;
    return celsius;

}

    private static double Fahren(double fahrenheit){
    double temp = 0;
    fahrenheit = temp * 9 / 5 + 32;
    return fahrenheit;
}

}

ouput 输出中

Please enter the temperature: 
250
Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)
f
32.0

There are compile time error in your code: You can't use local variable without intinializing it. 您的代码中存在编译时错误:如果不局部化本地变量,就不能使用它。 Here inside celsiusEq,Fahren methods intialize temp variable like: 在celsiusEq中,Fahren方法将temp变量初始化为:

double temp = 0;

Then it should work. 然后它应该工作。

FYI Please read following article. 仅供参考,请阅读以下文章。

http://www.dummies.com/how-to/content/local-variables-in-java.html http://www.dummies.com/how-to/content/local-variables-in-java.html

I would do something like: 我会做类似的事情:

import java.util.Scanner;
public class Exercise3 {

    public static void main(String[] args) {
        double temp;
        String a;

        System.out.println("Please enter the temperature: ");
        Scanner input = new Scanner(System.in);
        temp = input.nextDouble();


        System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
        a = input.nextLine();
        switch(a)
        {
            case "c":
                temp = toCelsius(temp);
                System.out.println("Temp in Celsius: " + temp);
                break;
            case "f":
                temp = toFahren(temp);
                System.out.println("Temp in Fahren: " + temp);
                break;
            default:
                System.out.println("Invalid Entry");
        }

    }


    private static double toCelsius (double fahren){
        return (fahren - 32) * 5 / 9;
    }

    private static double toFahren(double celsius){
        return celsius * 9 / 5 + 32;
    }

}

But hey! 但是,嘿! Many other people beat me to the punch. 许多其他人击败了我。

private static double celsiusEq(double temp) {
    return (temp - 32) * 5 / 9;

}

private static double Fahren(double temp) {
    return temp * 9 / 5 + 32;
}

and don't forget to print a result vlaue like System.out.println(celsiusEq(temp)); 并且不要忘记打印类似System.out.println(celsiusEq(temp));的结果。 :-) :-)

Your are just confusing with temp and Celsius variables in side methods. 您只是在辅助方法中将temp和Celsius变量弄混了。 So i have changed the code and following is the updated version of your code. 所以我更改了代码,以下是您代码的更新版本。

public class Exercise3 {

public static void main(String[] args) {
    double temp;
    String c = "c";
    String f = "f";
    String a;
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the temperature: ");
    temp = input.nextDouble();

    input = new Scanner(System.in);

    System.out
            .println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
    a = input.nextLine();
    if (a.equals(c)) {
        celsiusEq(temp);
    } else {
        Fahren(temp);
    }

}

private static double celsiusEq(double temp ) {
    double celsius;
    celsius = (temp - 32) * 5 / 9;
            System.out.println("AFTER CONVERTION " + celsius);
    return celsius;

}

private static double Fahren(double temp ) {
    double fahrenheit;
    fahrenheit = temp * 9 / 5 + 32;
            System.out.println("AFTER CONVERTION " + fahrenheit);
    return fahrenheit;
}
     }

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

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