Aller au contenu
View in the app

A better way to browse. Learn more.

Next

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[JAVA] Comment créer une fonction en java?

Featured Replies

Posté(e)

Bonjour,

Je souhaiterais savoir comment faire pour mettre dans une fonction, le code ci-dessous en gras qui génére un nombre aléatoire et qui retourne le nombre aléatoire.

Merci pour votre aide

import java.util.*;

public class NombreAdeviner{

public static void main (String[]args){

int num;

<gras>

int n=1000;

Random r=new Random();

int t1=((int)(r.nextInt(n)));

</gras>

Terminal.ecrireIntln(t1);

Terminal.ecrireStringln("Veuillez entrer un nombre en 0 et 1000");

num=Terminal.lireInt();

Terminal.ecrireIntln(num);

if(num==t1){

Terminal.ecrireStringln(" ok");

}

else if(num<t1){

Terminal.ecrireStringln("num<t1");

}

else{

Terminal.ecrireStringln("num>t1");

}

}

}

<config>Linux / Mozilla 1.9.0.19</config>

Posté(e)

Si c'est juste pour faire un petit soft comme ça tu peux faire une méthode statique.

Par contre, il ne faut pas que tu recrées le Random à chaque fois car à mon avis, il te fera toujours le même nombre.

Dans ce cas, utilise plutôt Math.random() qui fait tout pour toi ...

private static int getRandomNumber() {  return (int) (Math.random() * 1000);}

Voilou.

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.