在同一个类中调用的方法

时间:2022-05-06 23:48:10

long time searcher but first time I'm posting a question. I am an IT student going into [haven't started yet] my second programming class. The first was just an intro to Java (we're talking basics really). I have been having a hard time with calling methods within the same class. I have attempted search-foo with poor results. A few articles pop up but they don't cover exactly what I'm looking for. Included is an example (quickly and probably poorly written) to get across what I'm asking. The basic gist [remember to stay with me since I'm new to programming in general] is that I want to add two numbers, creating a third, and have the system display the result...

长时间的搜索者,但我第一次发布一个问题。我是一名IT学生,正在进入[尚未开始]我的第二个编程课程。第一个只是Java的介绍(我们真的在谈论基础知识)。我在同一个类中调用方法时遇到了困难。我试图搜索结果很糟糕。弹出一些文章,但它们并没有涵盖我正在寻找的内容。包括一个例子(快速,可能写得不好),以了解我所要求的。基本的要点[记得留在我身边因为我是一般的编程新手]是我要添加两个数字,创建第三个,并让系统显示结果......

public class MethodCallExample{

public static void main(String[] args){

  int valueTwo = 3;
  MethodToCall();
  int valueOne;
  int TrueValue = valueOne + valueTwo;
  System.out.println("The total value is " + TrueValue + "!");
}

public static int MethodToCall(){

  int valueOne = 2;

  return valueOne;
}
}

When I go to compile I get one of two errors depending on which derp I try to underp. If I compile as its' written, I receive a "valueOne might not have been initialized" error, and if I either move or remove -int valueOne - I receive "cannot find symbol" referring to valueOne. Any help is greatly appreciated since I am still learning.

当我去编译时,我会得到两个错误中的一个,这取决于我尝试使用哪个derp。如果我编译为'写',我收到“valueOne可能没有被初始化”错误,如果我移动或删除-int valueOne - 我收到“找不到符号”指的是valueOne。由于我还在学习,所以非常感谢任何帮助。

Sincerely, Hubert Farnsworth

真诚的,休伯特法恩斯沃思

3 个解决方案

#1


2  

When you call MethodToCall, you aren't doing anything with the returned value. You need to store the returned value in your variable i.e.

当您调用MethodToCall时,您没有对返回的值执行任何操作。您需要将返回的值存储在变量中,即

int valueOne = MethodToCall();

#2


0  

It looks like you are confused with the variable scopes. Try doing

看起来你对变量范围感到困惑。试着做

int valueOne = MethodToCall();

Inside your main.

在你的主要内部。

#3


0  

When you return something then you need a variable to hold the returned value.. so

当你返回一些东西然后你需要一个变量来保存返回的值..所以

int valueone = methodtovalue();

int valueone = methodtovalue();

would be correct..

会是正确的..

Also note that the variable declared in a function would lose its scope when it is reaches the main program because the variable is declared in function. So valueone in function is different from the valueone declared in main() because valueone in function has its scope only within function and valueone in main has its scope till the end of mainprogram

另请注意,函数中声明的变量在到达主程序时会失去其范围,因为变量是在函数中声明的。函数中的valueone与main()中声明的valueone不同,因为函数中的valueone仅在函数内具有其范围,而main中的valueone的范围直到main程序结束

#1


2  

When you call MethodToCall, you aren't doing anything with the returned value. You need to store the returned value in your variable i.e.

当您调用MethodToCall时,您没有对返回的值执行任何操作。您需要将返回的值存储在变量中,即

int valueOne = MethodToCall();

#2


0  

It looks like you are confused with the variable scopes. Try doing

看起来你对变量范围感到困惑。试着做

int valueOne = MethodToCall();

Inside your main.

在你的主要内部。

#3


0  

When you return something then you need a variable to hold the returned value.. so

当你返回一些东西然后你需要一个变量来保存返回的值..所以

int valueone = methodtovalue();

int valueone = methodtovalue();

would be correct..

会是正确的..

Also note that the variable declared in a function would lose its scope when it is reaches the main program because the variable is declared in function. So valueone in function is different from the valueone declared in main() because valueone in function has its scope only within function and valueone in main has its scope till the end of mainprogram

另请注意,函数中声明的变量在到达主程序时会失去其范围,因为变量是在函数中声明的。函数中的valueone与main()中声明的valueone不同,因为函数中的valueone仅在函数内具有其范围,而main中的valueone的范围直到main程序结束