vendredi 20 septembre 2019

Why does my code asking me twice about "amount of kids" while it should ask once because of the if statement. JAVA HW

I tried to change the "equals." to "==" but it is not working. I actually learning Java in my school and I love Java. If (someone could help me I will appreciate that) Thank You;

 import java.util.Scanner;
public class Lesson7Ex5 {
public static Scanner reader=new Scanner (System.in);
    public static void main(String[] args) {
int galil=0,gakids=0,negev=0,nekids=0,gaadults=0,neadults=0;
for (int i=1;i<=50;i++)
{
System.out.println("enter type of trip");
String type=reader.next();
if (type.equals("galil"))
    galil++;
    System.out.println("enter amount of kids");
    gakids+=reader.nextInt();
    System.out.println("enter amount of adults");
    gaadults+=reader.nextInt();
if (type.equals("negev"))
    negev++;
    System.out.println("enter amount of kids");
    nekids+=reader.nextInt();
    System.out.println("enter amount of adults");
    neadults+=reader.nextInt(); 
}
System.out.println("the amount of kids that goes to the galil is:"+gakids+"and the amount of kids that goes to the negev is:"+nekids);
System.out.println("the amount of adults that goes to the galil is:"+gaadults+"and the amount of adults that goes to the negev is:"+neadults);
System.out.println("the amount of familys that goes to the galil is:"+galil+"and the amount of familys that goes to the negev is:"+negev);


    }

}    

Aucun commentaire:

Enregistrer un commentaire