Question

I have this code done in two methods, but it has to be done in one,...

I have this code done in two methods, but it has to be done in one, and I can not figure out how to have it work and run correctly in one method.

Chess Board:

You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it;
i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc.
The user and the computer can take any number of pieces from one lane.
The object is to not take the last piece.

import java.util.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Scanner;

public class ChessBoardTest{


public static void main(String str[]) throws IOException {
  
Scanner scan = new Scanner (System.in);

int lane5[] = {0,0,0,0,0};
int lane4[] = {0,0,0,0};
int lane3[] = {0,0,0};
int lane2[] = {0,0};
int lane1[] = {0};

int lanenum;
int piecenum;

System.out.println("Please select a lane that you want to take from");
lanenum = scan.nextInt();

System.out.println("How many do you want to take? Selecet a number 1 - 3.");
piecenum=scan.nextInt();

int removeThePieces;

if (lanenum == 5&&piecenum<=5) {
lane5 = removeThePieces(lane5, piecenum);
System.out.println(lane5);
}
else if (lanenum == 4&&piecenum<=4) {
lane4 = removeThePieces(lane4, piecenum);
System.out.println(lane4);
}
else if(lanenum == 3&&piecenum<=3) {
lane3 = removeThePieces(lane3, piecenum);
System.out.println(lane3);
}
else if(lanenum == 2&&piecenum<=2) {
lane2 = removeThePieces(lane2, piecenum);
System.out.println(lane2);
}
else if(lanenum == 1&&piecenum<=1) {
lane1 = removeThePieces(lane1, piecenum);
System.out.println(lane1);
}
}
public int[] removeThePieces(int[] arr, int number) {
int[] anotherArray = new int[arr.length - number];
for (int i = 0, k = 0; i < arr.length-number; i++) {
anotherArray[k++] = arr[i];
}
return anotherArray;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Scanner;

public class ChessBoardTest{


public static void main(String str[]) throws IOException {

int count=15;
  
Scanner scan = new Scanner(System.in);

int lane5[] = {0,0,0,0,0};
int lane4[] = {0,0,0,0};
int lane3[] = {0,0,0};
int lane2[] = {0,0};
int lane1[] = {0};

int lanenum,l5,l4,l3,l2,l1;
int piecenum,ch;

ChessBoardTest cbt=new ChessBoardTest();

l5=lane5.length;
l4=lane4.length;
l3=lane3.length;
l2=lane2.length;
l1=lane1.length;

do
{

if(count>1)
{

System.out.println("Please select a lane that you want to take from : ");
lanenum = scan.nextInt();

System.out.println("How many do you want to take? : ");
piecenum=scan.nextInt();

if(lanenum==5)
{
if(l5>=piecenum)
{
l5=l5-piecenum;
System.out.println("Pieces taken now : "+piecenum+". Pieces left in lane 5: "+l5);
count=count-piecenum;
}
else
System.out.println("Pieces left in lane 5: "+l5);
}
else if(lanenum==4)
{
if(l4>=piecenum)
{
l4=l4-piecenum;
System.out.println("Pieces taken now : "+piecenum+". Pieces left in lane 4: "+l4);
count=count-piecenum;
}
else
System.out.println("Pieces left in lane 4: "+l4);
}
else if(lanenum==3)
{
if(l3>=piecenum)
{
l3=l3-piecenum;
System.out.println("Pieces taken now : "+piecenum+". Pieces left in lane 3: "+l3);
count=count-piecenum;
}
else
System.out.println("Pieces left in lane 3: "+l3);
}
else if(lanenum==2)
{
if(l2>=piecenum)
{
l2=l2-piecenum;
System.out.println("Pieces taken now : "+piecenum+". Pieces left in lane 2: "+l2);
count=count-piecenum;
}
else
System.out.println("Pieces left in lane 2: "+l2);
}
else if(lanenum==1)
{
if(l1>=piecenum)
{
l1=l1-piecenum;
System.out.println("Pieces taken now : "+piecenum+". Pieces left in lane 1: "+l1);
count=count-piecenum;
}
else
System.out.println("Pieces left in lane 1: "+l1);
}
System.out.println("Pieces left in lanes");
System.out.println("==========================");
System.out.println("Pieces left in lane 5: "+l5+", lane 4: "+l4+", lane 3: "+l3+", lane 2: "+l2+", lane 1:"+l1);
System.out.println("In total, pieces left : "+count);

}
else
System.out.println("Last piece left.Cannot take.Please press '0' to exit");

System.out.println("Do you wish to continue -- yes-1 or no-0 : ");
ch=scan.nextInt();

}while(ch==1);

}

}

Add a comment
Know the answer?
Add Answer to:
I have this code done in two methods, but it has to be done in one,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am confused about how to code this. I have tried multiple ways of coding this...

    I am confused about how to code this. I have tried multiple ways of coding this and nothing has worked for me. If someone could help that would be great thanks. /*Chess Board: You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it; i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc. The user and the computer can take any number of pieces from one lane. The object is...

  • I have spent so much time on this and I am having trouble getting all the...

    I have spent so much time on this and I am having trouble getting all the methods to work together correctly to run the code right, and I am not sure what is wrong with it. /* You will recreate one or two versions of the logic game shown in the Algorithms movie and you will ask which one the user would like to play. Upon completion of the game, display who won and ask if they would like to...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...

    Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() {     arr = new String[10];     size = 0; } public int size() {     return size; } public void add(String value) {    ...

  • Write and submit the source code for the following program. The program will use an integer...

    Write and submit the source code for the following program. The program will use an integer array of size 10 to store the prices of smartphones. It will then determine and print the prices of the most expensive and cheapest phones. Use the following variables: int[] prices = new int[10]; // Array of smartphone prices Assignment Ask the user for the price of each smartphone (using a for loop) Sort the list of smartphones (once) from low to high price...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT