0

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
4

2 に答える 2

2

メインのメソッドから返されるすべての値は、それらの値を何も返さないだけです。変数を関数に渡すとき、それらは値によって渡されます。例えば:

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 ifとは関係なくが再作成されます。今のところ、あなたのプログラムはそれらの値を捨てて古いものを保持しているだけです。

userSumまた、mainには、、、およびuserAverageinという変数があり、これらを別のスコープで再定義しますsumaverageコードフローが合計および平均化されると、そのメソッドの新しい変数が作成されます。これらの値を同じにしたい場合は、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タイプのオブジェクトが2つあるとします。クラスComputerは、個々のコンピューターが持つ変数(インスタンス変数)と、すべてのコンピューターが共有する変数(静的変数)を定義します。したがって、インスタンス変数がマウスとキーボードのコンピューターAがある場合、別のコンピューターBのインスタンス変数のマウスとキーボードとは完全に異なりますが、Computer.innernetteという静的変数を共有できます。

于 2013-01-31T01:01:10.363 に答える
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

メソッドが指定された値を返すたびに、期待している現在の合計ではありません。

同じ名前で宣言された2つの変数がある場合でも、それらは異なります。あなたがしたいことは同じ変数を参照することです、これをするためにあなたは変数が宣言されているスコープに参照を持っている必要があります。

累計を取得するには

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

    ...

}

さらなる参照範囲について

于 2013-01-31T01:00:55.687 に答える