mardi 31 mars 2015

Java Nested If Help (The Logic is seemingly correct but always outputs the same thing)

I am trying to use nested if statements in order to determine the value of a variable and with this code, the correct results are not occurring. Please can someone help? I am using an array to hold values that are being compared to the input (using this in order to show "knowledge" of java). Then, I am calling the function testFlower() to initialize the logic. I am not able to identify where in my logic I went wrong. Every time anything is inputted, the result that is printed out by the print line is always "Roses". This returned String should change when different flower names are typed in but this does not occur. Does anybody have any ideas on the issue in the code?



import java.util.Scanner;

public class A {

public final double TAX = 1.127;
public final double s_basket = 10, m_basket = 20, l_basket = 30, s_elephant = 15, l_elephant = 30,
s_seal = 15, l_seal = 30, s_bear = 15, l_bear = 30, s_penguin = 15, l_penguin = 30,
mChocolate = 15, wChocolate = 20, dChocolate = 30;

public String flowers[] = {"ROSE", "TULIP", "DAISY", "DAFFODIL", "SUNFLOWER", "ORCHID"};

public Scanner s = new Scanner(System.in);

public String flowerType;


public void testFlower() {
if (!(flowerType.equals(flowers[1]))) {
if (!(flowerType.equals(flowers[2]))) {
if (!(flowerType.equals(flowers[3]))) {
if (!(flowerType.equals(flowers[4]))) {
if (!(flowerType.equals(flowers[5]))) {
flowerType = "Roses";
System.out.println(flowerType);
} else {
flowerType = "Orchids";
System.out.println(flowerType);
}
} else {
flowerType = "Sunflowers";
System.out.println(flowerType);
}
} else {
flowerType = "Daffodils";
System.out.println(flowerType);
}
} else {
flowerType = "Daises";
System.out.println(flowerType);
}
} else {
flowerType = "Tulips";
System.out.println(flowerType);
}
}

public static void main(String[] args) {
A a = new A();

System.out.println("Hello! Welcome to my flower shop! What type of flowers do you want? (rose, tulip, daisy, daffodil, sunflower, orchid)");
a.flowerType = a.s.nextLine();
a.flowerType.toUpperCase();

a.testFlower();

}
}

Aucun commentaire:

Enregistrer un commentaire