簡體   English   中英

來自JOptionPane的總和輸入

[英]Sum input from JOptionPane

我擁有的代碼非常清楚,到目前為止,它所做的只是提示用戶輸入10個整數,這是我要求和的。 我想這樣做,以便在輸入每個數字之后,將其添加到sum變量中。 我怎樣才能做到這一點?
這是代碼。

int x = 10;
int i = 1;
int maxNumber = 100;
int sum = 0;

while (i <= 10) {
    x = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade at a time."));
    i++;
}

你可以做:

while (i <= 10) {
    x = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade at a time."));
    i++;
    sum+=x; //This line will keep the total
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM