jeudi 26 septembre 2019

Multiple outputs based on each if statement being true. Java

I am trying to make both of these if statements output if true.

Like if I am enter:

cat
car

I want it to output:

Cat and Car are both the same length. 
Cat and Car both start with C.

Right now I am only getting the First output

Here is the code:

import java.util.Scanner;  // Import the Scanner class


public class Main
{
    public static void main(String[] args) {
        System.out.println("input words");

        String myObj, myObj1;

         Scanner sc = new Scanner(System.in);  // Create a Scanner object
         myObj = sc.nextLine();  // String Input
         myObj1 = sc.nextLine(); // String Input


         if(myObj.length() == myObj1.length()){  // System check for String Length
             System.out.println( myObj + " and " + myObj1 + " are the 
            same length.");

         }
         if ((myObj1.charAt(0) == 'C') && (myObj.charAt(0) == 'C')){

            System.out.println(myObj + " and " + myObj1 + " start with C." );
            } // Output if both start with C

Aucun commentaire:

Enregistrer un commentaire