Lab Assignment #2 :: CPTR 105 :: 03/13/09

  1. Write a program that reads a one-line sentence and displays the following responses:

Solution

Scanner kb = new Scanner(System.in);
String inputLine = kb.nextLine();
int stringLength = inputLine.length();

if (inputLine.charAt(stringLength - 1) == '?' && (stringLength % 2 == 0))
    System.out.println("Yes");
else if (inputLine.charAt(stringLength - 1) == '?' && (stringLength % 2 == 1))
    System.out.println("No");
else if (inputLine.charAt(stringLength - 1) == '!')
    System.out.println("Wow");
else 
    System.out.println("You always say \"" + inputLine + "\"");