This question already has an answer here:
- How to make this method more efficient? 4 answers
Hi guys i need a better way to use these if statements .How shall i make it easy. My code is to get user input and the input will determine which drink will be displayed on screen with cost .
public class Main {
public static void main(String[] args) {
int coffee =1;
int tea=2;
int chocolate =3;
int answer;
Scanner input = new Scanner(System.in);
System.out.print("Enter 1 for Coffee,2 for tea or 3 for chocalte");
answer =input.nextInt();
if(answer == 1)
{
System.out.println("Cofffee is £3.50 ");
}else {
if (answer == 2) {
System.out.println("Tea is £2.50 ");
}
}
if (answer == 3)
System.out.println("Chocolate is £2.00 ");
{
if (answer > 3)
System.out.println("No drinks match Number selected ");
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire