calculation after if. user input

calculation after if. user input

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. Patato"); System.out.println("5. Turnips"); System.out.println("6. Carrots");

fruit = in.nextInt(); if (fruit <=1) { System.out.println ("Super choice"); System.out.print("Enter quantity of apples required "); aQuantity = in.nextInt(); appleCost = aPrice * aQuantity; //calculate final cost by adding tax String fs1 = String.format(" your apples will cost you: %.2f", appleCost); System.out.println(fs1); }

else if (fruit <=2){

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);

}
else if (fruit <=3){

this is what im getting System.out.println(+appleCost + carrotCost+ simple addition 5 errors found: File: C:\Users\ Error: variable appleCost might not have been initialized File: C:\Users\ Error: variable carrotCost might not have been initialized

How do i initalize appleCost ect if its inside an if and it needs user input????

View Answers

October 23, 2011 at 6:42 PM

My apologies good evening peoples

Once again I am on top of my head if anyone could point me in the right direction I would be very grateful.

As you can see from snippet of code below I can choose 1 of the following items (i have to pick 3 items so have the below done 3 times it works I am not to use loops yet) the problem is at the end of picking my products it should add up total. It doesn?t seem to do that without the ifs, if else it will. I have tried putting the nested if I press 1 for apples then it just makes me take strawberries ..... thinking about this all day in work and its now 4.45 am


October 24, 2011 at 12:07 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);
}
}

October 25, 2011 at 9:19 PM

Tks for reply eventually got it to work out Not as neetly but it worked. i dont know if i should start loving this java of hate it.

System.out.print("Enter quantity of turnips required ");

Quantity = in.nextInt(); basket3 = tPrice * Quantity; //calculate final cost String fs1 = String.format(" your turnips will cost you: %.2f", +basket3); System.out.println(fs1);}

basket = basket1+basket2+basket3; System.out.println (" your basket total comes to $"+basket); if (basket <19.99 ) { System.out.println("no discount today"); yourdiscount = basket*discount; total = basket-yourdiscount;









Related Tutorials/Questions & Answers:
calculation after if. user input
calculation after if. user input  System.out.print ("please select... user input????   My apologies good evening peoples Once again I am...[]args){ Scanner input=new Scanner(System.in); System.out.print
escaping user input in php
escaping user input in php  Is it possible to escape the user input while submitting data into database in PHP
Advertisements
Java User Input
Java User Input  I am using Scanner class for user Input. Scanner s = new Scanner(System.in); String name = s.next(); but i am unable to store full name in name variable...how i can store full name plsss reply   use
user input to database
user input to database  /* * Studentinfo2App.java */ package studentinfo2; import java.sql.Connection; import java.sql.DriverManager; import... in my database user input from my applet does not show
user input to database
user input to database  /* * Studentinfo2App.java */ package studentinfo2; import java.sql.Connection; import java.sql.DriverManager; import... in my database user input from my applet does not show
user input to database
user input to database  /* * Studentinfo2App.java */ package studentinfo2; import java.sql.Connection; import java.sql.DriverManager; import... in my database user input from my applet does not show
button to accept user input
the radiobutton and spinner.The user input does not show on my Studentinfo.mdb...button to accept user input  private void jButton1ActionPerformed... that accepts firstname, lastname and gender from user and insert to database
How to read user input in C++
How to read user input in C++  How can i get an input from the users in C++?   You can use the standard input "cin" to get the input from user.. For example: int age; cin >> age
calculation
from text box and display it to user. the requirement is..from total amount... employee is issued with voucher number after paying,and if required we can add some... amount and display it to user...can any one help me as soon as possible using jsp
java user input - Java Beginners
java user input  how to input from keyboard? please give a example.  Hi Friend, There are lot of ways to input data from keyboard.You...:****** try{ System.out.print("Enter String:"); BufferedReader input = new
Java get User Input
Java get User Input       In this section, you will learn how to get the user input from... that will communicate with the user at the command line and returns the user input. We have
user input in java - Java Beginners
user input in java   i am trying to write a code to compute the average of 5 numbers but i dont know what to do for the program to accept user input  Hi import java.util.*; public class AverageExample { public
error in taking input from user.
error in taking input from user.  //i m having problem inputting the array from user.my program is import java.io.*; class bubble { public static void main(String args[]) { int a[]=new int[20]; int i=0; BufferedReader br=new
Design a registration.jsp page with input for user registration details
Design a registration.jsp page with input for user registration details  Design a registration.jsp page with input for user registration details like firstname, lastname, choose username, choose password, confirm password. After
How to take input by user using jDialogue and use that input
How to take input by user using jDialogue and use that input  I am using NetBeans i have a 'Search by employee id' button . I want when user click this button a dialogue box appear which take employee_id as input from user
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening the program. I am developing a college library management system, i would like
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening the program. I am developing a college library management system, i would like
Save the input data until user get changed
Save the input data until user get changed  I would like to know how to store the give input data and perform the calculations even after re-opening the program. I am developing a college library management system, i would like
XP Bowling Game User input help
XP Bowling Game User input help  I was asked to do the XP Bowling Example as an assignment. I have the programme passing all tests, but now I need to make the code accept input from a user and display the scores/frames
Write a program that takes the marks of user as input
Write a program that takes the marks of user as input  Write a program that takes the marks of user as input. If marks are greater then 60 the program should display "Great, you have passed the course", else the program should
Managing Multiple User Input Data in an Array (or ArrayList)
Managing Multiple User Input Data in an Array (or ArrayList)  Hey... record alphabetically (2) If user chooses choice1, the input data is stored in an ARRAY (or ARRAYLIST) until the user chooses to stop inputting data. Kindly
Display PHP clock with user input date and time
Display PHP clock with user input date and time  The following PHP code displays a clock with current date and time. I want the clock to receive user input date and time. How can this be done? <?php date<em>
Read user input in Java using Scanner
Example of of reading user input in Java using Scanner In this section we... input from console. Our sample program asks user to enter name and then age, user input is read using the scanner class. Information entered by user is printed
Getting ISD code when user input country name
Getting ISD code when user input country name  I want to create program which user needs by typing Country name he will get ISD code of that country... code, from that list i have to read the file and when user input any country name
input
input  a java program that will exchange the last names of two students that were input by a user
how to retreive data from database of a particular user after login
how to retreive data from database of a particular user after login  .... after the user has filled the form he should go to the welcome page where he... my question is how to retrieve this information of user after registration
how to retreive data from database of a particular user after login
how to retreive data from database of a particular user after login  ......please help in display details of particular student after he login ...i want to display the details of only current user who logged
how to send smtp mail to entered user mail id after user registered using javascript?
how to send smtp mail to entered user mail id after user registered using javascript?  how to send smtp mail to entered user mail id after user registered using javascript
how to send smtp mail to entered user mail id after user registered using javascript?
how to send smtp mail to entered user mail id after user registered using javascript?  how to send smtp mail to entered user mail id after user registered using javascript
ModuleNotFoundError: No module named 'calculation'
with following command: pip install calculation After the installation...ModuleNotFoundError: No module named 'calculation'  Hi, My Python... 'calculation' How to remove the ModuleNotFoundError: No module named '
age calculation
age calculation  pls send me the age calculation validation coding in java script. i'm waiting for ur reply
tax calculation
tax calculation  I want you to write a program while take some amount and do the tax calculation Sr.No. Particulars Present Rate Amount in INR 1 Service Fee - 100 2 Service tax Tax 12.00% 12 Education cess
ModuleNotFoundError: No module named 'refund_calculation'
After the installation of refund_calculation python library...ModuleNotFoundError: No module named 'refund_calculation'  Hi, My... named 'refund_calculation' How to remove the ModuleNotFoundError: No module
ModuleNotFoundError: No module named 'simplex_calculation'
_calculation After the installation of simplex_calculation python library...ModuleNotFoundError: No module named 'simplex_calculation'  Hi, My... named 'simplex_calculation' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'Solar-Calculation'
After the installation of Solar-Calculation python library, ModuleNotFoundError...ModuleNotFoundError: No module named 'Solar-Calculation'  Hi, My... named 'Solar-Calculation' How to remove the ModuleNotFoundError: No module
User Registration Form Using JSP(JspBeans) after that how i can insert in database
User Registration Form Using JSP(JspBeans) after that how i can insert in database   User Registration Form Using JSP(JspBeans) after that how i can insert in database
need help with two dimensional array that takes input from user and bubble sorts and selections sorts
need help with two dimensional array that takes input from user and bubble sorts and selections sorts  I can get both of the sorts figured out but i... how many elements are going to be input by the user?   Here is a code
Total Hours Calculation.
Total Hours Calculation.   Hi,How to calculate the total hours by startime and endtime.not railway time just only normal time.please help. Thanks
Create a Program that Calculates Input
Create a Program that Calculates Input   Create a program that takes user input and does a calculation with it, then prints the results back for the user. This could include like a sales tax calculator, or a tip calculator
Servlet Error Message based on user input
Servlet Error Message based on user input   ... to check the user input against database and display the message to the user. This example illustrate how to ensure that user input is correct by validating
Array and input
[ ]={1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26}; i want to ask user to input one of the above numbers then i want to print the array without the number user input. how will i do tht
ModuleNotFoundError: No module named 'odoo8-addon-product-template-book-weight-calculation'
-calculation After the installation of odoo8-addon-product-template-book...-calculation'  Hi, My Python program is throwing following error...-weight-calculation' How to remove the ModuleNotFoundError: No module named '
input box
input box  give me the code of input box in core java
input box
input box  give me the code of input box in core java
execution time calculation - JSP-Servlet
and after the program block. It returns a long variable. ======================= Eg
input output
input to read the user input..ADS_TO_REPLACE_1 In this section, you will see how... Input And Output       Introduction The Java I/O means Java Input/Output and is a part
Simple input application in Echo3
an application which contains the window from where the user can  input a text and this value would be shown into the content pane when user will click... Simple input application in Echo3   
Input - Output String Functions
Input - Output String Functions      ... data from a user (text field, text area) since it is not secure to receive a string data and input them directly into your database. Some special characters
Input in Stateless Bean - EJB
Input in Stateless Bean  Hello, I am having problem in taking the input in stateless bean, when I am trying to get integer or double type input from the user in the client file, It shows me the error
how to done calculation for dynamic generated textboxes in jquery/javascript/ajax?
how to done calculation for dynamic generated textboxes in jquery/javascript/ajax?  how to done calculation for dynamic generated textboxes in jquery/javascript/ajax

Ads