Jump to content

Générer un nombre aléatoire


flyer74
 Share

Recommended Posts

Bonjour bonjour !

J'essaye de trouver le moyen de généré un nombre aléatoire entre 0 et 8, voilà comment je fait,

package com.monjeux.android;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class SoloFacileJinteligence extends Activity
{

TextView Ia1;
TextView Ia2;
TextView Ia3;
TextView Ia4;

private int VarCode1;
private int VarCode2;
private int VarCode3;
private int VarCode4;
public void onCreate(Bundle savedInstanceState)
{
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.solo_facil_ia);

	 Ia1 = (TextView) findViewById(R.id.Ia1);
	 Ia2 = (TextView) findViewById(R.id.Ia2);
	 Ia3 = (TextView) findViewById(R.id.Ia3);
	 Ia4 = (TextView) findViewById(R.id.Ia4);

	 iaSelectionCode();
	 testeur();

}
private void iaSelectionCode()
{
			// Creation de la combinaison secrete par un random
	Random randIa = new Random();
	VarCode1 = randIa.nextInt(8);
	VarCode2 = randIa.nextInt(8);
	VarCode3 = randIa.nextInt(8);
	VarCode4 = randIa.nextInt(8);

	//niveau facile, les quatre chiffre son different
	while (VarCode1 == VarCode2 || VarCode1 == VarCode3 || VarCode1 == VarCode4)
	{
			//si le premier chiffre et identique a l'un des autre on re random
		VarCode1 = randIa.nextInt(8);
	}
	while (VarCode2 == VarCode1 || VarCode2 == VarCode3 || VarCode2 == VarCode4)
	{
		VarCode2 = randIa.nextInt(8);
	}
	while (VarCode3 == VarCode1 || VarCode3 == VarCode2 || VarCode3 == VarCode4)
	{
		VarCode3 = randIa.nextInt(8);
	}
	while (VarCode4 == VarCode1 || VarCode4 == VarCode2 || VarCode4 == VarCode3)
	{
		VarCode4 = randIa.nextInt(8);
	}
	//ensuite on previen le joueur
	Context ia = getApplicationContext();
	CharSequence text = "L'IA est préte et à choisis la combinaison.";
	int duration = Toast.LENGTH_SHORT;
	Toast toastIaOk = Toast.makeText(ia, text, duration);
	toastIaOk.show();
}
private void testeur()
{
			 // juste pour teste si le random fonctionne
	Random randIa = new Random();
	Ia1.setText(randIa.nextInt(9));
Ia2.setText(randIa.nextInt(9));
Ia3.setText(randIa.nextInt(9));
Ia4.setText(randIa.nextInt(9));
}
}

Mais cela ne fonctionne pas, auriez vous une explication ?

merci

Flyer74

Link to comment
Share on other sites

Oui !

Le programme se lance et quand je clique sur le bouton qui est sensé m'afficher cette activity, un erreur apparais,

Voila le LogCat:

01-08 09:15:39.482: E/AndroidRuntime(377): FATAL EXCEPTION: main
01-08 09:15:39.482: E/AndroidRuntime(377): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mastermind.android/com.mastermind.android.SoloFacileJinteligence}: android.content.res.Resources$NotFoundException: String resource ID #0x5
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.os.Looper.loop(Looper.java:126)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.app.ActivityThread.main(ActivityThread.java:3997)
01-08 09:15:39.482: E/AndroidRuntime(377):  at java.lang.reflect.Method.invokeNative(Native Method)
01-08 09:15:39.482: E/AndroidRuntime(377):  at java.lang.reflect.Method.invoke(Method.java:491)
01-08 09:15:39.482: E/AndroidRuntime(377):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-08 09:15:39.482: E/AndroidRuntime(377):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-08 09:15:39.482: E/AndroidRuntime(377):  at dalvik.system.NativeStart.main(Native Method)
01-08 09:15:39.482: E/AndroidRuntime(377): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.content.res.Resources.getText(Resources.java:242)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.widget.TextView.setText(TextView.java:2977)
01-08 09:15:39.482: E/AndroidRuntime(377):  at com.mastermind.android.SoloFacileJinteligence.testeur(SoloFacileJinteligence.java:72)
01-08 09:15:39.482: E/AndroidRuntime(377):  at com.mastermind.android.SoloFacileJinteligence.onCreate(SoloFacileJinteligence.java:34)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
01-08 09:15:39.482: E/AndroidRuntime(377):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
01-08 09:15:39.482: E/AndroidRuntime(377):  ... 11 more

QUand il dit "$NotFoundException: String resource ID #0x5" c'est bien en rapport avec les nombres aléatoires ? Enfin la je comprend pas trop pourquoi ca marche pas ...

Merci,

Flyer74

Link to comment
Share on other sites

Ce qui se passe, c'est que quand tu fais un setText sur un TextView pour afficher le résultat du random, tu lui passes un entier. Mais setText attend soit une chaine de caractère, soit un identifiant de ressource (qui est un entier). C'est cette deuxième signature de méthode qui est appelée, et ça plante parce qu'il n'y a pas de ressource qui corresponde. Il faut que tu convertisses l'entier que tu veux afficher en une chaine avant de la passer à setText

En faisant comme cela

tv.setText( Integer.toString( randIA.nextInt(8) );

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...