java - if contains two words -
i brand new @ coding , can not application run right. please help!
i have written following code hw assignment:
import java.util.scanner; public class hw1q2 { public static void main(string[] args) { scanner keyboard = new scanner(system.in); string sentence, str1, str2; system.out.println("enter sentence containing either word \"blue\" or word \"green\" both or neither"); sentence = keyboard.nextline(); str1 = "blue"; str2 = "green"; if(sentence.contains("blue")); if(sentence.contains("green")){ system.out.println("sunny");} else{ if(sentence.contains("blue")){ system.out.println("ocean");} else{ if(sentence.contains("green")){ system.out.println("garden");} else{ system.out.println("dull"); }}} } }
the goal return
garden
if typegreen
ocean
if typeblue
sunny
if type both anddull
if type neither
the problem if write sentence includes green
, still returns sunny
not garden
.
you'll need use &&
, check blue , green. logic below should work. order critical. have check both words before start checking either word. otherwise print out ocean or garden before can print sunny.
if(sentence.contains("blue") && sentence.contains("green")) { system.out.println("sunny"); } else if (sequence.contains("blue")) { system.out.println("ocean"); } else if (sequence.contains("green")) { system.out.println("garden"); } else { system.out.println("dull"); }
Comments
Post a Comment