Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

2.5 Η κλάση math στην Java

Ναι, φυσικά. Η κλάση java.lang.Math παρέχει μια σειρά από μεθόδους που επιτρέπουν την εκτέλεση διαφόρων μαθηματικών λειτουργιών, όπως τριγωνομετρικές, λογαριθμικές, εκθετικές, δυναμικές, στρογγυλοποίηση και άλλες.

Παρακάτω υπάρχουν μερικά παραδείγματα μεθόδων που παρέχονται από την κλάση Math:

  • Η μέθοδος Math.max(x,y) επιστρέφει το μεγαλύτερο από δύο αριθμούς.
  • Η μέθοδος Math.min(x,y) επιστρέφει το μικρότερο από δύο αριθμούς.
  • Η μέθοδος Math.abs(x) επιστρέφει την απόλυτη τιμή ενός αριθμού.
  • Η μέθοδος Math.sqrt(x) επιστρέφει την τετραγωνική ρίζα ενός αριθμού.
  • Η μέθοδος Math.round(x) στρογγυλοποιεί έναν αριθμό μεταβλητής κινητής υποδιαστολής στον πλησιέστερο ακέραιο.
  • Η μέθοδος Math.random() επιστρέφει έναν τυχαίο αριθμό ανάμεσα στο 0.0 και το 1.0.

Αυτές οι μέθοδοι μπορούν να είναι χρήσιμες σε διάφορες μαθηματικές εφαρμογές, όπως επιστημονικοί υπολογισμοί, προγραμματισμός παιχνιδιών και χρηματοοικονομικά

Η μέθοδος Math.max(x, y) χρησιμοποιείται για να βρούμε τη μεγαλύτερη τιμή ανάμεσα στα x και y:

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int x = 5;
int y = 10;
int maxNum = Math.max(x, y);
System.out.println("The highest number is: " + maxNum);
int x = 5; int y = 10; int maxNum = Math.max(x, y); System.out.println("The highest number is: " + maxNum);
int x = 5;
int y = 10;
int maxNum = Math.max(x, y);
System.out.println("The highest number is: " + maxNum);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The highest number is: 10
The highest number is: 10
The highest number is: 10

Η μέθοδος Math.min(x, y) χρησιμοποιείται για να βρούμε τη μικρότερη τιμή ανάμεσα στα x και y:

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int x = 5;
int y = 10;
int minNum = Math.min(x, y);
System.out.println("The lowest number is: " + minNum);
int x = 5; int y = 10; int minNum = Math.min(x, y); System.out.println("The lowest number is: " + minNum);
int x = 5;
int y = 10;
int minNum = Math.min(x, y);
System.out.println("The lowest number is: " + minNum);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The lowest number is: 5
The lowest number is: 5
The lowest number is: 5

Η μέθοδος Math.sqrt(x) επιστρέφει την τετραγωνική ρίζα του x:

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
double x = 64;
double squareRoot = Math.sqrt(x);
System.out.println("The square root of " + x + " is: " + squareRoot);
double x = 64; double squareRoot = Math.sqrt(x); System.out.println("The square root of " + x + " is: " + squareRoot);
double x = 64;
double squareRoot = Math.sqrt(x);
System.out.println("The square root of " + x + " is: " + squareRoot);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The square root of 64.0 is: 8.0
The square root of 64.0 is: 8.0
The square root of 64.0 is: 8.0

[adinserter block=”2″]

Η μέθοδος Math.abs(x) επιστρέφει την απόλυτη (θετική) τιμή του x:

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int x = -5;
int absValue = Math.abs(x);
System.out.println("The absolute value of " + x + " is: " + absValue);
int x = -5; int absValue = Math.abs(x); System.out.println("The absolute value of " + x + " is: " + absValue);
int x = -5;
int absValue = Math.abs(x);
System.out.println("The absolute value of " + x + " is: " + absValue);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The absolute value of -5 is: 5
The absolute value of -5 is: 5
The absolute value of -5 is: 5

Η Math.random() επιστρέφει έναν τυχαίο αριθμό μεταξύ 0.0 (συμπεριλαμβανομένου) και 1.0 (εξαιρουμένου):

Παράδειγμα κώδικα σε Java για τη χρήση της Math.random() για τη δημιουργία ενός τυχαίου ακεραίου μεταξύ δύο δεδομένων ακεραίων (min και max):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int min = 1;
int max = 10;
int randomNum = min + (int)(Math.random() * (max - min + 1));
System.out.println("Ένας τυχαίος αριθμός μεταξύ " + min + " και " + max + " είναι: " + randomNum);
int min = 1; int max = 10; int randomNum = min + (int)(Math.random() * (max - min + 1)); System.out.println("Ένας τυχαίος αριθμός μεταξύ " + min + " και " + max + " είναι: " + randomNum);
int min = 1;
int max = 10;
int randomNum = min + (int)(Math.random() * (max - min + 1));

System.out.println("Ένας τυχαίος αριθμός μεταξύ " + min + " και " + max + " είναι: " + randomNum);

Στο παραπάνω παράδειγμα, οι μεταβλητές min και max έχουν τιμές 1 και 10 αντίστοιχα, και η μεταβλητή randomNum υπολογίζεται ως ένας τυχαίος ακέραιος μεταξύ των δύο αυτών τιμών, συμπεριλαμβανομένων και των δύο. Η Math.random() επιστρέφει έναν τυχαίο δεκαδικό αριθμό ανάμεσα στο 0.0 και το 1.0 (εξαιρουμένου του 1.0), και για να μετατραπεί σε ακέραιο, πολλαπλασιάζουμε τον τυχαίο αριθμό με το διάστημα (max - min + 1) και προσθέτουμε τον min.

Για να έχετε περισσότερο έλεγχο στον τυχαίο αριθμό, για παράδειγμα, αν θέλετε μόνο έναν τυχαίο αριθμό ανάμεσα στο 0 και το 100, μπορείτε να χρησιμοποιήσετε τον ακόλουθο τύπο:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int randomNum = (int)(Math.random() * 101); // Ένας τυχαίος αριθμός ανάμεσα στο 0 και το 100 (συμπεριλαμβανομένου του 0, αλλά όχι του 100)
int randomNum = (int)(Math.random() * 101); // Ένας τυχαίος αριθμός ανάμεσα στο 0 και το 100 (συμπεριλαμβανομένου του 0, αλλά όχι του 100)
int randomNum = (int)(Math.random() * 101);  // Ένας τυχαίος αριθμός ανάμεσα στο 0 και το 100 (συμπεριλαμβανομένου του 0, αλλά όχι του 100)

Στον παραπάνω κώδικα, η Math.random() επιστρέφει έναν τυχαίο δεκαδικό αριθμό ανάμεσα στο 0.0 και το 1.0 (εξαιρουμένου του 1.0), και με τον πολλαπλασιασμό με τον αριθμό 101, παίρνουμε έναν τυχαίο αριθμό ανάμεσα στο 0 και το 100 (συμπεριλαμβανομένου του 0, αλλά όχι του 100), και τέλος, μετατρέπουμε τον τυχαίο αριθμό σε ακέραιο με τη χρήση της (int).

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων