Friday 15 March 2013

java - Coding Error Need Assistance -



java - Coding Error Need Assistance -

i working on exercise java programming book joyce farrell , have run issue. allow me explain exercise first.

the question in 2 parts , can't either work:

a. write application displays menu of 3 items in restaurant follows:

cheeseburger 4.99 pepsi 2.00 chips 0.75

prompt user take item using number (1, 2, or 3) corresponds item, or come in 0 quit application. after user makes first selection, if selection 0, display bill of $0. otherwise, display menu again. user should respond prompt item number order or 0 quit. if user types 0, display cost of single requested item. if user types 1, 2, or 3, add together cost of sec item first, , display menu 3rd time. if user types 0 quit, display total cost of 2 items; otherwise display total 3 selections. save file fastfood.java.

b. modify application in a. if user makes menu selection or has made, ignore selection - is, not add together sec cost same item total. user still allowed 3 entries. save file fastfood2.java.

here code far, need help.

import java.util.scanner;

public class fastfood { public static void main (string[] args) { int chosenitem1 = 1; int chosenitem2 = 2; int chosenitem3 = 3; double chosencost1 = 0.0; double chosencost2 = 0.0; double chosencost3 = 0.0; final double noorder = 0.0; final double burger = 4.99; final double pepsi = 2.00; final double chips = 0.75; scanner keyboard = new scanner(system.in); menu(burger, pepsi, chips, noorder); firstorder(chosenitem1, chosencost1); if(chosencost1 == noorder) { system.out.println("your total order comes $" + chosencost1); } else menu(burger, pepsi, chips, noorder); secondorder(chosenitem2, chosencost2); if(chosencost2 == chosencost1) { system.out.println("your total order comes $" + chosencost1); } else menu(burger, pepsi, chips, noorder); thirdorder(chosenitem3, chosencost3); system.out.println("your bill totals $" + (chosencost1 + chosencost2 + chosencost3)); } public static double thirdorder(int chosenitem3, double chosencost3) { scanner keyboard = new scanner(system.in); chosenitem3 = keyboard.nextint(); if(chosenitem3 == 1) chosencost3 = 4.99; if(chosenitem3 == 2) chosencost3 = 2.00; if(chosenitem3 == 3) chosencost3 = 0.75; if(chosenitem3 == 0) chosencost3 = 0.00; homecoming chosencost3; } public static double secondorder(int chosenitem2, double chosencost2) { scanner keyboard = new scanner(system.in); chosenitem2 = keyboard.nextint(); if(chosenitem2 == 1) chosencost2 = 4.99; if(chosenitem2 == 2) chosencost2 = 2.00; if(chosenitem2 == 3) chosencost2 = 0.75; if(chosenitem2 == 0) chosencost2 = 0.00; homecoming chosencost2; } public static double firstorder(int chosenitem1, double chosencost1) { scanner keyboard = new scanner(system.in); chosenitem1 = keyboard.nextint(); if(chosenitem1 == 1) chosencost1 = 4.99; if(chosenitem1 == 2) chosencost1 = 2.00; if(chosenitem1 == 3) chosencost1 = 0.75; if(chosenitem1 == 0) chosencost1 = 0.00; homecoming chosencost1; } public static void menu(double burger, double pepsi, double chips, double noorder) { system.out.printf( "cheeseburger" + burger, "pepsi" + pepsi, "chips" + chips, "nothing" + noorder, "make selection. press 0 quit."); } }

my issue unable user select item(s) , print out , re-ask question, @ moment getting screen cheeseburger4.99 , nil else. have no thought of have done wrong..

let's take part illustration of problem:

firstorder(chosenitem1, chosencost1); public static double firstorder(int chosenitem1, double chosencost1) { scanner keyboard = new scanner(system.in); chosenitem1 = keyboard.nextint(); if(chosenitem1 == 1) chosencost1 = 4.99; if(chosenitem1 == 2) chosencost1 = 2.00; if(chosenitem1 == 3) chosencost1 = 0.75; if(chosenitem1 == 0) chosencost1 = 0.00; homecoming chosencost1; }

the main problem is, you're returning variable chosencost1, you're not storing returend value anywhere. that, alter method phone call to:

chosencost1 = firstorder(chosenitem1, chosencost1);

the sec "problem" is, you're using unnecessary method arguments. ok omit them:

chosencost1 = firstorder(); public static double firstorder() { scanner keyboard = new scanner(system.in); int chosenitem1 = keyboard.nextint(); if(chosenitem1 == 1) homecoming 4.99; if(chosenitem1 == 2) homecoming 2.0; if(chosenitem1 == 3) homecoming 0.75; homecoming 0.0; // if not 1, 2 or 3 selected, homecoming 0 }

you should able prepare program. , please read , think @rcs comment.

java

No comments:

Post a Comment