IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Java:total marks donne fausse résultat


Sujet :

avec Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut Java:total marks donne fausse résultat
    bonjour
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    import java.util.*;
    public class Student 
    {
      int rollNo;
      double sub1;
      double sub2;
      double sub3;
      double totalMarks;
      
      public Student(int rollNo,double sub1,double sub2,double sub3,double totalMarks)
      {
    	this.rollNo=rollNo;
    	this.sub1=sub1;
    	this.sub2=sub2;
    	this.sub3=sub3;
    	this.totalMarks=totalMarks;
      }
      public int getRollNo()
      {
    	  return rollNo;
      }
      public double getSub1()
      {
    	  return sub1;
      }
      public double getSub2()
      {
    	  return sub2;
      }
      public double getSub3()
      {
    	  return sub3;
      }
      public double getTotalMarks()
      {
    	  return totalMarks;
      }
    
    public static void main (String[]args)
    {
    	Scanner input=new Scanner(System.in);
    	double max1=0,max2=0,max3=0,max=0;
    	int no=0;
    	double s1=0,s2=0,s3=0,tm=0;
    	ArrayList<Student> list=new ArrayList<Student>();
    	for(int i=1;i<=2;i++)
    	{
    	 System.out.print("Enter Roll No:");
    	 no=input.nextInt();
    	 System.out.print("Enter marks of maths:");
    	 s1=input.nextDouble();
    	 System.out.print("Enter marks of science:");
    	 s2=input.nextDouble();
    	 System.out.print("Enter marks of Hindi:");
    	 s3=input.nextDouble();
    	 tm=s1+s2+s3;
    	 list.add(new Student(no,s1,s2,s3,/*s4,s5,*/tm));
    	 System.out.println();
    	 
    	 if(s1>max1)
    	 {
    	  max1=s1;	 
    	 }
    	 if(s2>max2)
    	 {
    	  max2=s2;	 
    	 }
    	 if(s3>max3)
    	 {
    	  max3=s3;	 
    	 }
    	 if(tm>max)
    	 {
    	  max=tm;	 
    	 }}
    	 System.out.println("********Roll No and TOTAL Marks*********");
    	 System.out.println("Roll No   Total Marks");
    	 for(Student s:list)
    	 {
    		 System.out.println(s.getSub1()+""+s.getSub2());
    		 //System.out.println(tm);
    	 }
    	 System.out.println("");
    	 System.out.println("********Highest marks in each subject with RollNo*******");
    	 for(Student s:list)
    	 {
    		 if(s.getSub1()==max1)
    		 {
    			 System.out.println("Roll No"+s.getRollNo()+"get highest marks in Subject1 i.e"+max1);	 
    		 }
    	 }
    	 for (Student s:list)
    	 {
    	   if(s.getSub2()==max2)
    	   {
    		   System.out.println("Roll No"+s.getRollNo()+"get highest marks in Subject 2 i.e"+max2);
    	   }
    	 }
    	 System.out.println("");
    	 for (Student s:list)
    	 {
    	   if(s.getSub3()==max3)
    	   {
    		   System.out.println("Roll No"+s.getRollNo()+"get highest marks in Subject 3 i.e"+max3);
    	   }
    	 }
    	 System.out.println();
    	 System.out.println("*****************Student Obtained Highest Marks**********");
    	 for(Student s:list)
    	 {
    		 if(s.getTotalMarks()==max)
    		 {
    			 System.out.println("Roll No"+s.getRollNo()+"get highest Marks"); 
    		 }
    		 
    	 }
    	 }
    	}
    resultat:
    Enter Roll No:1
    Enter marks of maths:12
    Enter marks of science:4
    Enter marks of Hindi:15

    Enter Roll No:2
    Enter marks of maths:13
    Enter marks of science:2
    Enter marks of Hindi:12

    ********Roll No and TOTAL Marks*********
    Roll No Total Marks
    12.04.0
    13.02.0


    ********Highest marks in each subject with RollNo*******
    Roll No2get highest marks in Subject1 i.e13.0
    Roll No1get highest marks in Subject 2 i.e4.0

    Roll No1get highest marks in Subject 3 i.e15.0

    *****************Student Obtained Highest Marks**********
    Roll No1get highest Marks

    aidez moi svp

  2. #2
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 7
    Points : 10
    Points
    10
    Par défaut
    Tu peux nous expliquer plus clairement ton code et ton erreur, qu'on comprenne ce qui va pas, stp!!

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     System.out.println("********Roll No and TOTAL Marks*********");
    	 System.out.println("Roll No   Total Marks");
    	 for(Student s:list)
    	 {
    		 System.out.println(s.getSub1()+""+s.getSub2());
    		 //System.out.println(tm);
    	 }
    j'obtiens ce resultat
    ********Roll No and TOTAL Marks*********
    Roll No Total Marks
    12.04.0
    13.02.0

    je veux il s'affiche comme suivant:
    ********Roll No and TOTAL Marks*********
    Roll No 1 TotaL Marks
    31.0
    27.0

    car roll no 1 à additionner 12+4+15
    roll no 2 à additionner 13+2+12

  4. #4
    Membre émérite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Septembre 2008
    Messages
    1 190
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2008
    Messages : 1 190
    Points : 2 657
    Points
    2 657
    Par défaut
    On est pas devin tu sais... Et là on ne sait pas du tout ce que tu veux faire.
    Donc déjà met nous un contexte sur ton code, qu'est ce qu'il signifie et essaye de faire au juste?

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    la premier poste indique les codes intégral et en sous vous voyez le résultat de
    ce programme;les fonts en gras les codes qui ont des pb
    il s'affiche aucun code erreur;
    mais une partie de mon prg donne le résultat faux
    "Roll No and TOTAL Marks"

  6. #6
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    Ne met pas en forme les codes car ça fait disparaître les couleurs syntaxiques. Ce que tu cherches n'est pas celle ci
    tm=s1+s2+s3;

  7. #7
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    non,j'ai déjà essayé marche pas

  8. #8
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    for(Student s:list)
    	 {
    		 System.out.println(s.getTotalMarks); //<===
    		// System.out.println(tm);
    	 }

  9. #9
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    je l'ai fait
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    for(Student s:list)
    	 {
    		 System.out.println(s.getTotalMarks);
    		 System.out.println(s.getSub1()+""+s.getSub2());
    		 System.out.println(tm);
    	 }
    j'ai erreur message suivant
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    getTotalMarks cannot be resolved or is not a field

    at Student.main(Student.java:80)

  10. #10
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 7
    Points : 10
    Points
    10
    Par défaut
    J'ai (enfin) compris ton code et ce que tu voulais faire.
    Donc le seul bug c'est que tu demande pas à ton programme d'affiche la donnée que tu veux.

    Tu fais :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    System.out.println("********Roll No and TOTAL Marks*********");
    System.out.println("Roll No   Total Marks");
    for(Student s:list){
      System.out.println(s.getSub1()+""+s.getSub2());
      //System.out.println(tm);
    }
    Mais tu devrais faire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    System.out.println("********Roll No and TOTAL Marks*********");
    System.out.println("Roll No   Total Marks");
    for(Student s:list){
      System.out.println(s.getTotalMarks());
    }
    (Dans ton dernier post, tu n'as pas mis les parenthèses de l'appel à la méthode, tu n'avais d'ailleurs même pas chercher à résoudre cette dernière erreur qui est très simple.... )

  11. #11
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2008
    Messages
    684
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2008
    Messages : 684
    Points : 147
    Points
    147
    Par défaut
    merci beaucoup ça marche

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 12
    Dernier message: 22/04/2015, 09h06
  2. Quelle formule donne ces résultats
    Par Vincdeladrome dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 18/12/2006, 12h34
  3. Java Banque de donnée et GUI
    Par gagalive dans le forum Général Java
    Réponses: 11
    Dernier message: 04/06/2006, 09h37
  4. crc32() donne un résultat négatif
    Par Hal2001 dans le forum Langage
    Réponses: 1
    Dernier message: 19/04/2006, 18h11
  5. Script Java appelé dans Xsl sans résultats
    Par metallic dans le forum XSL/XSLT/XPATH
    Réponses: 7
    Dernier message: 03/08/2005, 15h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo