0

2 つの変数の入力を整数として取得し、2 つの変数を加算したいと考えています。ans は別の変数に格納されます。プログラムは加算が終了するたびに繰り返し、ユーザーに変数の入力を求め、再度加算を行います。

私の疑問は、どうすればすべての追加と追加を再度追加できるかということです:

例:

Input a= 5
Input b=5
ans=10

Agin プログラムが要求する

Input a= 6
Input b= 6
ans=12

プログラムですべての「 ans 」値を取得し、すべての「 ans 」の追加を行うにはどうすればよいですか

Final Ans=10+12=22

コード:

import java.io.*;

import java.util.Scanner;

public class math{

        public void add()
        {
            Scanner keyboard = new Scanner(System.in);

            int a;
            int b;

            System.out.print("\nEnter a : ");
            a = keyboard.nextInt();

            System.out.print("Enter b : ");
            b = keyboard.nextInt();

            int c=a+b;

            System.out.println("\nans is  :"+c);

            math ob_m=new math();
            ob_m.add();
        }

        public static void main(String args[])
        {
            math ob_main=new math();
            ob_main.add();
        }
    }

コードは次々に追加を行うだけですが、もう1つのタスクを実行してほしい....

それはすべて、すべての追加事項も追加します。どうすればできますか?

4

8 に答える 8

1
    import java.io.*;

import java.util.Scanner;

public class Test {

    int a;
    int b;
    int sum = 0;

    public void add() {
        Scanner keyboard = new Scanner(System.in);



        System.out.print("\nEnter a : ");
        a = keyboard.nextInt();

        System.out.print("Enter b : ");
        b = keyboard.nextInt();

        sum = sum + (a + b);

        System.out.println("\nans is  :" + (a + b));

    }

    public static void main(String args[]) {
        Test ob_main = new Test();
        while (true) {
            ob_main.add();
        }
    }
}
于 2013-03-21T10:10:29.193 に答える
0

ループを使用してアクションを繰り返し、各加算を合計で保存できますか?

于 2013-03-21T10:05:31.710 に答える
0

フィールドGrandSUMがあり、

GrandSum = 0;

そして、すべての追加の後に、それにansを追加します。

GrandSum += ans;

最後に、GrandSumはあなたが望む結果をもたらすでしょう。

編集:

import java.io.*;

import java.util.Scanner;

public class math{
    int GrandSum = 0;//added
        public void add()
        {
            Scanner keyboard = new Scanner(System.in);

            int a;
            int b;

            System.out.print("\nEnter a : ");
            a = keyboard.nextInt();

            System.out.print("Enter b : ");
            b = keyboard.nextInt();

            int c=a+b;
            GrandSum += c;//added

            System.out.println("\nans is  :"+c);                
        }

        public static void main(String args[])
        {
            math ob_main=new math();
            ob_main.add();
            //.....repeat as many times you want
            ob_main.add();
            System.out.println("grandsum: " + ob_main.GrandSum);
        }
    }
于 2013-03-21T10:05:50.073 に答える
0

変化する

public void add() {
    Scanner keyboard = new Scanner(System.in);

    int a;
    int b;
    int total = 0;

    for(int i = 0; i < 2; i++) {
        System.out.print("\nEnter a : ");
        a = keyboard.nextInt();

        System.out.print("Enter b : ");
        b = keyboard.nextInt();

       int c = a+b;
       total += c;
    }
    System.out.println("\nans is  :"+total);
}
于 2013-03-21T10:12:23.460 に答える
0

すべての回答を追加の変数に保存します。次に、完了したら、すべての回答変数を合計します

于 2013-03-21T10:05:01.697 に答える
0

追加し続けるtotal変数がありますc

また、不要な再帰を while ループに変更しました。

public class math
{
    public static void main(String args[])
    {
        int total = 0;
        Scanner keyboard = new Scanner(System.in);

        while (true)
        {
            System.out.print("\nEnter a (-999 to quit): ");
            int a = keyboard.nextInt();

            // terminating condition, modify appropriately
            if (a == -999)
                break; // break out of the while-loop

            System.out.print("Enter b: ");
            int b = keyboard.nextInt();

            int c = a + b;
            total += c;

            System.out.println("\nans is: " + c);
        }
        System.out.println("total is: " + total);
    }
}
于 2013-03-21T10:13:36.533 に答える
0
package farzi;

import java.util.ArrayList;
import java.util.Scanner;

public class dummy {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        ArrayList<Integer> list2 = new ArrayList<Integer>();
        ArrayList<Integer> sum = new ArrayList<Integer>();
        String choice = "";
        do{
            System.out.println("enter the first number");
            int a = keyboard.nextInt();
            System.out.println("enter the second number");
            int b = keyboard.nextInt();
            int tempSum = a+b;
            list1.add(a);
            list2.add(b);
            sum.add(tempSum);
            System.out.println("Do you want to continue : type yes or no");
            choice = keyboard.next();
        }while(choice.toLowerCase().charAt(0)=='y');
        System.out.println("here are the inputs with theri sum");
        System.out.println("num1\t num2\t sum");
        for(int i=0;i<list1.size();i++)
        {
            System.out.println(list1.get(i)+"\t"+list2.get(i)+"\t"+sum.get(i));
        }

    }

}
于 2013-03-21T10:39:16.967 に答える