Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of summation of elements in java Array

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "the method of summation of elements in java array". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "the method of summation of elements in java array" can help you solve the problem.

Demand

1. The keyboard enters 5 integers, which are stored in the array and summed up with the array.

Process

2. Create a keyboard input object and prepare for keyboard input.

Define a summation variable to record the cumulative result.

Dynamically initialize the int array of length 5, ready to store keyboard input values.

Stores the numbers entered by the keyboard in an array.

Iterate through the array, take out each element, and sum.

Output.

Example

Package com.itheima.test; import java.util.Scanner; public class Test3Array {/ * requirements: keyboard input 5 integers, store in the array, and sum the array ideas: 1. Create keyboard input object and prepare keyboard input 2. Define a summation variable and prepare to record the cumulative result 3. Dynamically initialize an int array with a length of 5, ready to store the value entered by the keyboard. Store the values entered by the keyboard into the array 5. Iterate through the array, take out each element, and sum 6. Output sum * / public static void main (String [] args) {/ / 1. Create a keyboard entry object and prepare the keyboard entry Scanner sc = new Scanner (System.in); / / 2. Define a summation variable and prepare to record the cumulative result int sum = 0; / / 3. Dynamically initialize an int array of length 5, ready to store the keyboard input value int [] arr = new int [5]; / / 4. Store the values entered by the keyboard in the array for (int I = 0; I < arr.length; iTunes +) {System.out.println ("Please enter number" + (item1) + "integer:"); / / arr [I] = 10; arr [I] = sc.nextInt ();} / / 5. Traverse the array, take out each element, and sum for (int I = 0; I < arr.length; iTunes +) {sum + = arr [I];} / / 6. Output sum System.out.println ("sum:" + sum);}} this is the end of the content about "the method of summing elements in the java array". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report