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

How to write the code of Java commodity information statistics

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to write the code for Java commodity information statistics. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

I. topic analysis

Title:

At present, there are five items on one shelf. After inputting the commodity price, you need to automatically output the commodity with the highest price, the sum of the commodity price and the average price of the commodity.

Analysis:

According to the requirements of the title, the data type of the commodity price must be the same type, so an one-dimensional array can be used to store the price of the commodity. Because the price of the commodity is unknown, you need to enter it manually, so you need a utility class Scanner in Java to assign the entered value to the array through the for loop. Through the method of traversing the array, each element in the array is added to get the total price of the commodity; at the same time, assuming that the first commodity is the most expensive, while traversing, determine whether the next one is larger than the given value, and if so, replace it; at the end, divide by the total length of the array, that is, you can get the average price of the commodity.

II. Concrete realization

(1) first declare the data type of the array, as well as the type that declares the maximum, total price, and average price.

/ / declare the type of the array. Int [] prices=new int [5]; / / declare maximum, total price and average price int maxPrice, sumPrice = 0, avgPrice

(2) Import Scanner tool class

Import java.util.Scanner; Scanner sc=new Scanner (System.in)

(3) assign the input value to the array through the for loop

/ / enter the price System.out.println of 5 items ("Please enter the price of 5 items:"); / / cycle to assign the entered value to the array for (int I = 0; I < 5; iCommodity +) {prices [0] = sc.nextInt ();}

(4) through for cycle and if judgment, the total price and the maximum value in the array are calculated, and the average price is obtained by dividing by the length of the array.

/ / assume that the first item is the most expensive maxPrice = prices [0]; / / iterate through the array for (int I = 0; I < prices.length; itemized +) {sumPrice + = prices [I] / / if the price is larger than the maximum price, then replace it with if (maxPrice < prices [I]) {maxPrice = prices [I];}} / / calculate the average avgPrice = sumPrice / prices.length

(5) print the result

System.out.println ("Max Price:" + maxPrice); System.out.println ("Total Price:" + sumPrice); System.out.println ("average result:" + avgPrice); Please enter prices for 5 items:

twenty

fifteen

thirty-two

sixteen

twenty-four

Maximum price: 24

Total price: 24

Average result: 4. Complete code import java.util.Scanner;public class Demo01 {public static void main (String [] args) {/ / declares the type of the array. Int [] prices = new int [5]; / declare maximum, total price and average price int maxPrice, sumPrice = 0, avgPrice; Scanner sc=new Scanner (System.in); / / enter the price System.out.println of 5 goods ("Please enter the price of 5 goods:"); / / cycle assigns the entered value to the array for (int I = 0; I < 5) Prices +) {sumPrice [0] = sc.nextInt ();} / assume that the first item is the most expensive maxPrice = prices [0]; / / iterate through the array for (int I = 0; I < prices.length; iTunes +) {sumPrice + = prices [I] / / aggregate price / / add judgment, if it is larger than the maximum price, then replace it with if (maxPrice < prices [I]) {maxPrice = prices [I];}} / / calculate the average avgPrice = sumPrice / prices.length; System.out.println ("maximum price:" + maxPrice) System.out.println ("Total Price:" + sumPrice); System.out.println ("average result:" + avgPrice);}} above is all the content of this article "how to write the Code for Java Commodity Information Statistics". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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