所以我正在尝试为我的Java课程编写一个简单的程序,但我已经向老师寻求帮助,但这是一个远程学习课程,并没有从这个方向得到很多帮助。所以我在这里。
我们要求解决的问题是创建一个程序 (1)询问用户的号码, (2)找到所有用户定义数字的总和, (3)找到这些数字的平均值, (4)然后将它们交还给用户。
我使用这些数字不仅仅是为了完成我已完成的事情,而是因为它们必须是要调用的独立模块。
如何在while语句中更新userNum变量。目前他们不是。或者有一种更简单的方法来做到这一点,我可以忽略。任何帮助是极大的赞赏。谢谢。
public class P2 {
public static void main(String[] args) {
int userNumCount = 1;
double userNum = input();
double userSum = sum(userNum);
double userAverage = average(userSum, userNumCount);
sum(userNum);
while (userNum != 0 && sum(userNum) <= 100){
++userNumCount;
output(userSum, userAverage);
input();
sum(userNum);
average(userSum, userNumCount);
}//end else
while (userNum != 0 && sum(userNum) >=100){
++userNumCount;
JOptionPane.showMessageDialog (null, "Warning, your sum is currently over 100!!!!!");
output(sum(userNum), userAverage);
input();
sum(userNum);
average(userSum, userNumCount);
}//end else
if (input() == 0){
output(userSum, userAverage);
JOptionPane.showMessageDialog (null, "Thank you for using this program have a nice day.");
}//end else if
}//end main module
public static double input(){
String userNumString;
userNumString = JOptionPane.showInputDialog("Please enter your number or input 0 to end the program.");
double userInput = Double.parseDouble (userNumString);
return userInput;
}//end input module
public static double sum(double userNum){
double userSum =+userNum;
return userSum;
}//end sum module
public static double average (double userSum, int userNumCount){
double userAverage = userSum/userNumCount;
return userAverage;
}//end average module
public static void output (double userSum, double userAverage){
JOptionPane.showMessageDialog (null, "The sum of the numbers input so far is: " + userSum + ". And the Average is " + userAverage + "." );
}//end output module
}//end class
答案 0 :(得分:2)
从main中的方法返回的所有值都只是将它们的值返回到任何值。将变量传递给函数时,它们将按值传递。例如:
public void main(String args[]){
int f = 5;
doSomething(f);
System.out.println(f);
}
public int doSomething(int i){
i+=1;
return i;
}
doSomething
返回的值为6,但程序输出5.当您调用函数时,int i
将独立于f重新创建。截至目前,您的程序只是抛弃这些价值并保留旧价值。
此外,您在主要名为userSum
和userAverage
的变量中有变量,在sum
和average
中您可以在不同的范围内重新定义这些变量。当代码流进入sum和average时,它会为该方法创建新变量。如果您希望这些值相同,则需要将它们设置为静态,方法是在main方法之外定义它们并将它们声明为静态。
我认为您可能遇到的问题是范围。几乎每次你有一个开放式括号,程序都会改变范围。当一个块关闭时(当有一个右括号}时,变量的范围结束,即它们不再存在。例如:
class someClass
{
//Block 0
static int staticNum = 0;
public static main(String args[])
{
//Block 1
int level1 = 0;
if(true)
{
//Block 2
int level2 = 0;
} else {
//Block 3
level1++; //ok because the level1 is accessible from here
staticNum++; //ok because staticNum is static
}
//resume block 1
level2++; //not ok because level2 was in a different scope
doSomething(level1)
}
public static void doSomething(int i){
//Block 5
int level1 = 0; //different than the one in the main method
i++; //ok but once execution leaves i wont exist anymore
staticNum++; //ok because staticNum is static and exists in the class's scope
level1++; //not ok because level1 is not defined for this scope
}
}
当执行在一个块中时,它可以在嵌套级别中访问块'上方'中的任何变量。查看上面的内容,在块2和块3中,您可以访问块1或块0中的任何内容。块3无法访问块2中的变量,因为它们超出了彼此的范围,因为当块关闭时释放在该块中实例化的变量。块5的范围与块1,2和3完全不同。
块0是特殊的,因为它与类相关联。声明static
的方法体之外的任何东西都是类范围的变量,因为您可以在有权访问该类的地方访问它。您可以使用ClassName.staticNum
之类的东西在另一个类中访问它。此外,当您在类中访问它时,您使用静态值的任何方法也需要声明为静态。
类体中未声明为静态的任何内容都是实例变量。它与类的实例相关联,这些实例称为对象。类定义对象的模板。例如,假设我们有两个类型为Computer的对象。类计算机定义每台计算机具有的变量(实例变量),以及每台计算机共享的变量(静态变量)。因此,如果我的计算机A具有实例变量鼠标和键盘,它与另一个计算机B实例变量鼠标和键盘完全不同,但它们可以共享一个名为Computer.innernette的静态变量。
答案 1 :(得分:1)
这是不正确的。
public static double sum(double userNum){
double userSum =+userNum;
return userSum;
}
在基本伪语言中,每次调用此方法时都会发生这种情况
receive userNum
create userSum
set userSum = 0
add userNum to userSum
give userSum
每次该方法返回给定的值,而不是我认为你期望的运行总数。
如果您使用相同名称声明了两个变量,它们仍然不同。你想要做的是引用相同的变量,为此你需要在声明变量的范围内有引用。
获得总计
public class P2 {
public static double userSum = 0;
//because this is a public member it's scope is global i.e. you can refer to it anywhere.
...
public static void main(String[] args) {
...
/* do not declare userSum here.
if you do any reference to userSum will use the local variable
not the global variable.
double userSum = 0; <-- declare local variable it's scope is until the
end of this method nothing outside the method
can see it but you can pass it as a parameter
userSum = 1; <-- local variable is set to 1
P2.userSum = 2; <-- local variable is set to 1 global is set to 2
*/
input();// for this the input method is called and returns a value,
// but you haven't said to put it anywhere so java will throw it away
// all that effort for nothing.
userNum = input(); // in this case it will put the new value in userNum.
}
public static double sum(double userNum){
userSum =+userNum; // do not declare userSum use it from the class context
return userSum;
}
...
}
供进一步参考scope