Profit Calculator in Java -
i trying build program calculate profit made 4 items.this code have far. im not doing calculation part yet, im trying allow user choose items want purchase , how store values.
public static void main(string[]args) { int item=0; double price=0; string itemname=""; string yes =""; string no=""; string answer=""; string response; scanner list=new scanner(system.in); system.out.println( "these current items availabe:" ); system.out.println( "item number\t item name" ); system.out.println( "1)\t\t flour\n2)\t\t juice\n3)\t\t crix\n4)\t\t cereal" ); system.out.println("enter item number wish purchase"); item=list.nextint(); if( item == 1 ) { price = 25; itemname = "flour"; system.out.println( "you selected flour" ); } else if( item == 2 ) { price = 15; itemname = "juice"; system.out.println( "you selected juice" ); } else if( item == 3 ) { price = 10; itemname = "crix"; system.out.println( "you selected crix" ); } else if( item == 4 ) { price = 30; itemname = "cereal"; system.out.println( "you selected cereal" ); } else { system.out.println( "invalid item number entered!" ); } return; system.out.println("would purchase item?"); scanner answer1=new scanner(system.in); response=answer1.next(); if(answer==yes) { system.out.println("enter item number wish purchase"); item=list.nextint(); } else if(answer==no) { system.out.println("thank shopping us"); }
the question is, how go doing or method far accurate?
also if else statements when answer yes or no, still asks enter item number wish purchase
if input no. how correct this?
this not right, on many levels:
string yes=""; //this empty string... name not mean anything... .... if(answer==yes){ //comparing empty string bad way...
should probably
private static final string yes="yes"; //now has content
and later
if(answer.equals(yes)) { //proper string equalitz checking ...
remember: string
s objects. use .equals()
compare equality.
apply same no
part of course.
also:
scanner answer1=new scanner(system.in); response=answer1.next(); //you store result response if(answer==yes){ //you check answer???
should be:
scanner answer1=new scanner(system.in); response=answer1.next(); //you store result response if(response.equals(yes)){ //correct check
Comments
Post a Comment