I don't know what is the wrong with my code nested loop help me


#1

Hello , this code should work as _

when the user type sell for first time print Sorry. There are no more games in store

so the user must choose the add option first then he can sell

and when he sell the user cant sell more than what he added

i hope you understand ^^

  String add="add";
     String sell="sell";
     String exit="exit";
     String choose="";
     Scanner input=new Scanner(System.in);
	 int idgame=0;

         do{    	 
    	 System.out.println("**********************************************************************");
    	 System.out.println("*\t      Welcome to Gaming Center :)\t \t             *");
    	 System.out.println("*\t      ---------------------------\t \t             *");
    	 System.out.println("*\t      Please enter one of the following options:\t     *");
    	 System.out.println("*\t      1) add ==> this allows you to add a game to inventory  *");
    	 System.out.println("*\t      2) sell ==> this allows you to sell games to a customer*\t \t     ");
    	 System.out.println("*\t      3) exit ==> to end this program\t                     *");
    	 System.out.println("**********************************************************************");
         System.out.print("Enter your option :> ");
    	 choose=input.next();
    	 idgame=0;
    	 int numberofgames=0;
    	 if(choose.equalsIgnoreCase("add")){// if
    		 while(idgame!=-1){ // while
    			 
    			 System.out.print("Please, enter game id (-1 to end):");
    			 numberofgames++;

    			 idgame=input.nextInt();
    			 
    			 
    		 }// while
    		 
    	 }             // if
    		 
    	 else if(choose.equalsIgnoreCase("sell")){ // else if
    		 double total=0;
    		 double price=0;
    		 double discount=0;
    		int numberofgames1=numberofgames;
    		 if(numberofgames1==0){      // if 
    			 System.out.print("Sorry. There are no more games in store :(");
    		 } // if
    		 else {
    			 while(numberofgames1>0){ // while
    				 
    				 System.out.print("Please, enter game id (-1 to end):");
    				 idgame=input.nextInt();
    				 System.out.print("Please, enter the price of next game:");
    				 price=input.nextDouble();
    				 total=total+price;
    				 double  discount1=0;
    				 double totalb4=0;
    				 totalb4=total*discount1;

    				 if(numberofgames1>2){
    					 discount=0.20;
    					 totalb4=total*discount;
    					 total=total-totalb4;
    					 
    					 
    					 
    				 }// if
    				 
    				 else if (numberofgames1!=0){
    					 
    					 System.out.println("Can not sell more games. Out of stock :(");
    					 System.out.println("Total price before discount:"+total);
    					 System.out.println("Your discount is:"+totalb4);
    					 System.out.print("Total price after discount:"+total);
    				 }
    				 
    			 } // while
    		 
    		 
    		 
    		 
    	  // else if 
    	 
    	 }
    	 }
    		 }
		
		while(idgame!=0);

#2

anyone could help me ?:frowning:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.