码迷,mamicode.com
首页 > 其他好文 > 详细

字符串的运算

时间:2017-10-08 00:15:54      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:class   import   string   images   .com   mina   next   logs   exit   

设计思想

先以字符串的形式接收数据,然后强转成整形进行运算

程序流程图

技术分享

程序源码

import java.util.Scanner;
public class Addition {
public static void main( String args[] )
{
Scanner scanner=new Scanner (System.in);
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
// read in first number from user as a string
System.out.println("请输入第一个数:");
firstNumber=scanner.next();
// read in second number from user as a string
System.out.println("请输入第二个数:");
secondNumber =scanner.next();

// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );

// add the numbers
sum = number1 + number2;
System.out.println("和为:"+sum);
System.exit( 0 ); // terminate the program
}
}

运行结果

技术分享

 

字符串的运算

标签:class   import   string   images   .com   mina   next   logs   exit   

原文地址:http://www.cnblogs.com/wei-jing/p/7636231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!