Java (not so)Simple formatting - java

Hey guys so my homework was to:
1)Prompt the user to enter the number of cells C
2)Declare an integer array cell[] with C elements
3)Prompt the user to enter the number of time steps N
3)Prompt the user to enter the index of cells that contain 1(enter negative index to finish)
4)Run the cellular automaton for N time steps, using the rules defined above
5)On each time step, display the cells, printing a ‘#’ if the cell contains a 1,
a space if the cell contains a 0
A desired output would be:
Enter number of cells (<= 80): 10
Enter number of time steps: 10
Enter the index of occupied cells (negative index to end): 4 6 -1
0123456789
# #
####
## #
### ##
# ####
### #
# # ##
######
# #
# ##
# ###
My code so far is this:
import java.util.Scanner;
class P7{
public static void main(String[] args){
int i, N, C, index;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of cells(<=80):");
C = in.nextInt();
int[] cell = new int[C];
System.out.println("Enter number of time steps:");
N = in.nextInt();
System.out.println("Enter the index of occupied cells(-num to end):");
for(i = 0; i < C; i++){
cell[i] = 0;
}
while(true){
index = in.nextInt();
if(index < 0){
break;
}
cell[index] = 1;
}
for(i = 0; i < N; i++)
updateCells(cell);
displayCells(cell);
}
public static void updateCells(int array[]){
int i;
int[] temp = new int[array.length];
for(i = 1; i < array.length - 1; i++){
if(array[i]==1 && array[i-1]==1 && array[i+1]==1)
temp[i] = 0;
else if(array[i]==1 && array[i-1]==1 && array[i+1]==0)
temp[i] = 1;
else if(array[i]==0 && array[i-1]==1 && array[i+1]==1)
temp[i] = 1;
else if(array[i]==0 && array[i-1]==1 && array[i+1]==0)
temp[i] = 0;
else if(array[i]==1 && array[i-1]==0 && array[i+1]==1)
temp[i] = 1;
else if(array[i]==1 && array[i-1]==0 && array[i+1]==0)
temp[i] = 1;
else if(array[i]==0 && array[i-1]==0 && array[i+1]==1)
temp[i] = 1;
else if(array[i]==0 && array[i-1]==0 && array[i+1]==0)
temp[i] = 0;
}
for(i = 0; i < array.length; i++){
array[i] = temp[i];
}
}
public static void displayCells(int data[]){
int i;
for(i=0;i < data.length; i++){
if(data[i] == 1)
System.out.println("#");
else if(data[i] == 0)
System.out.println(" ");
}
}
}
This is my current output:
Enter number of cells(<=80): 10
Enter number of time steps: 3
Enter the index of occupied cells(-num to end):
1
2
3
4
-1
#
#
#
#
Any and all help will be appreciated :D

I actually don't understand what you trying to do, try this if that might would help. If you have the question in written or any pdf, then I can probably tell. Best
import java.util.Scanner;
public class P7{
public static void main(String[] args){
int i, N, C, index;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of cells(<=80):");
C = in.nextInt();
int[] cell = new int[C];
System.out.println("Enter number of time steps:");
N = in.nextInt();
System.out.println("Enter the index of occupied cells(-num to end):");
for(i = 0; i < N; i++){
cell[i] = 0;
}
while(true){
index = in.nextInt();
if(index < 0){
break;
}
cell[index] = 1;
}
for(i = 0; i < N; i++)
updateCells(cell);
displayCells(cell);
}
public static void updateCells(int cell[]){
int i;
int[] temp = new int[cell.length];
for(i = 1; i < cell.length - 1; i++){
if(cell[i]==1 && cell[i-1]==1 && cell[i+1]==1)
temp[i] = 0;
else if(cell[i]==1 && cell[i-1]==1 && cell[i+1]==0)
temp[i] = 1;
else if(cell[i]==0 && cell[i-1]==1 && cell[i+1]==1)
temp[i] = 1;
else if(cell[i]==0 && cell[i-1]==1 && cell[i+1]==0)
temp[i] = 0;
else if(cell[i]==1 && cell[i-1]==0 && cell[i+1]==1)
temp[i] = 1;
else if(cell[i]==1 && cell[i-1]==0 && cell[i+1]==0)
temp[i] = 1;
else if(cell[i]==0 && cell[i-1]==0 && cell[i+1]==1)
temp[i] = 1;
else if(cell[i]==0 && cell[i-1]==0 && cell[i+1]==0)
temp[i] = 0;
}
for(i = 0; i < cell.length; i++){
cell[i] = temp[i];
}
}
public static void displayCells(int cell[]){
int i;
for(i=0;i < cell.length; i++){
if(cell[i] == 1)
System.out.print("#");
else if(cell[i] == 0)
System.out.print(" ");
}
}
}`enter code here`

Related

How can I gets histogram using arrays in java?

Take input from user in an array in form of numeric and show histogram Where i'm wrong The code is given in java the user can enter 5 input in number and histogram should show in any form with no constraints
package p21;
import java.util.Scanner;
public class P21 {
public static void main(String[] args) {
{
int count[] = new int[10]; // count array will keep elements of element
// in particular range;
int elements[]; // for example 27 15 34 22 11 11 19
{ // in above input there is count[0]=0;
for (int i = 0; i < elements.length; i++) // count[1]=4 and count[2]=2 and count[3]=1;
{
if (elements[i] >= 0 && elements[i] < 50) {
if (elements[i] < 10) {
count[0]++;}
else if (elements[i] >= 10 && elements[i] < 20) {
count[1]++;}
else if (elements[i] >= 20 && elements[i] < 30) {
count[2]++;}
else if (elements[i] >= 30 && elements[i] < 40) {
count[3]++;}
else {
count[4]++;
}}
else if (elements[i] >= 50 && elements[i] <= 100) {
if (elements[i] < 60) {
count[5]++;}
else if (elements[i] >= 60 && elements[i] < 70) {
count[6]++;}
else if (elements[i] >= 70 && elements[i] < 80) {
count[7]++;}
else if (elements[i] >= 80 && elements[i] < 90) {
count[8]++;}
else {
count[9]++;
}}}}
{
System.out.println("Histogram of the elements:");
for (int i = 0; i < count.length; i++) // this loop will print line
{
for (int j = 0; j < count[i]; j++) // this will print elements element(*)
{ // at each line.
System.out.print("* ");
}
if (count[i] != 0) // if line does'nt contain zero
System.out.println(""); // then if will change the row;
}
}
}
/*
in above code if count[i]=zero means if there is elements
element in particular range say [0-9] then it will
elementst jump on next line;
*/
{
{
Histogram hg = new Histogram();
System.out.println("Enter the elements of Elements want in a Histogram:");
Scanner sc = new Scanner(System.in);
int noOfElements = sc.nextInt();
int histogramElements[] = new int[noOfElements];
System.out.println("Enter the Elements for Histogram:");
for (int i = 0; i < noOfElements; i++) {
histogramElements[i] = sc.nextInt();
}
hg.showHistogram(histogramElements);
}
For your binning approach..... since you only care about groups of 10. Divide the input number by 10. Bound the divided result then use the result to index your counter array.

I am trying to assign a number to each character

import java.util.Scanner;
public class Recursion
{
//variables to hold string values
public static String s1 = new String(new char[10]);
public static String s2 = new String(new char[10]);
public static String s3 = new String(new char[11]);
public static String charSet = new String(new char[11]);
//variables to hold number values
public static int numberOne;
public static int numberTwo;
public static int numberThree;
public static int maxCharCount;
public static int[] numberSet = new int[10];
//function which generates a number
public static void checkForEquality()
{
numberOne = numberTwo = numberThree = 0;
int i;
int j;
for (i = 0; i < s1.length(); i++)
{
for (j = 0; j < maxCharCount; j++)
{
if (s1.charAt(i) == charSet.charAt(j))
{
if (i == 0 && numberSet[j] == 0)
return;
//generate the number
numberOne = (numberOne * 10) + numberSet[j];
}
}
}
for (i = 0; i < s2.length(); i++)
{
for (j = 0; j < maxCharCount; j++)
{
if (s2.charAt(i) == charSet.charAt(j))
{
if (i == 0 && numberSet[j] == 0)
return;
//generate number
numberTwo = (numberTwo * 10) + numberSet[j];
}
}
}
for (i = 0; i < s3.length(); i++)
{
for (j = 0; j < maxCharCount; j++)
{
if (s3.charAt(i) == charSet.charAt(j))
{
if (i == 0 && numberSet[j] == 0)
return;
//generate the number
numberThree = (numberThree * 10) + numberSet[j];
}
}
}
}
public static void display(){
if (numberOne + numberTwo == numberThree) {
//display the output
int i=0;
System.out.println();
System.out.print(" Summation Puzzle solved. ");
System.out.print("n");
System.out.print(s1);
System.out.print("<==>");
System.out.print(numberOne);
System.out.print("n");
System.out.print(s2);
System.out.print("<==>");
System.out.print(numberTwo);
System.out.print("n");
System.out.print(s3);
System.out.print("<==>");
System.out.print(numberThree);
System.out.print("n");
//loop to show the result
for (i = 0; i < maxCharCount; i++)
{
System.out.println(charSet.charAt(i));
System.out.print("<==>");
System.out.print(numberSet[i]);
System.out.print("n");
}
System.exit(0);
}
}
//recursive function which will call itself
public static void Combinations(int indexCounter, int[] availableSet)
{
int i;
if (indexCounter != 0)
{
for (i = 0; i < 10; i++)
{
numberSet[indexCounter] = i;
if (availableSet[i] == 1)
{
availableSet[i] = 0;
Combinations(indexCounter + 1, availableSet);
availableSet[i] = 1;
}
}
}
if (indexCounter == maxCharCount)
checkForEquality();
}
public static void createCharSet()
{
int i;
int setIndex;
int present;
int j;
setIndex = 0;
for (i = 0; i < s1.length(); i++)
{
present = 0;
for (j = 0; j < setIndex; j++)
{
if (s1.charAt(i) == charSet.charAt(j))
{
present = 1;
}
}
if (present == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, s1.charAt(i));
}
}
for (i = 0; i < s2.length(); i++)
{
present = 0;
for (j = 0; j < setIndex; j++)
{
if (s2.charAt(i) == charSet.charAt(j))
{
present = 1;
}
}
if (present == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, s2.charAt(i));
}
}
for (i = 0; i < s3.length(); i++)
{
present = 0;
for (j = 0; j < setIndex; j++)
{
if (s3.charAt(i) == charSet.charAt(j))
{
present = 1;
}
}
if (present == 0)
{
charSet = StringFunctions.changeCharacter(charSet, setIndex++, s3.charAt(i));
}
}
maxCharCount = setIndex;
}
public static void calculateSummation()
{
int loop;
if (maxCharCount > 10)
{
System.out.print("Please check the input again");
return;
}
else
{
int[] avaliableSet = new int[10];
for (loop = 0; loop < 10; loop++)
{
avaliableSet[loop] = 1;
}
Combinations(0, avaliableSet);
}
}
//main method
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first String :");
s1 = scan.next();
System.out.print("Enter the second String :");
s2 = scan.next();
System.out.print("Enter the thirsd String :");
s3 = scan.next();
createCharSet();
System.out.print(" result of your 3 three strings = ");
System.out.print(charSet);
calculateSummation();
checkForEquality();
display();
}
}
Every time I run the program it just assigns 0 to each value. I want to be able to assign a value from 1-10 to each non numeric character.
can someone help me out.?
it should look something like this
Sample output
First off, you may find debugging easier if you properly format your code. I remember you posting last night where you experienced similar issues resulting from syntax. You may find that your code is easier to read if you format it like this:
//function which generates a number
public static void checkForEquality(){
numberOne = numberTwo = numberThree = 0;
int i;
int j;
for (i = 0; i < s1.length(); i++){
for (j = 0; j < maxCharCount; j++){
if (s1.charAt(i) == charSet.charAt(j)){
if (i == 0 && numberSet[j] == 0)
return;
//generate the number
numberOne = (numberOne * 10) + numberSet[j];
}
}
}
for (i = 0; i < s2.length(); i++){
for (j = 0; j < maxCharCount; j++){
if (s2.charAt(i) == charSet.charAt(j)){
if (i == 0 && numberSet[j] == 0)
return;
//generate number
numberTwo = (numberTwo * 10) + numberSet[j];
}
}
}
Take a look at just this portion of your code and see if it still properly follows your algorithm. You may find an error which you could've located yourself with properly formatted code. If you take a look at a previous question of yours (which I answered) you'll find a helpful link that guides you to writing better code.
Also, I didn't try to compile this in my own IDE, but you should take a look at Eclipse. A lot of the small errors you're getting could be located automatically by Eclipse.

Printing A Diamond Shape In Java, Based On User Input

I have a problem with a program I am trying to write. A user inputs a positive odd integer, otherwise the program prompts the user until they do. When they do, the program prints a diamond shape corresponding to the user input.
I have this piece so far that prints the left hand diagonal of such a figure, but cannot figure out how to print the rest of it. Here is the code:
import java.util.Scanner;
public class DrawingProgram {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the drawing program:");
System.out.println("Please Input a Positive Odd Integer:");
char userAnswer;
int userInput;
userInput = keyboard.nextInt();
if (userInput%2 == 0){
System.out.println("That is not a Positive Odd Integer!");
}
else if (userInput < 0){
System.out.println("That is not a Positive Odd Integer");
}
else if (userInput%2 == 1){
for (int row = 1; row<= userInput; row++){
for (int col = 1; col<= userInput; col++ ){
if (row+col==userInput-1 )
System.out.print( "*");
else
System.out.print( " ");
}
System.out.print("\n");
}
}
}
}
A user inputs a positive odd integer, otherwise the program prompts
the user until they do
You need to scan in a loop
do
{
userInput = keyboard.nextInt();
if (userInput % 2 == 0)
System.out.println("That is not an Odd Integer!");
if(userInput < 0)
System.out.println("That is not a Positive Odd Integer");
} while(userInput < 0 || userInput %2 == 0);
Now you can remove that validation else if (userInput%2 == 1){
Now the first thing I realize when checking at your loop is if (row+col==userInput-1 || ) which won't compile as you have a comparison operator with nothing following.
Note that you can replace System.out.print("\n"); with System.out.println("") but that's not really important...
Now replace your loop condition so that they start as 0
for (int row = 0; row <= userInput; row++){
for (int col = 0; col <= userInput; col++ ){
Now since you want a Diamond, you need to have 4 diagonal, so 2 loops (one for top and bottom)...
for (int i = 1; i < userInput; i += 2)//Draw the top of the diamond
{
for (int j = 0; j < userInput - 1 - i / 2; j++)//Output correct number of spaces before
{
System.out.print(" ");
}
for (int j = 0; j < i; j++)//Output correct number of asterix
{
System.out.print("*");
}
System.out.print("\n");//Skip to next line
}
for (int i = userInput; i > 0; i -= 2)//Draw the bottom of the diamond
{
for (int j = 0; j < userInput -1 - i / 2; j++)
{
System.out.print(" ");
}
for (int j = 0; j < i; j++)
{
System.out.print("*");
}
System.out.print("\n");
}
So the final code would look like this
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the drawing program:");
System.out.println("Please Input a Positive Odd Integer:");
char userAnswer;
int userInput;
do
{
userInput = keyboard.nextInt();
if (userInput % 2 == 0)
{
System.out.println("That is not an Odd Integer!");
}
if (userInput < 0)
{
System.out.println("That is not a Positive Odd Integer");
}
} while (userInput < 0 || userInput % 2 == 0);
for (int i = 1; i < userInput; i += 2) //This is the number of iterations needed to print the top of diamond (from 1 to userInput by step of two for example with 5 = {1, 3, 5} so 3 rows.
{
for (int j = 0; j < userInput - 1 - i / 2; j++)//write correct number of spaces before, example with 5 = j < 5 - 1 -i / 2, so it would first print 4 spaces before, with 1 less untill it reach 0
{
System.out.print(" ");//write a space
}
for (int j = 0; j < i; j++)
{
System.out.print("*");//write an asterix
}
System.out.println("");
}
// Same logic apply here but backward as it is bottom of diamond
for (int i = userInput; i > 0; i -= 2)
{
for (int j = 0; j < userInput -1 - i / 2; j++)
{
System.out.print(" ");
}
for (int j = 0; j < i; j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}

(ADDED) Noughts and Crosses game. While loop

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
visBoard[i][j] = "[ ]";
board[i][j] = 0;
check[i][j] = false;
}
}for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
System.out.print(visBoard[i][j]);
}System.out.print("\n");
}
//Getting Names
System.out.println("Player 1 - Enter your name");
play1 = sc.nextLine();
System.out.println("Player 2 - Enter your name");
play2 = sc.nextLine();
//
moves = 0;
symbol = " X ";
do{
do{
//Get Coords
System.out.println("X Coordinate");
xcoord = sc.nextInt() -1;
System.out.println("Y Coordinate");
ycoord = sc.nextInt() -1;
if(check[xcoord][ycoord] == true){
System.out.println("Not a valid move!");
}
}while(check[xcoord][ycoord] == true);
//Making move
check[xcoord][ycoord] = true;
visBoard[xcoord][ycoord] = symbol;
if(symbol.equals(" X ")){
board[xcoord][ycoord] = 1;
}else if(symbol.equals(" O ")){
board[xcoord][ycoord] = 5;
}else{
System.out.println("You've messed up James");
}
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
System.out.print(visBoard[i][j]);
}System.out.print("\n");
}
//Check if game has won
//columns
total = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
total = total + board[j][i];
}if(total == 15 || total == 3){
gamewon = true;
}
}total = 0;
//rows
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
total = total + board[i][j];
}if(total == 15 || total == 3){
gamewon = true;
}
}total = 0;
//diagonals
for(int i = 0; i < 3; i++){
total = total + board[i][i];
}if(total == 15 || total == 3){
gamewon = true;
}total = 0;
diag = 2;
for(int i = 0; i < 3; i++){
total = total + board[i][diag];
diag--;
}if(total == 15 || total == 3){
gamewon = true;
}
moves++;
if(gamewon == false){
if(moves == 9){
System.out.println("Game has been drawn! No one wins!");
}else{
mod = moves % 2;
if(mod == 0){
symbol = " X ";
}else{
symbol = " O ";
}
}
}
}while(gamewon == false || moves != 9);
if(gamewon == true){
if(symbol.equals(" X ")){
System.out.println("Winner is "+play1);
}else{
System.out.println("Winner is "+play2);
}
}else{
System.out.println("Game is drawn");
}
}
}
This is a further question from a previous question I had. This game won't end until moves reaches 9 even though the while loop should stop once someone has won. The boolean will turn true, but it will continue to loop.
How do I fix this issue with keeping the while condition, and possibly without using breaks?
You need an and not an or
while(gamewon == false && moves != 9);
Reading that to yourself it says while there is no winner and we are not at move 9. However it's usually better form to code your loops to check that you haven't exceeded a bound rather than you have hit the bound exactly, and it is also nicer to simply test the boolean directly so the following is more stylish:
while(!gamewon && moves < 9);
while(gamewon == false || moves != 9)....
This tells the loop to execute while game isnt won, or moves are not 9. For it to end, BOTH conditions need to change, the game needs to be ended AND moves needs to be 9.
Change your || operator to &&. This way the game will keep going while the game is not won AND the moves is not 9. It seems a bit strange but if you can follow the logic, you'll see that you need the AND operator.
Therefore, you're looking for:
while(gamewon == false && moves != 9)

How to replace an integer element in a 2d array with a string type, using a Random

I have this table (2d array) and I'm using a Random utility from numbers 0-4 to select a row and column to replace a number with the letter "P" and I have everything but I get this.
- Type mismatch: cannot convert from String
to int
- Type mismatch: cannot convert from
Random to int
- Type mismatch: cannot convert from
Random to int
Meanwhile this question How to change value of array element in 2D arrays? says you can just do
someArray[row][column] = "x";
Here is what I have (I assume my problem is with using the random)
import java.util.Random;
public class Server{
private int number;
private boolean pennyLanded;
public Server()
{
int n = 0;
number = n;
pennyLanded = false;
}
public boolean pennyLanded()
{
return pennyLanded;
}
public void setPennyLanded()
{
pennyLanded = true;
}
public int getNumber()
{
return number;
}
public String toString()
{
if (pennyLanded)
return "P";
else
return "" + number;
}
public static int[][] tableMaker(){
int[][] table = new int[5][5];
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
if(i==2 && j==2){
table[i][j] =3;
}
else if(i==0 || i==4){
table[i][j] = 1;
}
else if(j==4 || j==0){
table[i][j] = 1;
}
else if((i==1 || i==3) && (j>0 || j<4)){
table[i][j] = 2;
}
else if((i==2 && j==1) || (i==2 && j==3)){
table[i][j] = 2;
}
}
}
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
System.out.print(table[i][j] + " ");
}
System.out.println();
}
return table;
}
public static int[][] tossPenny(){
Random row = new Random();
Random column = new Random();
int[][] table = Server.tableMaker();
for(int i=0; i<5; i++){
table[row.nextInt(4)][column.nextInt(4)] = -1;
}
return table;
}
}
The table is printed in a Client class
public class Client{
public static void main(String[] args){
System.out.println(Server.tableMaker());
}
}
I Don't know how simple of a fix this is, and I have searched I just have a particular problem
The table is printed as so
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
And I would like something like this say the random puts out 1 and 2
1 1 1 1 1
1 2 P 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
You have to use row.nextInt(int max) instead of row, where the generated random integer n is 0 <= n < max. The parameter of new Random() only defines a seed, so I suggest you not use a constant number there unless you want your random algorithm to be deterministic.
Furthermore you mixes up the types a bit. tableMaker() should be String[][] in order to write Strings into it and tossPenny() should be void since it doesn't return anything.
import java.util.Random;
public class Server {
public static String[][] tableMaker() {
String[][] table = new String[5][5];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table.length; j++) {
if (i == 2 && j == 2) {
table[i][j] = "" + 3; // Converting int 3 to String "3"
} else if (i == 0 || i == 4) {
table[i][j] = "" + 1;
} else if (j == 4 || j == 0) {
table[i][j] = "" + 1;
} else if ((i == 1 || i == 3) && (j > 0 || j < 4)) {
table[i][j] = "" + 2;
} else if ((i == 2 && j == 1) || (i == 2 && j == 3)) {
table[i][j] = "" + 2;
}
}
}
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table.length; j++) {
System.out.print(table[i][j] + " ");
}
System.out.println();
}
return table;
}
public static void tossPenny(int a) {
Random row = new Random();
Random column = new Random();
String Penny = "P";
String[][] table = Server.tableMaker();
for (int i = 0; i < 5; i++) {
table[row.nextInt(5)][column.nextInt(5)] = Penny;
}
}
}
Was able to figure it out with help of a IRL friend and this avoids the memory store errors as seen before, Server is below
import java.util.Random;
public class Server{
private static int[][] table;
public static int[][] tableMaker(){
table = new int[5][5];
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
if(i==2 && j==2){
table[i][j] =3;
}
else if(i==0 || i==4){
table[i][j] = 1;
}
else if(j==4 || j==0){
table[i][j] = 1;
}
else if((i==1 || i==3) && (j>0 || j<4)){
table[i][j] = 2;
}
else if((i==2 && j==1) || (i==2 && j==3)){
table[i][j] = 2;
}
}
}
return table;
}
public static void printTable(){
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
if(table[i][j] == -1) System.out.print("P ");
else System.out.print(table[i][j] + " ");
}
System.out.println();
}
}
public static int[][] tossPenny(){
Random row = new Random();
Random column = new Random();
for(int i=0; i<5; i++){
table[row.nextInt(4)][column.nextInt(4)] = -1;
}
return table;
}
}
Client is here
import java.util.Scanner;
public class Client{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Server.tableMaker();
Server.printTable();
System.out.println();
Server.tossPenny();
Server.printTable();
input.close();
}
}
Thanks a lot everyone especially #DavidWallace You helped me get around most of my problems and I was able to fix everything using the private static int[][] table;

Categories