设计思想:定义双精度变量 i 和 s=0 ,定义另一个变量 n 作为计数,方便之后求平均值。程序里将数据输入 i 中,通过 s 来将各个数据求和,最后除 n 得平均值。

程序流程图:

源程序代码:

package jiafa;
import java.util.*;
public class jia {
public static void main(String[] args){
System.out.println("请输入相加的数(最后请输入任意字母来结束输入并进行运算):");
Scanner shuru=new Scanner(System.in);
double i;
double s=0;
int n=0;
while(shuru.hasNextDouble())
{
i=shuru.nextDouble();
n++;
s+=i;
}
System.out.println("这"+n+"个数之和为:"+s);
System.out.print("其平均值为:"+s/n);
}
}

结果截图:

【JAVA】数字相加的更多相关文章

  1. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  2. 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】

    [129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

  3. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 给定一个整数,求解该整数最少能用多少个Fib数字相加得到

    一,问题描述 给定一个整数N,求解该整数最少能用多少个Fib数字相加得到 Fib数列,就是如: 1,1,2,3,5,8,13.... Fib数列,满足条件:Fib(n)=Fib(n-1)+Fib(n- ...

  5. LeetCode OJ:Add Digits(数字相加)

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  6. 给定n个数字,问能否使这些数字相加得到h【折半查找/DFS】

    A Math game Time Limit: 2000/1000MS (Java/Others) Memory Limit: 256000/128000KB (Java/Others) Submit ...

  7. LeetCode.2-两个数字相加(Add Two Numbers)

    这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储, ...

  8. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  9. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  10. js “+” 连接字符串&数字相加 数字相加出现多位小数 函数调用单引号双引号嵌套和转义字符的使用

    一.机制 JavaScript中,加号不仅表示相加还表示字符串连接 当加号两边存在字符串时,加号代表连接,实际上是将两侧都转为了字符串,如 "1" + 1 = "11&q ...

随机推荐

  1. spring mvc配置完后实现下载功能

    实现是前台: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...

  2. 20145213《Java程序设计》第七周学习总结

    20145213<Java程序设计>第七周学习总结 教材学习内容总结 周末快乐的时间总是短暂的,还没好好感受就到了要写博客的周日.有人喟叹时间都去哪儿了,那本周我们就来认识一下Java里的 ...

  3. 【XLL 文档翻译】【第2部分】C API 回调函数 Excel4, Excel12

    Excel4 和 Excel12 函数使得 DLL 可以调用 Excel 工作表函数.宏表函数.命令.XLL特定函数或命令.最近的一些 Excel 版本都支持 Excel12 函数.这两个函数支持下面 ...

  4. iOS开发之Xcode 相对路径与绝对路径

    iOS开发之 相对路径与绝对路径 https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/Xcod ...

  5. Android笔记:ListView

    listview属性 android:divider属性,可以指定ListView 分隔线的颜色,#0000 表示将分隔线设为透明色. listview效率的问题 adapter的三个参数int po ...

  6. 关于each

    1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...

  7. Mysql之取消主从复制

    Mysql5.7 Mysql取消主从复制很简单.只需在其要终止同步的Server上[一般是Slave]执行下面语句即可: stop slave; reset slave; 如图: .

  8. js判断是否为ie6以外的浏览器,若是,则调用相应脚本

    if(navigator.userAgent.indexOf("MSIE 6.0") < 0) { //相应JavaScript脚本}

  9. iis 7.5应用程序池自动停止

    今天在我的windows7旗舰版上配置iis7 (Internet Information Server)失败,一直未能启动服务,访问本地网络提示"Service Unavailable H ...

  10. svn删除所有.svn文件

    svn 删除所有的 .svn文件 find . -name .svn -type d -exec rm -fr {} \;