首页 > 其他 > 详细

课程作业02 1.

时间:2017-10-06 12:55:53      阅读:389      评论:0      收藏:0      [点我收藏+]

程序设计思想:运用输入语句输入循环N个数据,定义int类型数组存放转换成整形之后的数据值,定义一个int类型变量存储相加之后的值,进行运算后将结果输出即可。

程序设计流程图:

技术分享

 

 

 

程序源代码:

package shiyueyixiti;

import javax.swing.JOptionPane;

import java.util.Scanner;

public class shiyueyi {

 

public static void main(String[] args) {

// TODO 自动生成的方法存根

int i;//用来循环使用

int n;//用来代表输入数字个数

String a;

a=JOptionPane.showInputDialog( "请输入需要进行相加的数字个数:" );

//System.out.println("请输入需要进行相加的数字个数:");

n=Integer.parseInt( a );

String Number[]=new String[n];;//string entered by user

        int number[]=new int[n];         //  number to add

         int  sum=0;              // 存储所有number的和

 

  for(i=0;i<n;i++)

  {  Number[i] =

     JOptionPane.showInputDialog( "Enter "+(i+1)+" integer" );

  }

  // read in  number from user as a string

  for(i=0;i<n;i++)

  {

  number[i] = Integer.parseInt( Number[i] );

  

  }

  // convert numbers from type String to type int

  // add the numbers

  for(i=0;i<n;i++)

  {

  sum=sum+number[i];

  

  }

 

  // display the results

  JOptionPane.showMessageDialog(

     null, "The sum is " + sum, "Results",

     JOptionPane.PLAIN_MESSAGE );

 

  System.exit( 0 );   // terminate the program

}

}

程序运行截图:

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

 

 

 

 

 

 

 

课程作业02 1.

原文:http://www.cnblogs.com/mqlblog/p/7631375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!