10 Contoh Coding Bahasa Pemrograman Java
10 Contoh Coding Bahasa Pemrograman Java
1. Menampilkan Hello World
public class MyClass {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Hasilnya :
Hasilnya :
Hasilnya :
Hasilnya :
5. Penggunaan String pada Java
public class MyClass {
public static void main(String[] args) {
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
}
}
Hasilnya :
6. IF dan Else
public class MyClass {
public static void main(String[] args) {
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
}
}
Hasilnya :
7. Switch
public class MyClass {
public static void main(String[] args) {
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}
Hasilnya :
8. Penggunaan While
public class MyClass {
public static void main(String[] args) {
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
}
}
Hasilnya :
9. Penggunaan Array
public class MyClass {
public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
}
}
Hasilnya :
10. User Input
import java.util.Scanner; // import the Scanner class
class MyClass {
public static void main(String[] args) {
System.out.println("Enter username");
userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}
Hasilnya :
Pelajari contoh coding java lainnya pada artikel : 16 Contoh Pemrograman Java
int main() {
cout << "Hello World!";
return 0;
}
Hasilnya :
2. Mendeklarasikan variabel
#include <iostream>
using namespace std;
int main() {
int myNum = 15; // Now myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum;
return 0;
}
Hasilnya :
3. Operator
#include <iostream>
using namespace std;
int main() {
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
cout << sum1 << "\n";
cout << sum2 << "\n";
cout << sum3;
return 0;
}
Hasilnya :
4. User Input
#include <iostream>
using namespace std;
int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}
Hasilnya :
5. Penggunaan String
#include <iostream>
#include <string>
using namespace std;
int main () {
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
return 0;
}
Hasilnya :
6. Array
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
return 0;
}
Hasilnya :
1. Echo/ Print
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
</body>
</html>
Hasilnya :
2. Tipe data
<!DOCTYPE html>
<html>
<body>
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
Hasilnya :
3. Variabel
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
</body>
</html>
Hasilnya :
4. Konstanta
<!DOCTYPE html>
<html>
<body>
<?php
// case-sensitive constant name
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
?>
</body>
</html>
Hasilnya :
5. If dan Else
<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>
</html>
Hasilnya :
6. Array
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
</body>
</html>
Hasilnya :
7. Form input
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
Hasilnya :
8. Form Validasi
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
Hasilnya :
1. Output dengan innerHTML
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
Hasilnya :
2. Variabel
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>
</body>
</html>
Hasilnya :
3. Operator
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Operators</h2>
<p>x = 5, y = 2, calculate z = x + y, and display z:</p>
<p id="demo"></p>
<script>
var x = 5;
var y = 2;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Hasilnya :
4. Aritmatika
<!DOCTYPE html>
<html>
<body>
<p>A typical arithmetic operation takes two numbers and produces a new number.</p>
<p id="demo"></p>
<script>
var x = 100 + 50;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Hasilnya :
5. Event
<!DOCTYPE html>
<html>
<body>
<button onclick="document.getElementById('demo').innerHTML=Date()">The time
is?</button>
<p id="demo"></p>
</body>
</html>
Hasilnya :