Can this code of mine be more efficient?

import java.util.Scanner;

public class Game {
	
	public static void main(String args[]){
		
		boolean gamestatus = true;
		//Boolean to control the whole game
		
		 double total = 0;
		 //Temporary variable for storing the average
		
		Banker banker = new Banker();
		//creation of the object banker;
		
		int cases = 26;
		
		int turn = 0;
		//counter variable to count the number of turns
		
		Scanner input = new Scanner( System.in );
		//creation of the scanner object
		
		Briefcase briefcase[] = new Briefcase[27];
		//an array of briefcases
		
		double amounts[] = {0,0.01,0.5,1,5,10,11,14,20,300,600,150,
							13,14,15,16,17,18,19,20,21,22,23,24,25,26,28};
		//The Amounts to be inserted in to the cases
		
		for(int i = 0; i<briefcase.length ; i++){
			briefcase[i] = new Briefcase();
			double value = amounts[i];
			briefcase[i].setAmount(value);
			briefcase[i].setFace(i);
		}//initialization and insertion of amounts in the briefcase
		
		
		
		System.out.println("\tPlease Select from the Following Cases!");
		System.out.println();
		// a simple welcome message wouldn't hurt
		
		
		for(int a = 1 ;a<briefcase.length; a++){
			System.out.print("\t "+briefcase[a].getFace()+" ");
			if(a%5==0){
				System.out.println();
			}
		}//prints out all the available briefcases
		
		System.out.print("\n\tPlease Chose your Case! : ");
		
		int nUser = input.nextInt();
		int myCase = briefcase[nUser].getFace();
		double myAmount = briefcase[nUser].getAmount();
		briefcase[nUser] = null;
		cases--;
		//Selects the user Briefcase;
		
		
		while(gamestatus==true)
		{
			if(cases==25){
				   for(int i = 5 ; i>0;i--){
					   System.out.print("\tPlease remove "+i+" Case: ");
					   int nChoice = input.nextInt();
					   briefcase[nChoice]=null;
					   cases--;
				   }
				   turn++;
				   System.out.println("\tThe bankers offer is: ");
				   for(int i = 0;i<briefcase.length;i++){
					   if(briefcase[i]==null){}
					   else{
						   total = total+briefcase[i].getAmount();
					   }
					   
				   }
				   System.out.println("\n\t"+banker.Offer(turn, total));
			 System.out.println("\t accept or reject");
				   System.out.println("Please input 1 for accept 2 for recject");		   int temp = input.nextInt();
				  if(temp ==1){gamestatus = false;}
				  else{gamestatus = true;}
				 }
			//
			
			else if(cases==20){
				   for(int i = 5 ; i>0;i--){
					   System.out.print("\tPlease remove "+i+" Case: ");
					   int nChoice = input.nextInt();
					   briefcase[nChoice]=null;
					   cases--;
				   }
				   turn++;
				   System.out.println("\tThe bankers offer is: ");
				   for(int i = 0;i<briefcase.length;i++){
					   if(briefcase[i]==null){}
					   else{
						   total = total+briefcase[i].getAmount();
					   }
					 
				   }
				   System.out.println("\n\t"+banker.Offer(turn, total));
				   System.out.println("\t accept or reject");
				   System.out.println("Please input 1 for accept 2 for recject");
				   int temp = input.nextInt();
				  if(temp ==1){gamestatus = false;}
				  else{gamestatus = true;}
				 }
			
			
		}

 }
	
}

re edited my code , so how can I make this efficient(specifically the if statements in the while loop)

import java.util.Scanner;

public class Game {

	public static void main(String args[]){

		boolean gamestatus = true;
		//Boolean to control the whole game

		double total = 0;
		//Temporary variable for storing the average

		Banker banker = new Banker();
		//creation of the object banker;

		int cases = 26;

		int turn = 0;
		//counter variable to count the number of turns

		Scanner input = new Scanner( System.in );
		//creation of the scanner object

		Briefcase briefcase[] = new Briefcase[27];
		//an array of briefcases

		double amounts[] = {0,0.01,0.5,1,5,10,11,14,20,300,600,150,
				13,14,15,16,17,18,19,20,21,22,23,24,25,26,28};
		//The Amounts to be inserted in to the cases

		for(int i = 0; i<briefcase.length ; i++){
			briefcase[i] = new Briefcase();
			double value = amounts[i];
			briefcase[i].setAmount(value);
			briefcase[i].setFace(i);
		}//initialization and insertion of amounts in the briefcase


		
		System.out.println("\tPlease Select from the Following Cases!");
		System.out.println();
		// a simple welcome message wouldn't hurt


		for(int a = 1 ;a<briefcase.length; a++){
			System.out.print("\t "+briefcase[a].getFace()+" ");
			if(a%5==0){
				System.out.println();
			}
		}//prints out all the available briefcases

		System.out.print("\n\tPlease Chose your Case! : ");

		int nUser = input.nextInt();
		int myCase = briefcase[nUser].getFace();
		double myAmount = briefcase[nUser].getAmount();
		briefcase[nUser] = null;
		cases--;
		//Selects the user Briefcase;


		while(gamestatus==true)
		{
			if(cases==25){
				for(int i = 6 ; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;
				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\t"+banker.Offer(turn, total));
				System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();
				if(temp ==1){gamestatus = false;}
				else{gamestatus = true;}
			}
			//

			else if(cases==19){

				for(int a = 1 ;a<briefcase.length; a++){

					if(briefcase[a]==null){

					}
					else{
						System.out.print("\t "+briefcase[a].getFace()+" ");
						if(a%5==0){
							System.out.println();
						}
					}

				}

				for(int i = 5 ; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;
				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\n\t"+banker.Offer(turn, total));
				System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();
				if(temp ==1)
				{
					gamestatus = false;
				}
				else{
					gamestatus = true;
				}
			}

			else if(cases==14){

				for(int a = 1 ;a<briefcase.length; a++){

					if(briefcase[a]==null){

					}
					else{
						System.out.print("\t "+briefcase[a].getFace()+" ");
						if(a%5==0){
							System.out.println();
						}
					}

				}

				for(int i = 4; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;
				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\n\t"+banker.Offer(turn, total));
				System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();
				if(temp ==1)
				{
					gamestatus = false;
				}
				else{
					gamestatus = true;
				}
			}

			else if(cases==10){

				for(int a = 1 ;a<briefcase.length; a++){

					if(briefcase[a]==null){

					}
					else{
						System.out.print("\t "+briefcase[a].getFace()+" ");
						if(a%5==0){
							System.out.println();
						}
					}

				}

				for(int i = 3; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;
				System.out.println(cases);
				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\n\t"+banker.Offer(turn, total));
				System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();
				if(temp ==1)
				{
					gamestatus = false;
				}
				else{
					gamestatus = true;
				}
			}

			else if(cases==7){

				for(int a = 1 ;a<briefcase.length; a++){

					if(briefcase[a]==null){

					}
					else{
						System.out.print("\t "+briefcase[a].getFace()+" ");
						if(a%5==0){
							System.out.println();
						}
					}

				}

				for(int i = 4 ; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;
				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){

					}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\n\t"+banker.Offer(turn, total));
				System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();

				if(temp ==1){

					gamestatus = false;
				}
				else{
					gamestatus = true;
				}
			}

			else if(cases==7){

				for(int a = 1 ;a<briefcase.length; a++){

					if(briefcase[a]==null){

					}
					else{
						System.out.print("\t "+briefcase[a].getFace()+" ");
						if(a%5==0){
							System.out.println();
						}
					}

				}

				for(int i = 2 ; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;

				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\n\t"+banker.Offer(turn, total));
				System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();
				if(temp ==1)
				{
					gamestatus = false;
				}
				else{
					gamestatus = true;
				}
			}



			else if(cases==8){

				for(int a = 1 ;a<briefcase.length; a++){

					if(briefcase[a]==null){

					}
					else{
						System.out.print("\t "+briefcase[a].getFace()+" ");
						if(a%5==0){
							System.out.println();
						}
					}

				}

				for(int i = 2; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;
				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\n\t"+banker.Offer(turn, total));
			        System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();
				if(temp ==1)
				{
					gamestatus = false;
				}
				else{
					gamestatus = true;
				}

			}

			else{

				for(int a = 1 ;a<briefcase.length; a++){

					if(briefcase[a]==null){

					}
					else{
						System.out.print("\t "+briefcase[a].getFace()+" ");
						if(a%5==0){
							System.out.println();
						}
					}

				}

				for(int i = 1; i>0;i--){
					System.out.print("\tPlease remove "+i+" Case: ");
					int nChoice = input.nextInt();
					briefcase[nChoice]=null;
					cases--;
				}
				turn++;
				System.out.print("\tThe bankers offer is: ");
				for(int i = 0;i<briefcase.length;i++){
					if(briefcase[i]==null){}
					else{
						total = total+briefcase[i].getAmount();
					}

				}
				System.out.println("\n\t"+banker.Offer(turn, total));
				System.out.println("\t 1 to accept 2 to reject");
				int temp = input.nextInt();
				if(temp ==1)
				{
					gamestatus = false;
				}
				else{
					gamestatus = true;
				}
			}

			if(myAmount>banker.Offer(turn, total)){
				System.out.println("\tYou Won!!!");
				System.out.println("\tYour Money "+myAmount);
				System.out.println("\tAnd The Bankers Offer was: "+banker.Offer(turn, total));
			}else
			{
				System.out.println("\tYou Lose!!");
				System.out.println("\tYour Money "+myAmount);
				System.out.println("\tAnd The Bankers Offer was: "+banker.Offer(turn, total));
			}

		}


	}

}

The code inside those if (cases== locks looks very repetitive. Perhaps you can put that into a single method with parameters for those things that are different each time, then just call the method from your if tests.

The code inside those if (cases== locks looks very repetitive. Perhaps you can put that into a single method with parameters for those things that are different each time, then just call the method from your if tests.

can you provide an example how?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.