• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
戈瑾
博客园    首页    新随笔    联系   管理    订阅  订阅
两数相加(输入框)

目标:在两个输入框分别输入两个整数,弹出两数相加的结果

代码:

 1 package com.test;
 2 
 3 //An addition program 
 4 
 5 import javax.swing.JOptionPane;  // import class JOptionPane
 6 
 7 public class Adition {
 8 public static void main( String args[] )
 9 {
10    String firstNumber,   // first string entered by user
11           secondNumber;  // second string entered by user
12    int number1,          // first number to add
13        number2,          // second number to add
14        sum;              // sum of number1 and number2
15 
16    // read in first number from user as a string
17    firstNumber =
18       JOptionPane.showInputDialog( "Enter first integer" );
19 
20    // read in second number from user as a string
21    secondNumber =
22       JOptionPane.showInputDialog( "Enter second integer" );
23 
24    // convert numbers from type String to type int
25    number1 = Integer.parseInt( firstNumber ); 
26    number2 = Integer.parseInt( secondNumber );
27 
28    // add the numbers
29    sum = number1 + number2;
30 
31    // display the results
32    JOptionPane.showMessageDialog(
33       null, "The sum is " + sum, "Results",
34       JOptionPane.PLAIN_MESSAGE );
35 
36    System.exit( 0 );   // terminate the program
37 }
38 }

测试截图:

           

 

posted on 2020-10-07 19:33  戈瑾  阅读(214)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3