添加char并转换为int

时间:2016-10-25 16:19:45

标签: java char

String numOfTermsInput = JOptionPane.showInputDialog(null, "Please enter the number "
                                        + "of terms you would like to use to compute Pi: ");

Scanner numOfTermsScanner = new Scanner(numOfTermsInput);

int term = numOfTermsScanner.nextInt();
numOfTermsScanner.close();      

int num1 = term * 2;
int num2 = num1 + 1;
int num3 = num2 + 1;

int digit1 = num1 * 1000;
int digit2 = num2 * 10;
int digit3 = num3;
int sum = digit1 + digit2 + digit3;

if (digit3 > 9)
{

    sum = ((digit1 + digit2) * 10) + digit3;

}

System.out.println(digit1);
System.out.println(digit2);
System.out.println(digit3);
System.out.println(sum);

我想尝试将数字视为字符,这样当我得到8,9,10时,该值将为8910,或者是否有更有效的方法来执行此操作?您可以忽略if语句,这只是一次尝试,仅适用于8910.谢谢。

1 个答案:

答案 0 :(得分:0)

使用Integer.parseInt(String)将字符串转换为整数:

请参阅Integer.valueOf() vs. Integer.parseInt()