bonjour a tous,
comme ca fait que 2 jours que j'aprend le java, merci d'etre indulgent^^
voila, jai un petit probleme au niveau du while,
en fait mon code est censé faire les calcul d'une suite arithmetique,
ca c'est bon ça marche nikel,
par contre, des que je veut mettre une boucle while, juste pour pouvoir recommencer, ca me met :
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at winnitest.main(winnitest.java:41)
apparemment c'est la:
reponse = sc.nextLine().charAt(0);
qu'il y a un probleme
voila mon code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import java.util.Scanner;
public class winnitest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("---------------------------------");
System.out.println(" suite arithmetique by moi ");
System.out.println("---------------------------------");
char reponse = 'O';
Scanner sc = new Scanner(System.in);
while (reponse == 'O')
{
System.out.println( "saisissez U1" );
double u1 = sc.nextDouble();
System.out.println( "saisissez la raison");
double raison = sc.nextDouble();
if (raison == 0)
System.out.println("la raison n'est pas valide");
else
System.out.println("saisissez Un");
int nterme= sc.nextInt();
double result = (double)(u1)+ (double)(--nterme) * raison ;
System.out.println("---------------------------------");
System.out.println(" suite arithmetique resultat ");
System.out.println("---------------------------------");
System.out.println("U1 = " +u1+ "|" );
System.out.println("raison = " +raison+ "|" );
System.out.println("U"+ ++nterme+ "=" + result);
System.out.println("Voulez-vous réessayer ?(O/N)");
reponse = sc.nextLine().charAt(0);
}
System.out.println("Au revoir...");
}
} |
merci a tous ceux qui voudront bien m'éclaircir un peu^^
Partager