I'm starting out in Java
I produced the following class to test switch and conditional operators:
[highlight = Java]
import java.util.Scanner;
public class TestingSwitchThenConditionals {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter password ");
String password = input.nextLine();
switch (password)
{
case "Paris" : System.out.println("Correct");
System.out.println("you're in");
break;
case "London" : System.out.println("Almost");
break;
default: System.out.println("No entry!!!");
}
for (double count = 1;count <=5; count++)
{
System.out.printf("%25s to the power of %.0f = %.0f\n", count, count , Math.pow(count, count));
if ((count>=2) && (password == "London") )
{
System.out.printf("%s\n", password);
}
}
}
[/highlight]
When I use the && conditional it doesn't seem to 'work' in the sense that if I enter the password as London the contents of the if statement are ignored even when count is >=2
When I use the ^ conditional & enter London as the password I was expecting the if statement to execute only if count = 1 i.e only 1 of the operands is true & the other false (I'm using Java, How to Program 9th ed, p 226 for definition of ^ which tells me that BOTH operands are evaluated)
Any help would be greatly appreciated