Respuesta :
Answer:
System.out.println("Enter 2 strings:");
Scanner scan = new Scanner(System.in);
String a = scan.nextLine();
String b = scan.nextLine();
int c = a.length();
int d = b.length();
String e = a.substring(0, a.length()-1);
String f = b.substring(0, b.length()-1);
if (a.equals(b)){
System.out.println("Great!");
}
else if (c== d && e.equals(f)){
System.out.println("Close enough");
}
else{
System.out.println("Try again");
}
}
}
Explanation:
Following are the java program to comparing the words:
Program Explanation:
- Import package.
- Defining the main class.
- Defining the main method.
- Inside the main method two string "f,s" is declared that uses input method to input value.
- After input value multiple conditional statement is declared that checks inputs value.
- In the if block, it checks first string value equal to second value, if it is true it will print a message "Great!".
- In the else if block it checks string value that has same length, for this it uses "l" variable that checks length value, and the print the message "Close enough".
- In the else block, it will print the message "Try again".
Program:
import java.util.*;//import package public class Main //defining a class { public static void main(String[] ars) //defining main method { String f,s;//defining String variable Scanner obc=new Scanner(System.in);//creating Scanner class object System.out.print("Enter First word: ");//print message f=obc.next();//input value System.out.print("Enter Second word: ");//print message s=obc.next();//input value if(f.equals(s))//use if to check input string value are equal System.out.println("Great!");//print message else if(f.length()==s.length())//using else if block that checks string value has same length { int c=0;//defining integer variable int l=f.length();//defining integer variable l that holds first string length value for(int i=0;i<l;i++) //using for loop to compare string value { if(f.charAt(i)==s.charAt(i))//use if to compare string by using charAt method c++;//incrementing c value } if(c==l-1 && f.charAt(l-1)!=s.charAt(l-1)) //comparing string { System.out.println("Close enough");//print message } } else //defining else { System.out.println("Try again");//print message } } }
Output:
Please find the attached file.
Learn more:
brainly.com/question/20875119
bvbmb

Otras preguntas
