Functioning if statement, improving it

if (dog.equalsIgnoreCase("yes")) {
    drink.don.setCost(8.75);
    drink.don.getType();
    drin.l.add(drink.don.getType());
    drink.c.add((double) coke.don.getCost());
    cokeprice = coke + fanta.don.getCost();
else if (dog.equalsIgnoreCase("no"))
else catch(IllegalArgumentException iae) {
    System.out.println("requires yes or no");
}
}

Ignore the stupid naming conventions had to change them, incase any class mates decided to steal anything ;p

I'm trying to get my if statement to allow the user input yes and do a condition, then if "no" has been entered then nothing happens just moves onto the next statement, then anything else is illegal and the program crashes.

Answers


I don't know what language you are using, nor do I know what any of the methods you are using do, but here is an example of a similar statement in C#.

First, use a method to convert the user input to a true or false (boolean) value:

public static bool IsYes (string userInput)
{
    if (userInput == "yes')
    {
         return true;
    }
    else  if (userInput == "no")
    {
        return false;
    }
    else
    {
        throw new  CustomException();
    }
}

Next, you can take the result of IsYes() and use it for the if else statement:

if (IsYes(userInput))
{
    // code you want to execute if "yes" 
}
else
{
    // code you want to execute if "no"
}

Hopefully this code will give you an idea of how to use if-else statements, but in the future please explain your question more clearely. Remember, this is C#, so although if statements are similar in almost all languages some of the other code will differ. Also, this is just an example, it won't do anything on its own.


I don't like throwing exceptions, especially if I expect that the user might type in something that I don't want. I'd rather do something like

if (userInput.equalsIgnoreCase("Yes")) {
    // do yes
}
else if (userInput.equalsIgnoreCase("No")) {
    // do no
}
else {
    // Sorry, invalid input
}

Need Your Help

asp.net web API - no dll is available containing Microsoft.Practices.ServiceLocation.IServiceLocator

asp.net dependency-injection asp.net-web-api

So I want to set up dependency injection for my ASP.NET Web API project. Microsoft appear to have changed the way that DI is set up in MVC since version 3. Now I need to make a call like this:

Does every newer Android-phone use libtinyalsa.so meanwhile?

android audio standards alsa

Some years ago, there where multiple versions of how Android handles audio. There where OSS, then ALSA, then TinyAlsa and so on.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.