mercredi 28 septembre 2016

If/else with blood types

Beginner here. I am supposed to write a program that takes as input his blood type, and the blood type of his prey and outputs whether they are safe to consume. From the chart: http://ift.tt/2dALkUs The vampire is the recipient and the prey is the donor.

If/else statements are the first thing that came to mind. I am unsure how to determine the two blood types in an if else after i determined that the blood type at least had A,B,O in it. If someone could explain to me the steps, I would appreciate it a bunch.

import java.util.Scanner; 



public class Hw2pr6 {

public static void main(String[] args)
{

    String vampBlood, bloodEat;


    Scanner keyboard = new Scanner (System.in);

    System.out.print("\tWhat is the vampire's blood type? ");
    vampBlood = keyboard.nextLine();

    System.out.print("\tWhich blood type is on the menu today? ");
    bloodEat = keyboard.nextLine();


    if (bloodEat.contains("A") || bloodEat.contains("O") || bloodEat.contains("B")) {

        if (vampBlood == "AB+"){

            System.out.print ("Safe to consume!");

        }

        if (vampBlood == "AB-"){

            System.out.print ("Safe to consume!");

        }

        if (vampBlood == "A+"){

            System.out.print ("Safe to consume!");

        }

        if (vampBlood == "A-"){

            System.out.print ("Safe to consume!");

        }



    }

    else 

    {
        System.out.print ("That is not a valid blood type.\n");
    }

}

}

Aucun commentaire:

Enregistrer un commentaire