我试图使这两个if语句如果为true则输出。

就像我输入:

cat
car


我希望它输出:

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


现在我只得到第一个输出

这是代码:

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

最佳答案

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

对于输入car ot cat将不是true

c不等于C

读取值后,添加此代码

myObj = myObj.toUpperCase();
myObj1 = myObj1.toUpperCase();

09-04 22:39