0

0

Java程序打印数字的求和

WBOY

WBOY

发布时间:2023-08-20 09:49:03

|

962人浏览过

|

来源于tutorialspoint

转载

there are different ways of adding the numbers in java. the numbers to be added can be given as input at runtime or can be put directly inside the program. however, it is difficult to add numbers in this way if the list of numbers is too big. sometimes, the data to be added is given in an excel spreadsheet/ google sheet. so, the easy way is to save the excel spreadsheet /google sheet into a file with extension csv (comma separated values) and then csv file’s column can be selected for adding up the values together using java program. in this article, both the approaches of summations of numbers are specified.

多种方法

对于通过Java程序进行数字求和,使用以下两种方法。

  • Approach 1: Getting the numbers from the user at runtime.

  • 方法2:使用Java通过csv文件添加数字。

Let’s see the programs along with the output one by one.

立即学习Java免费学习笔记(深入)”;

方法一:在运行时从用户获取数字

In this approach, the scanner class will be used to take the user input and write the necessary code to print the computed result.

算法

  • Step 1 − Ask the user how many numbers the user want to add.

  • Step 2 − Ask the user the numbers to be added.

  • 第三步 - 添加输入的数字。

  • 步骤 4 - 显示数字的总和。

    TomatoCart
    TomatoCart

    TomatoCart是一个开源电子商务应用程序,源自osCommerce但拥有更好的用户体验。它提供一个全新的前台和后台界面,后台采用ExtJS开发。该程序支持销售任意类的商品从简单的产品到数字货物和服务。TomatoCart还提供一些市场营销功能如:礼券,按购买数量打折,交叉销售,愿望清单等。TomatoCart支持生成SEO友好的URL,自动生成sitemap,提供网站访问统计,内置webma

    下载

示例(方法1)

import java.util.Scanner;
public class newarr {
   public static void main(String[] args) {
      int nn;
      
      // Scanner is used to take input from the user
      Scanner scnn=new Scanner(System.in);
      System.out.print("Enter the number of items you want to add: ");
      
      //get the input
      nn=scnn.nextInt();
      int[] arrayofNum = new int[10];
      System.out.println("Enter the numbers to be added: ");
      for(int n=0; n

Output

Enter the number of items you want to add: 10
Enter the numbers to be added:
200
320
56
67
6768768
5454
67667
7878
78
99
Sum of given array is 6850587

Approach-2: Adding the numbers through CSV file using Java

In this approach, Excel spreadsheet are used. Then as per the algorithm, the program code would fetch the numbers which are presented in the sheet and the addition of the numbers would be displayed in output.

算法

  • 步骤 1 − 将Excel电子表格/ Google表格保存为CSV格式。

  • 第二步 - 逐行读取CSV文件。

  • Step 3 − Skip the header lines.

  • 第四步 − 选择要添加数据的列号。

  • 第五步 - 将字符串类型的数据转换为数值形式。

  • 步骤 6 − 添加指定列的数字。

  • Step 7 − Print the result.

Java程序打印数字的求和

CSV file Content

Year,2022
Months,Icecream sales (Rs)
Jan,100034
Feb,245767
Mar,391500
Apr,537233
May,682966
Jun,1028699
Jul,974432
Aug,890165
Sep,265898
Oct,141163
Nov,135736
Dec,103097

例子(方法2)

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
public class numbers {
   public static void main(String[] args) throws Exception {
      
      // specify the header lines in the csv file to skip
      int headerlines=2;
      int sum=0;
      
      // Read the csv file
      File file = new File("icecream_sales.csv");
      
      // Read all lines
      List lines = Files.readAllLines(file.toPath(),StandardCharsets.UTF_8);
      
      // variable to hold int values after conversion
      int a;
      //current row number
      int rowcount=1;
      for (String line : lines) {
         
         //skip the number of header lines in csv file
         if (rowcount<=headerlines){
            rowcount=rowcount+1;
            continue;
         }
         String[] array = line.split(",", -1);
      
         //read the numbers from the second column after skipping the header lines
         a= Integer.parseInt(array[1]);
         System.out.println("Icecream Sales (in INR) for the month of "+ array[0] + " is "+ a);
        
         //Adding the numbers from the second column of CSV file
         sum =sum +a;
      }
      
      // printing the sum
      System.out.println("Total sum (in INR) for icecream sale in the given year is :" + sum);
   }
}

Output

Icecream Sales (in INR) for the month of Jan is 100034
Icecream Sales (in INR) for the month of Feb is 245767
Icecream Sales (in INR) for the month of Mar is 391500
Icecream Sales (in INR) for the month of Apr is 537233
Icecream Sales (in INR) for the month of May is 682966
Icecream Sales (in INR) for the month of Jun is 1028699
Icecream Sales (in INR) for the month of Jul is 974432
Icecream Sales (in INR) for the month of Aug is 890165
Icecream Sales (in INR) for the month of Sep is 265898
Icecream Sales (in INR) for the month of Oct is 141163
Icecream Sales (in INR) for the month of Nov is 135736
Icecream Sales (in INR) for the month of Dec is 103097
Total sum (in INR) for icecream sale in the given year is :5496690

Conclusion

In this article, two different programs are given to add the numbers using the java code. In the first approach, the numbers are given by the user at run time and in the second approach, the numbers are taken from a column of a CSV file for summation.

相关文章

全能打印神器
全能打印神器

全能打印神器是一款非常好用的打印软件,可以在电脑、手机、平板电脑等设备上使用。支持无线打印和云打印,操作非常简单,使用起来也非常方便,有需要的小伙伴快来保存下载体验吧!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
java
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

845

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

743

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

740

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

397

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

400

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

447

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

431

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16947

2023.08.03

c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

25

2026.01.23

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.8万人学习

C# 教程
C# 教程

共94课时 | 7.5万人学习

Java 教程
Java 教程

共578课时 | 50.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号