using if and else if statments

using if and else if statments

Hello everyone Hope all are well Just new here & already have a question.

I have a little veg shop it sells 6 apples oranges turnips patotes straberries carrots. And add the price together. oh each item has a price eg apples = .30 ect my problem is I can only buy 3 of the above items without using loops or switch or while Im beginner so only if and nested if and else if This has my head in bits for days I have the below done x6 for each product How i get it to 3 (without throwing the other 3 items away)

double oPrice, orangesCost; int oQuantity; oPrice = 0.45; System.out.println(" \t\t Product Price Calculation"); System.out.print("Enter quantity of oranges required "); oQuantity = in.nextInt(); orangesCost = oPrice * oQuantity; //calculate final cost by adding tax String fs2 = String.format(" your oranges will cost you: %.2f", orangesCost); System.out.println(fs2);

View Answers

October 24, 2011 at 12:27 PM

import java.util.*;
class Example1{
    public static void main(String[]args){
        Scanner input=new Scanner(System.in);
        System.out.print ("please select one of the following by pressing tne number on your keypad");
        System.out.println();
        System.out.println("1. Apples");
        System.out.println("2. Oranges");
        System.out.println("3. Straberries");
        System.out.println("4. Potato"); 
        System.out.println("5. Turnips");
        System.out.println("6. Carrots");
        int fruit=0,quantity=0,totalcost=0;
        fruit = input.nextInt(); 
        if (fruit ==1){ 
                System.out.println ("Super choice"); 
                System.out.print("Enter quantity of apples required: "); 
                quantity = input.nextInt();
                int price=100;
                totalcost = price * quantity; 
        }
        if (fruit ==2){ 
                System.out.print("Enter quantity of oranges required: "); 
                quantity = input.nextInt();
                int price=50;
                totalcost = price * quantity; 
                }
        if (fruit ==3){ 
                System.out.print("Enter quantity of strawberries required: "); 
                quantity = input.nextInt();
                int price=200;
                totalcost = price * quantity; 
        }
        if (fruit ==4){ 
                System.out.print("Enter quantity of potatoes required: "); 
                quantity = input.nextInt();
                int price=10;
                totalcost = price * quantity; 
        }
        if (fruit ==5){ 
                System.out.print("Enter quantity of apples required: "); 
                quantity = input.nextInt();
                int price=25;
                totalcost = price * quantity; 
        }
        if (fruit ==6){ 
                System.out.print("Enter quantity of apples required: "); 
                quantity = input.nextInt();
                int price=20;
                totalcost = price * quantity; 
        }
        System.out.println("Total Cost: "+totalcost);
}
}

Ads









Related Tutorials/Questions & Answers:
using if and else if statments
using if and else if statments  Hello everyone Hope all are well Just... of the above items without using loops or switch or while Im beginner so only if and nested if and else if This has my head in bits for days I have the below done x6
Java sample using - for loop, if then else etc etc
Java sample using - for loop, if then else etc etc  sample program to produce summary information on sales report. The program will input Data of Salesman Id, Item code, and number of cuestomer. Sales id is 5digit long
Advertisements
programes on if....else
or not write a program to find minimum of 3 nos using nested if else. write... a program to input score of a student out of 100 and print grades using else...){ System.out.println("Leap Year!"); } else{ System.out.println("Not a Leap
If and else if ladder
If and else if ladder  How we can use if and else if statement?  ...) System.out.println("Grater than 100"); else if(i < 50) System.out.println("Grater than 50"); else
if/else statements
if/else statements  I'm having a hard time programming this question: code an if/else statement that test the value of a string variable named.... If the value is "UPS," code another if/else statement that tests the value
If-else not working
If-else not working  <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <html> <...="SELECT REGNO,SNAME FROM sub_comb WHERE L_category='Lab'"; } else if (Lab
If-else not working
If-else not working  <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> <html> <...="SELECT REGNO,SNAME FROM sub_comb WHERE L_category='Lab'"; } else if (Lab
if else condition - Java Beginners
if else condition  Please explain the if else condition in Java programming language
if else statement in java
if else statement in java  if else statement in java explain with example
if else statement in java
if else statement in java  explain about simple if else statement and complex if else statement in java with an example
Objective C if else statements syntax
Objective C if else statements syntax  What is the syntax of if else statements in Objective C?   Using of if else statement in Objective C There is no changes in the syntax format of if else statement. You can write
if else in javascript function
if else in javascript function  How can i write the else statement in JavaScript to check if the web browser is IE or not? Is there any other way to validate this way ..? Thanks
switch case instead of if else
switch case instead of if else  How to write code in switch case instead of if else in Java
Java if else condition
Java if else condition  its there any similar condition for (X<=5 || y<=10) please post the answer as soon as possible.... TQ
IF-ELSE In JSTL
IF-ELSE In JSTL In this section we will read about implementation of if-else tag in JSTL. We will see the implementation of this tag using a simple example. In JSTL if-else tag is not implemented directly like the if-else statement
check condition in if-else block
check condition in if-else block  Hi, I want to update database with following query int i= st.executeUpdate("Insert into text_editor(words) values('"+words+"')"); how can I give a check option so only valid words are inserted
Java if else
Java if else       The if-else statement is one of the widely used control flow statements while... to the programmer to handle the condition based programming. Syntax for if-else
jstl if else
In this section we have discussed about how to implement if-else tag in JSTL using a simple example. <c:if> tag does not work if the value entered..., as their is not default value. Hence, if-else tag is not implemented directly but is implemented
ModuleNotFoundError: No module named 'maybe-else'
ModuleNotFoundError: No module named 'maybe-else'  Hi, My Python... 'maybe-else' How to remove the ModuleNotFoundError: No module named 'maybe-else' error? Thanks   Hi, In your python environment you
The else Keyword
The else Keyword        The else is the one more keyword of Java. It is used to specify a statement..., the code after the else keyword is executed. It is an optional part of a branching
IF-Else Conditional Statement
IF-Else Conditional Statement Conditional Statements are used to carry out actions based on condition. If-Else is also a conditional statement used... the following conditional statement : if statement if...else statement
Mysql Else Condition
Mysql Else Condition       Mysql Else Condition evaluate the statements in database to perform... illustrate an example from the 'Mysql Else Condition'.To understand
Need Help Like No one Else Please
Need Help Like No one Else Please   write a program that computes miles per gallon for an automobile. The program should ask for the beginning mileage first. For each fill-up, the program should ask for the mileage and number
convert the following Pseudocode to java if/else structure
convert the following Pseudocode to java if/else structure   dirfrom=D:/processimagessoftware/software/images dirto=D:/processimagessoftware/software/processed/ dirto=D:/imagevideodb/ author=3 country=100 state=34
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO..."); System.out.println("| q. Quit"); question = console.next().charAt(0... 'l': CheckEmptyList(); case 'q': Quit(); case 'Q
Use if, if else,for,while,do wile ad switch in same Java Class
Use if, if else,for,while,do wile ad switch in same Java Class  hi how to use if, if else,for,while,do wile ad switch in same class? very urgent..   import java.util.*; import java.text.*; class Shop{ public String
Code to STOP Students from navigating anywhere else till the Online exam is over
Code to STOP Students from navigating anywhere else till the Online exam... is not able to navigate(can't open anything else just work on test) anywhere else... or how to do so for BOTH VERSIONS i am using netbeans IDE but i will prefer
using function
using function  write a program using function overloading to accept...') ||( ch == 'o') ||( ch == 'u')){ } else... == 'u')){ } else{ System.out.println(ch
If- Else Ladder
;January". We are using If- Else loop for the ladder. The loop will travel up...; <TITLE>Using an if-else Ladder</TITLE> </HEAD> <...If- Else Ladder      
java program using control statements...
java program using control statements...  public class...){ System.out.println("It is zero"); else if(x>0) System.out.println("it is positive number"); else if(x<0) System.out.println
Struts2 Else Tag Example
Struts2.2.1 Else Tag Example
using array
using array  transpose a matrix
using array
using array  transpose a matrix
using prompt
using prompt  how to use prompt
using array
using array  column wise total of a matrix
using function
using function   using a function to find the value of v from the foiiowing expression 1v=1u+1*f
using array
using array  Circular left shift array element by one position
using array
using array  Circular left shift array element by one position
using array
using array  read 10 digit number and display (star
using array
using array  display 10 digit number using array and print pyramid. 1 1 1 1 1 1 1 1 1 1 1 1
problem in using getKeyCode()
problem in using getKeyCode()  i am working with the jlist...while i was trying to use the arrow keys in the keyboard to select the values.../ jList.setSelectedIndex(0); jList.setVisible(false); } else
Using Ajax
Using Ajax  Hi, How I can use Ajax in web programming? Tell me any example which using Ajax to call server side script? Thanks   Hi, Here are examples: PHP Ajax and Database First Ajax Example Getting Started
using for loop
using for loop  what will be the source code of the program that the output will be printing all numbers that is divisible by 3 and 5 sample output: 3 is divisible by 3 5 is divisible by 5 6 is divisible by 3 9 is divisible by 3
using iReport
using iReport  Hello sir, I am using iReport3.0 and then i am calling the .jrxml file in jsp but i got the error.My JSP code is as below and after the jsp code i have placed the error code Thanks in advance: &lt
using constructor
using constructor  all constructor for matrix class?   The given code implements the Matrix class and show the addition of two matrices. public class Matrix{ int M; int N
Struts2 If,Else,ElseIf Tag Example

Ads