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