2015-07-11 29 views
0

不幸的是,我不能將我的總體方案(因爲它尚未完成,仍有待編輯),所以我會盡我所能來闡明我的問題。的Java:保存用戶輸入到一個循環計算

基本上,我試圖採取由用戶輸入的整數被保存,然後添加到由用戶輸入的下一個整數(在循環中)。

到目前爲止,我已經試過只是寫公式來看看如何將工作,但是這是一個死衚衕。我需要一些能夠「保存」用戶輸入的整數,當它再次循環並可用於計算時。

這裏是我想要做一個故障發生:

  1. 用戶輸入一個整數(如3)
  2. 整數保存(我不知道該怎麼做,並什麼)(例如3保存)
  3. 環路(大概時間)再繞一圈
  4. 用戶輸入一個整數(例如5)
  5. 以前保存的整數(3)加入到這個新輸入的整數(5 ),總共(3 + 5 =)8.
  6. 而更多的輸入,保存,並添加...

正如你可能會說,我在的Java初學者。但是,我確實懂得如何使用掃描儀,並且創建各種類型的循環(如while)。我聽說我可以嘗試使用「var」來解決我的問題,但我不知道如何應用「var」。我知道numVar,但我認爲這完全是另一回事。更何況,我還想看看是否有更簡單的解決方案來解決我的問題?

+0

使用兩個變量,在循環外定義:'int currentInput = 0; int sum = 0'。然後,在循環中,將用戶輸入讀入'currentInput'並將其添加到'sum'。沖洗並重復。 – Turing85

+0

你想每次循環減少你的總和嗎?或者給 - 數字來取消它? –

+0

@UmaKanth:我想連續添加新輸入的整數到所有以前輸入的整數的總和。你的回答非常有幫助。 :) – Kelsey

回答

1

好吧所以你想要的是存儲一個數字。

所以考慮將它存儲在一個變量,如loopFor

loopFor = 3 

現在我們再次詢問用戶輸入。

我們將它添加到loopFor變量。

所以,我們採用scanner的輸入也許,任何事情都可以使用,掃描儀是一個更好的選擇閱讀數字。

Scanner scanner = new Scanner(System.in);//we create a Scanner object 
int numToAdd = scanner.nextInt();//We use it's method to read the number. 

所以把它包起來。

int loopFor = 0; 
Scanner scanner = new Scanner(System.in);//we create a Scanner object 

do { 
    System.out.println("Enter a Number:"); 
    int numToAdd = scanner.nextInt();//We use it's method to read the number. 
    loopFor += numToAdd; 
} while (loopFor != 0); 
+0

謝謝你的幫助! – Kelsey

0

你可以只是有一個sum變量,並把它在每次迭代:

public static void main(String[] args) { 
    // Create scanner for input 
    Scanner userInput = new Scanner(System.in); 

    int sum = 0; 
    System.out.println("Please enter a number (< 0 to quit): "); 
    int curInput = userInput.nextInt(); 
    while (curInput >= 0) { 
     sum += curInput; 
     System.out.println("Your total so far is " + sum); 
     System.out.println("Please enter a number (< 0 to quit): "); 
    } 
} 
+0

很好的回答,但是,讓我們不要忘記問的人是初學者。你正在使用中間結構,恕我直言。 – Anzurio

+0

謝謝你的回答!我不知道如何處理「(<0退出)」,但我會記住這一點以備將來使用。 – Kelsey

0
// This will keep track of the sum 
    int sum = 0; 
    // This will keep track of when the loop will exit 
    boolean errorHappened = false; 
    do 
    { 
     try 
     { 
      // Created to be able to readLine() from the console. 
      // import java.io.* required. 
      BufferedReader bufferReader = new BufferedReader(new InputStreamReader(System.in)); 
      // The new value is read. If it reads an invalid input 
      // it will throw an Exception 
      int value = Integer.parseInt(bufferReader.readLine()); 
      // This is equivalent to sum = sum + value 
      sum += value; 
     } 
     // I highly discourage the use Exception but, for this case should suffice. 
     // As far as I can tell, only IOE and NFE should be caught here. 
     catch (Exception e) 
     { 
      errorHappened = true; 
     } 
    } while(!errorHappened); 
+0

如果我想添加11或某個不是單個數字的數字,該怎麼辦? –

+0

@UmaKanth System.in.read()會讀取任何你寫的東西,直到你按下回車鍵。 – Anzurio

+0

你可能想再看一遍,它只會考慮第一個字符。 –

0

你想實現一個模型 - 視圖 - 控制器(MVC)模式來處理這個問題。假設你正在做一個純Java應用程序,而不是基於Web的應用程序看看Oracle Java Swing Tutorial學習如何建立你的視圖和控制器。

模型類是很簡單的。我建議只是讓你的控制器上的屬性,例如是在控制器的頂部整數的一個Java的ArrayList

private Array<Integer> numbers = new ArrayList<Integer>(); 

那麼你的控制器可以有一個公共的方法添加一個數字,計算出總

public void addInteger(Integer i) { 
    numbers.addObject(i); 
} 
public Integer computeTotal() { 
    Integer total = 0; 
    for (Integer x : numbers) { 
     total += x; 
    } 
    return total; 
} 
+2

爲什麼要使用MVC,如果沒有GUI?正如OP所說,他/她是一名初學者。我認爲這是矯枉過正。 – Turing85

+0

因爲如果你想開始,你可能會開始使用適當的現代框架,而不是一些命令行界面,這與普通新手計算機用戶的體驗完全相反。 –

+0

@JohnFontaine:謝謝你的回答,但是由於我們還沒有在類中講過數組,所以我會再次使用這個解決方案。 :) – Kelsey

相關問題