0
是否有將多個對話框變爲一個對話框的方法?在Java中,您是否可以將多個對話框壓縮爲一個對話框
import javax.swing.JOptionPane;
public class loop {
public static void main(String args[]) {
int input;
int sum = 0;
int num1 = 0;
int counter = 1;
String num = "";
input=(Integer.parseInt(JOptionPane.showInputDialog ("Enter an integer")));
if (input == (-input)) {
input = input * (-1);
num = String.valueOf(input);
num1 = num.length();
JOptionPane.showMessageDialog(null,"the digits of " + input + " are: ");
for (int i = 0; i < num1; i++) {
String var = num.substring(i, counter);
int var1 = Character.getNumericValue(var.charAt(0));
JOptionPane.showMessageDialog(null, var + " ");
sum = sum + var1;
counter++;
}
JOptionPane.showMessageDialog(null,"the sum is: " + sum);
} else {
num = String.valueOf(input);
num1 = num.length();
JOptionPane.showMessageDialog(null, "the digits of " + input + " are: ");
for (int i = 0; i < num1; i++) {
String var = num.substring(i, counter);
int var1 = Character.getNumericValue(var.charAt(0));
JOptionPane.showMessageDialog(null,var + " ");
sum = sum + var1;
counter++;
}
JOptionPane.showMessageDialog(null, "the sum is: " + sum);
}
}
}
我必須編寫一個程序,用戶輸入一個整數,程序將輸出分解的數字和總和。有了上面的代碼,我可以運行該程序,但每個分解整數的數字都有自己的對話框,是否可以讓它輸出一個對話框而不是多個?
import javax.swing.JOptionPane;
public class loop{
public static void main(String []args){
{
long a,b,test;
long counter = 0;
a=(Long.parseLong(JOptionPane.showInputDialog ("Enter an integer")));
test = a;
while (test > 0)
{
test = test/10;
counter = counter + 1;
}
counter = counter - 1;
while (counter >= 0)
{
b = a% (long) Math.pow(10, counter);
a = a/(long) Math.pow(10,counter);
a = b;
counter = counter - 1;
{
String longString=String.valueOf(a);
JOptionPane.showMessageDialog(null, a + " ");
}
}
}
}
}
我剛入一點與它不同的方式,但現在它凝結一些數字和輸出這是從來沒有進入了一個0。
感謝您的回覆,我想我錯過了一些簡單的事情,或者過於複雜。不過,我現在無法獲得字符串。如果你有時間可以給我一個例子嗎? – 2015-04-04 02:15:48