Difference between Java and JavaScript (how to find the average)

Simple but also output

Java


import java.util.*;
public class Test12{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int sum = 0; //Variables for storing totals
int a=0;
while (true) {
    System.out.println("Please enter the number repeatedly(End with 0 for total and average)"); //Add termination condition to message
    int num = scan.nextInt();
    if (num == 0) {
        break;
    } else {
        sum += num;
        a++;//Add until the end
        }
    }
//System.out.println("The total is"+sum);//
System.out.println("The average is"+sum/a);
 }
}

This is an example.

JavaScript

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>jsave</title>
</head>
<body>
  <h1>Average calculation</h1>
  <input type="number" id="num" value="" placeholder="Enter a number"/>
  <button onclick="add()">Add</button>
  <button onclick="btCalc()">Calc!</button>
  <div id="msg"></div>
<script>
var nums=[];
var str="";
var eleNum=document.getElementById("num");
var eleMsg=document.getElementById("msg");
function add(){
//Converted to int but not Integer unlike Java

  var num=parseInt(eleNum.value);
  nums.push(num);
  eleMsg.textContent=num+"To the array";
}
function btCalc(){
	var sum=0;
	for(var i=0;i<nums.length;i++){
	str +=nums[i]+(i==nums.length-1?"":",");
    sum +=nums[i];
  }
	document.getElementById("data").textContent=str;
	var msg='[{${nums.join(",")}]The average of${sum/nums.length}is.';
    eleMsg.textContent=msg;
}
</script>
</body>
</html>

For the time being, var should be changed to block scope (let, const).

Speaking where I stumbled


<script>
'use strict';
window.onload=function(){
  //dom get
  let ele=document.getElementById("bt");
  let result=document.getElementById("result");
  //Sets a function that is called each time a particular event is delivered to the target.
  ele.addEventListener("click",function(){
    result.textContent="Clicked!";
  });
  ele.addEventListener("click",function(){
    window.alert("Clicked!!!");
  });
};
</script>

Execute the method after loading the HTML like? And. .. It was difficult to understand when considering the meaning and significance. ..

Recommended Posts

Difference between Java and JavaScript (how to find the average)
How to find the total score and average score
Differences between Java, C # and JavaScript (how to determine the degree of obesity)
How to find the tens and ones
[Java] Understand the difference between List and Set
[Java] Difference between == and equals
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
How to find the distance and angle between two points on a plane
Find the difference between List types
[Java] Difference between Hashmap and HashTable
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
Let's sort out the differences between Java substring and C # Substring, and how to port them.
[Java] Difference between array and ArrayList
[Java] Check the difference between orElse and orElseGet with IntStream
Differences in how to handle strings between Java and Perl
The road from JavaScript to Java
java Eclipse How to debug javaScript
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
How to find Java prime numbers
[Java] What is the difference between form, entity and dto? [Bean]
[Java] How to convert from String to Path type and get the path
Easy to understand the difference between Ruby instance method and class method.
[Java] How to use the File class
Difference between final and Immutable in Java
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Java] How to use the toString () method
[For beginners] Difference between Java and Kotlin
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to output and write files!
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Processing × Java] How to use the class
How to install the legacy version [Java]
How to get the date in java
[Processing × Java] How to use the function
About the difference between irb and pry
[Java] How to use the Calendar class
[Java] Difference between Intstream range and rangeClosed
How to switch between multiple Java versions
Difference between int and Integer in Java
Java vs. JavaScript: What ’s the Difference?
Java and JavaScript
[Java] How to get the current date and time and specify the display format
How to find out the Java version of a compiled class file
How to find the total number of pages when paging in Java
[Java improvement case] How to reach the limit of self-study and beyond
Understand the difference between int and Integer and BigInteger in java and float and double
How to create your own annotation in Java and get the value
How to find the cause of the Ruby error
[iOS] Understand the difference between frame and bounds
[Java] How to use FileReader class and BufferedReader class
[Rails / ActiveRecord] About the difference between create and create!
[Java] How to use Thread.sleep to pause the program
[Java] How to get and output standard input