Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i find correct total_score in this code?

print 25 for n=3 Happy sad happy
where n is no of elements it seems to print total_score as 0 every time
also mention the reason i am facing this problem thanks for the help in advance

import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    public class Mood {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int n= sc.nextInt();
            HashMap<Integer, String> hmap = new HashMap<>();
         /* for (int i = 0; i < n; i++) {
                Integer a = sc.nextInt();
                String b = sc.next();
    
                hmap.put(a, b);
            }*/
    
            String[] string = new String [n];
            sc.nextLine();
            for (int i = 0; i < n; i++)
            {
                string[i] = sc.nextLine();
            }
           /* HashMap<String, Integer> mood = new HashMap<>();
            mood.put("Happy",10);
            mood.put("Sad",5);
            mood.put("Neutral",2);
            mood.put("None",1);*/
            int Total_score=0;
            for (int i = 0; i <n ; i++) {
                if(string[i]=="Happy"){
                    Total_score=Total_score+10;}
                else if (string[i]=="Sad"){
                    Total_score=Total_score+5;}
                else if (string[i]=="Neutral"){
                    Total_score=Total_score+2;}
                else if (string[i]=="None"){
                    Total_score=Total_score+1;}
                else
                    break;
            }
            System.out.println(Total_score);
    
        }
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use this to compare strings myStr1.equals(myStr2) , i fell in this strap in java before

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading