java - Make a program that will ask for product code, the quantity of the product, and sums up all the prices -
i've been having hard time doing , i've been searching ideas on how code down i'm still beginner. here's source code. maybe you'll idea reading it. it's not running should though.
import javax.swing.joptionpane; public class sto { public static void main(string args[]) { char s; { int pcod=0, qua; int[] cod={101, 102, 103, 104, 105, 106, 107, 108, 109, 110}; double[] pri={1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 0.1}; double [] sal=new double[10]; string[] pro={"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"}; { if(pcod>=101||pcod<=110) { pcod=integer.parseint(joptionpane.showinputdialog(null,"enter product code:")); for(int i=0;i<10;i++) { if(pcod==cod[i]) { qua=integer.parseint(joptionpane.showinputdialog(null,"enter quantity:")); sal[i]=pri[i]*qua; joptionpane.showmessagedialog(null,"you bought:\n"+pro[i]+"......"+pri[i]+" pesos x"+qua); } } } else if(pcod<=101||pcod>=110) joptionpane.showmessagedialog(null,"invalid product code"); //some codes here make return enter product code again. }while(pcod!=00); for(int d=0;d<10;d++) sal[d]=qua; joptionpane.showmessagedialog(null,"total sales.........."+sal[d]+" pesos"); s=joptionpane.showinputdialog(null,"shop again?(y/n)").charat(0); } while(s=='y'); } }
// .. problem in next few lines for(int d=0;d<10;d++) sal[d]=qua; joptionpane.showmessagedialog(null,"total sales.........."+sal[d]+" pesos");
lacking brackets, for
loop includes next, indented code line. attribute d
defined inside for
loop statement, , goes out of focus beyond loop (cannot referenced/used in option pane).
to fix it, put {
, }
around 2 lines (one set of brackets include both lines).
that leads next problem.
int pcod=0, qua;
at point, pcod
0
qua
has no value. compiler warn, might not have been initialized
. quick fix give default value, might not fit logic of app., if exception thrown while parsing input int
value.
Comments
Post a Comment