mercredi 1 février 2017

Using Scanner library and if statment, simple task?

I need a little help. I'm learning Java and I'm stuck in one very simple exercise. Here is:

"Define the variables x and y as integers. The user needs to enter values for x and y. Write control data with if statement:

If x> 3, print "x is greater than 3"

If x <5, and y> 10, print "x is less than 5, and y greater than 10"

If y <10, print "y is less than 10"

If neither condition is not correct, print "condition is not correct''.

I've been using if statement, and simple input method where the program asks me to type my name, and after typing , program says "you're name is: ", but in this case I don't know how to combine library java.util.Scanner and method .next().

I Stuck here, am I doing right?

package exercise03;

import java.util.Scanner;
public class exercise03 {
public static void main(String[] args) {

Scanner input  = new Scanner(System.in);

   int x;
   int y;

   if(x>3){
       System.out.println("X greater than 3");
   }

   else if(x<5 && y>10){
       System.out.println("X is less than 5, and Y is greater than 10");
   }    

   else if(y<10){
       System.out.println("Y is less than 10");
   }

       System.out.println("Condition is not correct");

   }

}

Aucun commentaire:

Enregistrer un commentaire