- ហ្គេមទាយលេខ - Guessing Number
- ហ្គេមប៉ាវស៊ីងស៊ុង - Rock, Paper, Scissors
- ម៉ាស៊ីនគិតលេខសាមញ្ញា - Simple Calculator
- FizzBuzz
- Even or Odd
- Simple Menu
- ម៉ាស៊ីនគិតលេខ Factorial - Factorial Calculator
- Reverse a String
- រាប់ស្រៈ និងព្យញ្ជនៈ
- Simple Password Validator
1 ដល់ 100។ អ្នកប្រើប្រាស់ត្រូវទាយលេខ ហើយកម្មវិធីនេះផ្តល់ការណែនាំប្រសិនបើការទាយខ្ពស់ពេក ឬទាបពេក។ ប្រសិនជាទាញខុសចំនួន៥ដងនោះនឹងចាញ់ដោយស្វ័យប្រវត្ត។
ក្នុងលំហាត់នេះអ្នកទាំងអស់គ្នានឹងចេះអំពីការប្រើប្រាសកាន់តែច្បាស់នូវចំណុច ដូខខាងក្រោមនេះ៖
- java.util.Scanner
- អោយអ្នកប្រើប្រាស់ទាយលេខអោយបានត្រឹមត្រូវ
- java.util.Random
- អោយ System បញ្ចេញលេខចៃដន្យ ពី ០ ទៅ ដល់ ១០០
- while loop statement
- យល់ដឹងអំពីរបៀបប្រើប្រាស់ Loop(វិលជុំ) កាន់តែច្បាស់
- else-if ladder
- យល់ដឹងអំពីការប្រើប្រាស់ else-if ladder កាន់តែច្បាស់
- break statement
- យល់ថាតើយើងគួរតែប្រើប្រាស់ break នៅពេលណា
- Scanner is used to read user input.
- Random is used to generate the computer's move.
- The game runs in a loop, allowing the user to play multiple rounds until they choose to exit.
- The user is prompted to enter their move or type "Exit" to quit the game.
- The isValidMove method checks if the user's move is valid (Rock, Paper, or Scissors). Computer's Move
- The computer randomly selects a move from the rps array.
- The game checks the user's move against the computer's move to determine the winner or if it's a tie. End the Game
- If the user types "Exit," the game loop ends, and a thank you message is displayed.
This repository contains a simple Rock-Paper-Scissors game implemented in Java.
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
String[] rps = {"Rock", "Paper", "Scissors"};
boolean playAgain = true;
while (playAgain) {
System.out.println("Enter move (Rock, Paper, Scissors). To exit the game, type Exit:");
String userMove = scanner.nextLine();
if (userMove.equalsIgnoreCase("Exit")) {
playAgain = false;
} else if (isValidMove(userMove)) {
int computerIndex = random.nextInt(3);
String computerMove = rps[computerIndex];
System.out.println("Computer move: " + computerMove);
if (userMove.equalsIgnoreCase(computerMove)) {
System.out.println("It's a tie!");
} else if (userMove.equalsIgnoreCase("Rock") && computerMove.equals("Scissors")
|| userMove.equalsIgnoreCase("Paper") && computerMove.equals("Rock")
|| userMove.equalsIgnoreCase("Scissors") && computerMove.equals("Paper")) {
System.out.println("You win!");
} else {
System.out.println("You lose!");
}
} else {
System.out.println("Invalid move. Please try again.");
}
}
System.out.println("Thanks for playing!");
scanner.close();
}
private static boolean isValidMove(String move) {
return move.equalsIgnoreCase("Rock") || move.equalsIgnoreCase("Paper") || move.equalsIgnoreCase("Scissors");
}
}