Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. package string;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class finalTR_Game {
  6. public static void main (String []args) {
  7.  
  8. Scanner in = new Scanner(System.in);
  9.  
  10. boolean isLegal = true;
  11.  
  12. System.out.print("Enter sentence: ");
  13. String str = in.nextLine();
  14.  
  15. System.out.print("Enter number of turn: ");
  16. int numTurn = in.nextInt();
  17.  
  18. String lastWord = str.substring(str.lastIndexOf(" ")+1); // לוקחת את המילה האחרונה
  19.  
  20. for(int i=0; i < numTurn ; i++) {
  21. System.out.print("Enter another sentence: ");
  22. str = in.nextLine();
  23.  
  24. String firstWord = str.substring(0 , str.indexOf(' '));
  25.  
  26. if(!(lastWord == firstWord)) {
  27. isLegal = false;
  28. break;
  29. }
  30. lastWord = str.substring(str.lastIndexOf(" ")+1);
  31. }
  32.  
  33. if(!(isLegal)) {
  34. System.out.println("Error wrong word!!");
  35. }else {
  36. System.out.println("Game Over");
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement