2013-03-21 57 views
0

我想把兩個變量的輸入作爲整數,並且要用兩個變量進行加法運算。 ans將存儲在另一個變量中。該程序將在每次添加結束後重復,並要求用戶輸入變量,並再次添加。在java中添加問題

我QUS是taht我怎麼能再次添加所有aditions答:

EXM:

Input a= 5 
Input b=5 
ans=10 

阿欣程序會詢問

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

代碼只是做加法了一個又一個,但我想它會做一個任務是....

這一切都添加所有aditions reasuts也。我該怎麼做?

+0

目前你的代碼纔剛剛被調用一次 - 你可以使用一個循環 - 例如一個while循環或一個do while循環迭代直到滿足某個條件 - 就像用戶爲輸入輸入0一樣 – Katana24 2013-03-21 10:04:40

+2

爲什麼在add方法中創建一個新的'math'對象,然後再次調用add?你會永遠循環添加相同的值,然後刷新一遍又一遍... – Quetzalcoatl 2013-03-21 10:05:40

+0

@ Katana24該函數被遞歸調用(這裏 - 'ob_m.add();'),所以它不只是調用一次。 – Dukeling 2013-03-21 10:06:51

回答

0

只要繼續添加中間總和到另一個變量,所以最後你得到最後的總和。

另一種選擇(雖然簡單解決方案可用時不是優選的)是將這些總和放在列表中,並在最後遍歷列表並計算最終的總和。

另外,不要使用ob_m.add(); - 只需調用add();

+0

請讓我清楚 – user2191362 2013-03-21 10:05:04

+0

定義實例變量,我們稱它爲「total」。繼續添加總和 - 總+ = c;而已。 – 2013-03-21 10:06:11

0

將每個答案存儲在附加變量中。然後當你完成後,總結所有答案變量

+0

可以舉一個例子怎麼能爲這個任務創建一個循環? – user2191362 2013-03-21 10:07:31

+0

tnx所有用於治療我 – user2191362 2013-03-21 10:20:03

0

你可以使用一個循環來重複你的動作,並存儲每一個總和?

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); 
     } 
    } 
+0

這不會工作...你在每個添加新的對象,所以你的GrandSum將每次重置 – vlcekmi3 2013-03-21 10:10:36

+0

多次調用同一對象上的add()會給那個特定的對象正確的GrandSum。 – Ankit 2013-03-21 10:12:46

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(); 
     } 
    } 
} 
+0

tnx爲您的幫助 – user2191362 2013-03-21 10:14:12

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); 
} 
+0

tnx爲您的幫助 – user2191362 2013-03-21 10:13:42

+0

歡迎您 – tmwanik 2013-03-21 10:15:37

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); 
    } 
} 
+0

tnx求助:p – user2191362 2013-03-21 10:19:44

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

    } 

}