dimanche 18 août 2019

Control of program seems not entering the if statement [duplicate]

This question already has an answer here:

I have 2 class files. The code of both classes are given below. I run the program and give the values aaa 15 mumbai bbb 15 mumbai ccc 15 mumbai

I expect the output to be 3, but it gives 0.

Student.java

public class Student{

public String name;

public double age;

public String city;

public Student(String a,Double b,String c){

this.name=a;

this.age=b;

this.city=c;

}

}

Solution.java

import java.util.Scanner;

public class Solution{

public static int studentCountWithSameCityAndAge(Student x,Student y,Student z){

int count=0;

if((x.age==y.age)&&(x.city==y.city)){

count=2;

}

if((x.age==z.age)&&(x.city==z.city)){

count++;

}

else if((x.age==z.age)&&(x.city==z.city)){

count=2;

}

else if((y.age==z.age)&&(y.city==z.city)) {

count=2;

}

return count;

}

public static void main(String args[]){

Scanner scan=new Scanner(System.in);

String name,city;

double age;

Student e1,e2,e3;

name=scan.nextLine();

age=scan.nextDouble();

scan.nextLine();

city=scan.nextLine();

e1=new Student(name,age,city);

System.out.println(e1.name+" "+e1.age+" "+e1.city);

name=scan.nextLine();

age=scan.nextDouble();

scan.nextLine();

city=scan.nextLine();

e2=new Student(name,age,city);

System.out.println(e2.name+" "+e2.age+" "+e2.city);

name=scan.nextLine();

age=scan.nextDouble();

scan.nextLine();

city=scan.nextLine();

e3=new Student(name,age,city);

System.out.println(e3.name+" "+e3.age+" "+e3.city);

int num;

num=studentCountWithSameCityAndAge(e1,e2,e3);

System.out.println(num);

}

}

I expect the output to be 3, but it gives 0.

Aucun commentaire:

Enregistrer un commentaire