How do I show the amount of true and falses? - java

How do I show the amount of True and falses. For example:
True - 8 is divisible by 2.
True - 42 is divisible by 2.
False - 11 is not divisible by 2.
(What I Want to it to show...)
True: 2
False: 1
Random r = new Random();
int[] num = new int[3]; // same as "= {0,0,0,0,0}
boolean gameResult = true;
for (int i = 0; i < num.length; i++) {
num[i] = r.nextInt(100) + 1;
if (num[i] % 2 == 0) {
System.out.println("TRUE - " + num[i] + " is divisible by 2.");
} else {
System.out.println("FALSE - " + num[i]
+ " is not divisible by 2.");
gameResult = false;
}
}
if (gameResult) {
System.out.println("You Won");
} else {
System.out.println("You Lost");
}
}
}

Random r = new Random();
int[] num = new int[3]; // same as "= {0,0,0,0,0}
int tCount = 0, fCount = 0;
boolean gameResult = true;
for (int i = 0; i < num.length; i++) {
num[i] = r.nextInt(100) + 1;
if (num[i] % 2 == 0) {
System.out.println("TRUE - " + num[i] + " is divisible by 2.");
tCount++;
} else {
System.out.println("FALSE - " + num[i]
+ " is not divisible by 2.");
fCount++;
gameResult = false;
}
}
System.out.println("True:" + tCount + "False:" + fCount);
if (gameResult) {
System.out.println("You Won");
} else {
System.out.println("You Lost");
}
}
}
The above code returns true and false counts. You can now use them as you want.

Here is the java code:
import java.util.Random;
public class DivisibleTest{
public static void main(String []args){
Random r = new Random();
int counterTrue=0;
int counterFalse=0;
int[] num = new int[3]; // same as "= {0,0,0,0,0}
boolean gameResult = true;
for (int i = 0; i < num.length; i++) {
num[i] = r.nextInt(100) + 1;
if (num[i] % 2 == 0) {
System.out.println("TRUE - " + num[i] + " is divisible by 2.");
counterTrue++;
counterTrue=counterTrue+1;
} else {
System.out.println("FALSE - " + num[i]
+ " is not divisible by 2.");
gameResult = false;
System.out.println(counterFalse);
counterFalse++;
counterFalse=counterFalse+1;
}
}
if (gameResult) {
System.out.println("You Won");
System.out.println("True:" +counterTrue );
System.out.println("False:" +counterTrue );
} else {
System.out.println("You Lost");
System.out.println("True:" +counterTrue );
System.out.println("False:" +counterTrue );
}
}
}
Let me know if you have any questions

Related

counter issue encountered with java

I am making a dice game, the rules are 2 dice are thrown randomly, if the sum of both dice are 7 or 11 in the first try, the user wins. if the sum is 2,3 or 12 in the first try, the user loses. if the sum is 4,5,6,8,9,10 then that sum is set as an establish point and continues to throw the dice till the sum matches the established point and the user wins, however if the establish point is 7 the user loses.
the game plays 3 times and records how many times I have won or lost. however, I am not able to get that part finished, help would be appreciated, my code is bellow.
package roll;
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2) {
int win = 0;
int loss = 0;
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
}
public void play() {
for (int x = 0; x < 3; x++) {
rolldice();
play(d1, d2);
System.out.print("\n");
}
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RandomSumGame Teet = new RandomSumGame();
RandomSumGame d1counter = new RandomSumGame();
RandomSumGame d2counter = new RandomSumGame();
Teet.play();
}
}
What part are you not able to finish? Seems like the game is running 3 times? Is it the total number of wins and loses? To fix that, you could just move "int win" and "int lose" outside the play method to make them global variables.
EDIT:
If you don't want to use global variables, you could make a method that calls itself (recursive method)
import java.util.Random;
public class RandomSumGame {
boolean start;
int d1;
int d2;
int sum = d1 + d2;
int valuepoint;
String plus = "+";
public void play(int d1, int d2, int win, int loss) {
sum = d1 + d2;
for (;;) {
if (sum == 2 || sum == 3 || sum == 12) {
start = false;
loss++;
System.out.println(" = " + sum + ";you lose");
break;
} else if (sum == 7 || sum == 11) {
start = true;
win++;
System.out.println(" = " + sum + ";you win ");
break;
}
valuepoint = sum;
if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
|| valuepoint == 10) {
System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
break;
}
}
for (;;) {
if (sum == 7 || sum == 11) {
break;
} else {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
int f1 = d1;
System.out.print("\t" + "-you rolled again " + d1 + " " + plus);
for (int x = 0; x < 1; x++) {
d2 = 1 + rand.nextInt(6);
int f2 = d2;
int loda = f1 + f2;
System.out.print(" " + d2 + " = " + loda + "\n");
}
}
sum = d1 + d2;
if (sum == valuepoint) {
start = true;
win++;
System.out.print("\t" + "you win!" + "\n");
break;
} else if (sum == 7) {
loss++;
start = false;
System.out.print("\t" + "you lose " + "\n");
break;
}
}
}
System.out.print("wins: " + win + "\n");
System.out.print("Losses:" + loss);
if (win < 2 && loss < 2) {
System.out.print("\n");
//Recursive
play(win, loss);
}
}
public void play(int win, int loss) {
rolldice();
play(d1, d2, win, loss);
}
public void rolldice() {
Random rand = new Random();
for (int i = 0; i < 1; i++) {
d1 = 1 + rand.nextInt(6);
System.out.print("You rolled " + d1 + " " + plus);
}
for (int i = 0; i < 1; i++) {
d2 = 1 + rand.nextInt(6);
System.out.print(" " + d2 + " ");
}
}
public static void main(String[] args) {
RandomSumGame test = new RandomSumGame();
test.play(0,0);
}
}

How should I avoid java.lang.ArrayIndexOutOfBoundsException when trying to call a possible void element of a 2D array

I am trying to make a tic tac toe game and am new to java. I am getting outofbounds at the first if statement within the method isHorizontalWin and I am assuming it would happen within isVerticalWin as well. Correct me if I am wrong but I believe it is happening because part of gameboard has no value so the if statement is outofbounds however I am unsure in how to fix this. A side note: the formatting got messed up a little bit when pasting the code so I had to make some of the JOptionPanes go onto multiple lines to fit. Thanks!
import javax.swing.JOptionPane;
public class TicTacToe
{
public static void main(String[] args)
{
char gameboard[][] = new char[3][3];
printCurrentState(gameboard);
int Turn = 0;
int gameon=0;
while(gameon<9)
{
if(Turn==0)
{
JOptionPane.showMessageDialog(null, "Player 1's turn aka X");
}
else
{
JOptionPane.showMessageDialog(null, "Player 2's turn aka O");
}
boolean proceed=true;
String yo =JOptionPane.showInputDialog("Enter the row and column"+ "one"+
"after another. Example-for row 1 and comlumn 2 type \"12\"");
if(yo.length()!=2)
{
JOptionPane.showMessageDialog(null, "You did not put the row"+ "and column one after another- Try again");
proceed=false;
}
if(proceed==true) {
String aa= yo.charAt(0)+"";
int x = Integer.parseInt(aa)-1;
String ba= yo.charAt(1)+"";
int y = Integer.parseInt(ba)-1;
if((x < 0 || x > 2)||(y<0||y>2))
{
JOptionPane.showMessageDialog(null, "Please enter a row and"+
"column that both at least 1 and no bigger than 3. Try again!");
yo =JOptionPane.showInputDialog("Enter the row and column one"+
"after another. Example-for row 1 and comlumn 2 type \"12\"");
aa= yo.charAt(0)+"";
x = Integer.parseInt(aa)-1;
ba= yo.charAt(1)+"";
y = Integer.parseInt(ba)-1;
}
if(gameboard[x][y] == 'X' || gameboard[x][y] == 'O')
{
JOptionPane.showMessageDialog(null, "That is already taken"+
"by an "+ gameboard[x][y]+". Go again!");
yo =JOptionPane.showInputDialog("Enter the row and column"+
"one after another. Example-for row 1 and comlumn 2 type \"12\"");
aa= yo.charAt(0)+"";
x = Integer.parseInt(aa)-1;
ba= yo.charAt(1)+"";
y = Integer.parseInt(ba)-1;
}
if(Turn== 0)
{
gameboard[x][y] = 'X';
printCurrentState(gameboard);
isHorizontalWin(gameboard);
isVerticalWin(gameboard);
isDiagnolWin(gameboard);
Turn++;
}
else if(Turn == 1)
{
gameboard[x][y] = 'O';
printCurrentState(gameboard);
isHorizontalWin(gameboard);
isVerticalWin(gameboard);
isDiagnolWin(gameboard);
Turn--;
}
gameon++;
}
}
if(isHorizontalWin(gameboard)==false&&isVerticalWin(gameboard)==false&&isDiagnolWin(gameboard)==false)
{
JOptionPane.showMessageDialog(null, "There was no winner this game ");
}
}
public static void printCurrentState(char gameboard[][])
{
System.out.println(" COLUMN");
System.out.println(" 1 2 3");
System.out.print(gameboard[0][0] + " | " + gameboard[0][1] + " | " + gameboard[0][2] +" 1 R"+ "\n"
+ "--|---|--\n" +
gameboard[1][0] + " | " + gameboard[1][1] + " | " + gameboard[1][2] +" 2 O"+ "\n"
+ "--|---|--\n" +
gameboard[2][0] + " | " + gameboard[2][1] + " | " + gameboard[2][2] +" 3 W "+"\n");
System.out.println();
}
public static boolean isHorizontalWin(char[][] gameboard)
{
int tally[][]= new int[3][3];
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; r++)
{
if(gameboard[r][c]=='O')
{
tally[r][c]=10;
}
else if(gameboard[r][c]=='X')
{
tally[r][c]=1;
}
else
{
tally[r][c]=0;
}
}
}
int c=0;
for(int m = 0; m < 3; m++)
{
c++;
int cool = 0;
for(int n = 0; n < 3; n++)
{
cool += tally[m][n];
if(cool == 30)
{
JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a horizontal win in row " +c);
System.exit(0);
return true;
}
else if(cool==3)
{
JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a horizontal win in row " + c);
System.exit(0);
return true;
}
}
}
return false;
}
public static boolean isVerticalWin(char[][] gameboard)
{
int tally[][]= new int[3][3];
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; r++)
{
if(gameboard[r][c]=='O')
{
tally[r][c]=10;
}
else if(gameboard[r][c]=='X')
{
tally[r][c]=1;
}
else
{
tally[r][c]=0;
}
}
}
int r=0;
for(int m = 0; m < 3; m++)
{
r++;
int cool = 0;
for(int n = 0; n < 3; n++)
{
cool += tally[n][m];
if(cool == 30)
{
JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a vertical win in column " +r);
System.exit(0);
return true;
}
else if(cool==3)
{
JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a vertical win in column " + r);
System.exit(0);
return true;
}
}
}
return false;
}
public static boolean isDiagnolWin(char[][] gameboard)
{
if((gameboard[0][0]=='O'&&gameboard[1][1]=='O'&&gameboard[2][2]=='O')||(gameboard[0][2]=='O'&&gameboard[1][1]=='O'&&gameboard[3][1]=='O'))
{
JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a diagonal win" );
System.exit(0);
return true;
}
if((gameboard[0][0]=='X'&&gameboard[1][1]=='X'&&gameboard[2][2]=='X')||(gameboard[0][2]=='X'&&gameboard[1][1]=='X'&&gameboard[2][0]=='X'))
{
JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a diagonal win" );
System.exit(0);
return true;
}
return false;
}
}
In isHorizontalWin, you probably forgot to change the r++ into c++ in the second for loop.
This results into r being incremented too many times.
So when r reaches a number that is higher than the amount of entries, the index (r) is out of bounds.
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; r++)
{
Needs to be:
for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; c++)
{

Correctly Printing For Loops in Java

I have been working on this program for a few hours and the code is working correctly but I can't seem to get it to print out correctly, it should just be printing once for each value, such as:
number: 6
dividers: 2 3 6 1
prime: is not prime
Output
Can anyone help? Screenshot is attached. Thanks!
public static void main(String[] args) {
Random randomNums = new Random();
int count;
for (int i = 1; i <= 37; i++) {
count = randomNums.nextInt(100) + 1;
System.out.println("number " + count);
for (int b = 1; b<=count; b++) {
if (count % b == 0) {
System.out.println("dividers " + b);
}
}
for (int a = 2; a< count; a++) {
if (count % a == 0) {
System.out.println("is not prime");
}
if (count % a != 0) {
System.out.println("is prime");
}
}
}
}
}
try to write this code :
public static void main(String[] args) {
Random randomNums = new Random();
int count;
for (int i = 1; i <= 37; i++) {
count = randomNums.nextInt(100) + 1;
System.out.println("number " + count);
String dividers = "";
for (int b = 1; b<=count; b++) {
if (count % b == 0) {
dividers += b.toString() +" ";
}
}
// control the print beside loop
System.out.println("dividers " + dividers);
// add the control for whether prime
bool prime = true;
for (int a = 2; a< count; a++) {
if (count % a == 0) {
System.out.println("prime : is not prime");
// add the control for skip loop
prime = false;
break;
}
}
if(prime){
System.out.println("prime : is prime");
}
}
}
}
based upon your logic, I am guessing that if you decide that a number is not a prime then that is the final result,
so
boolean isPrime = true;
String dividers = "";
for (int a = 2; a< count; a++) {
if (count % a == 0) {
isPrime = false;
dividers += a+" ";
}
}
if (isPrime) {
System.out.println ("is Prime");
} else {
System.out.println ("dividers "+dividers);
System.out.println ("is not Prime");
}

Role 2 dice 100000 times, write the amount of times each sums and rolled and make a graph - Java

Here is my code so far
package chapter3Codes;
public class TwoDice {
public static void main(String[] args) {
Dice a = new Dice();
Dice b = new Dice();
int sum = 0;
int is2=0; int is3=0; int is4=0; int is5=0; int is6=0;
int is7=0; int is8=0; int is9=0; int is10=0; int is11=0; int is12=0;
for (int i=1; i<= 100000; i++){
a.roll();
a.getFaceValue();
b.roll();
b.getFaceValue();
sum = (a.getFaceValue()+ b.getFaceValue());
if(sum == 2){
is2++;
}else{
if(sum == 3){
is3++;
}else{
if(sum == 4){
is4++;
}else{
if(sum == 5){
is5++;
}else{
if(sum == 6){
is6++;
}else{
if(sum == 7){
is7++;
}else{
if(sum == 8){
is8++;
}else{
if(sum == 9){
is9++;
}else{
if(sum == 10){
is10++;
}else{
if(sum == 11){
is11++;
}else{
if(sum == 12){
is12++;
}
}
}
}
}
}
}
}
}
}
}
}
System.out.println("The sum is 2 "+ is2 + " many times");
System.out.println("The sum is 3 "+ is3 + " many times");
System.out.println("The sum is 4 "+ is4 + " many times");
System.out.println("The sum is 5 "+ is5 + " many times");
System.out.println("The sum is 6 "+ is6 + " many times");
System.out.println("The sum is 7 "+ is7 + " many times");
System.out.println("The sum is 8 "+ is8 + " many times");
System.out.println("The sum is 9 "+ is9 + " many times");
System.out.println("The sum is 10 "+ is10 + " many times");
System.out.println("The sum is 11 "+ is11 + " many times");
System.out.println("The sum is 12 "+ is12 + " many times");
for(int i = 0; i <= is2;i++){
System.out.println("*");
}
for(int i = 0; i <= is3;i++){
System.out.println("*");
}
for(int i = 0; i <= is4;i++){
System.out.println("*");
}
for(int i = 0; i <= is6;i++){
System.out.println("*");
}
for(int i = 0; i <= is7;i++){
System.out.println("*");
}
for(int i = 0; i <= is8;i++){
System.out.println("*");
}
for(int i = 0; i <= is9;i++){
System.out.println("*");
}
for(int i = 0; i <= is10;i++){
System.out.println("*");
}
for(int i = 0; i <= is11;i++){
System.out.println("*");
}
for(int i = 0; i <= is12;i++){
System.out.println("*");
}
}
}
How do I fix my code so that every time is2 is incremented a star prints.
For example if is2 was incremented 3 times and is3 was incremented 4 times it would show like this
***
****
Since you haven't learned arrays yet, I'll just show you how to print the *s for your twos on one line. You need System.out.print many times, but only one System.out.println to finish the line. Something like,
for(int i = 0; i <= is2;i++){
System.out.print("*"); // <-- on one line.
}
System.out.println(); // <-- end the line.
If you use a hashmap things can be compacted too much as below:
public class TwoDice {
public static void main(String[] args) {
Dice a = new Dice();
Dice b = new Dice();
Map<Integer,Integer> sumMap = new Hashmap<Integer,Integer>();
int sum = 0;
for (int i=1; i<= 100000; i++){
a.roll();
a.getFaceValue();
b.roll();
b.getFaceValue();
//
sum = (a.getFaceValue()+
b.getFaceValue());
if(sumMap.get(sum)==null){
sumMap.put(sum,1);
}else{
sumMap.put(sum,sumMap.get(sum)+1);
}
}
for(int i=1; i<=12; i++)
{
int size = sumMap.get(i)==null? 0 : sumMap.get(i);
System.out.println("The sum is "+i+" "+ size +
" many times");
}
for(int i=1; i<=12; i++)
{
int size = sumMap.get(i)==null? 0 : sumMap.get(i);
for(int j=1; j<=size; j++){
System.out.print("*");
}
System.out.println("");
}
}
}

Array count logic error

Made some edits to the code to try and figure out why my X's [-1] are not being included in finding my average for that row. That is throwing of my averages. Any idea why It is not counting my -1's?
output[expected]:
USER INPUT: 3
O O O
X X X
X X X
TOTAL OPENNESS OF [I][J] = 1
TOTAL OPENNESS OF [I][J+1] = 2
TOTAL OPENNESS OF [I][J+2] = 1
TOTAL SUM AVERAGE FOR THAT ROW = 1.3
HOWEVER..FOR ROW 2 AND ROW 3
TOTAL SUM AVERAGE FOR THOSE ROWS = 0
WHICH IS INCORRECT IT SHOULD = -1
public static void openfactor(char[][] mazeValue, int n){
for(int i = 1; i<=n; i++)
{
double rowAvg=0;
double totalRowAvg=0;
for(int j=1;j<=n;j++)
{
int count=0;
int totalOpeness=0;
int totalRowOpeness = 0;
//double rowAvg=0;
if(mazeValue[i][j]=='X'){
System.out.println("tHIS IS AN X FOR : [" + i + "]" +"[" + j + "] IS -1 ");
count = -1;
}
else
{
//YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
if( j-1>=1)
{
if(mazeValue[i][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1 && j-1>=1)
{
if(mazeValue[i-1][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1)
{
if(mazeValue[i-1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n)
{
if(mazeValue[i][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n && i+1<=n)
{
if(mazeValue[i+1][j+1]=='O')
count++;
}
if (i+1<=n)
{
if(mazeValue[i+1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j-1>=1 && i+1<=n)
{
if(mazeValue[i+1][j-1]=='O')
count++;
}
if(i-1>=1 && j+1<=n)
{
if(mazeValue[i-1][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
totalOpeness = totalOpeness +count;
System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "] IS " +totalOpeness);
totalRowOpeness = totalRowOpeness + totalOpeness;
//}//eND OF iF CONDITION\
}
rowAvg = (double)totalRowOpeness/(double)n;
System.out.println("ROW AVERAGE: "+rowAvg);
totalRowAvg = totalRowAvg + rowAvg;
System.out.println("SUM ROW AVERAGE: "+totalRowAvg);
}
System.out.println("TOTAL SUM ROW AVERAGE: " +totalRowAvg);
}
}
public static void printMaze(char mazeValue[][]) {
System.out.println("MAZE");
for (int i = 1; i < mazeValue.length; i++) {
for (int j = 1; j < mazeValue[i].length; j++) {
System.out.printf("%5c", mazeValue[i][j]);
}
System.out.printf("\n");
}
}
public static void main(String[] args) {
// TODO code application logic here
Scanner kbd = new Scanner(System.in);
System.out.println("ENTER A SINGLE INTEGER: ");
int n = kbd.nextInt();
char[][] mazeValue = new char[n + 1][n + 1];
System.out.println("ENTER A PATH: ");
for (int i = 0; i < mazeValue.length; i++) {
for (int j = 0; j < mazeValue[i].length; j++) {
if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
mazeValue[i][j] = 'X';
else {
mazeValue[i][j] = kbd.next().charAt(0);
}
}
}
printMaze(mazeValue);
horizontalPath(mazeValue, n);
System.out.println(" ");
verticalPath(mazeValue,n);
System.out.println(" ");
openfactor(mazeValue, n);
}
}
I do not completely understand what u want to accomplished but I am going to to assume you want to find repeated values, do this using some search algorithm below is an example of a binary search. Hope it helps.
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + (middle + 1) + ".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list.\n");
}
}
Here's the complete code for your request. you need to reorder your if statements a little bit your logic was right:
and here is the output :
MAZE
O O X
O O O
X X O
TOTAL OPENESS FOR : [0][0] IS 3
TOTAL OPENESS FOR : [0][1] IS 4
THERE IS AN X HERE FOR : [0][2]
Average of O's in this row is : 66.66667%
TOTAL OPENESS FOR : [1][0] IS 3
TOTAL OPENESS FOR : [1][1] IS 5
TOTAL OPENESS FOR : [1][2] IS 3
Average of O's in this row is : 100.0%
THERE IS AN X HERE FOR : [2][0]
THERE IS AN X HERE FOR : [2][1]
TOTAL OPENESS FOR : [2][2] IS 2
Average of O's in this row is : 33.333336%
here's the code:
import java.util.Scanner;
public class sof {
public static boolean IsOutOfBound(int i, int j, int n)
{
if (i-1<1 || j-1<1 || i+1>n || j+1>n)
return true;
else
return false;
}
public static void openfactor(char[][] mazeValue, int n)
{
for(int i = 0; i<n; i++)
{
int TotalCounts=0;
for(int j=0;j<n;j++)
{
int count=0;
if(mazeValue[i][j]=='X'){
System.out.println("THERE IS AN X HERE FOR : [" + i + "]" +"[" + j + "] ");
//TotalCounts--;
}
else
{
//YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
if( j-1>=0)
{
if(mazeValue[i][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=0 && j-1>=0)
{
if(mazeValue[i-1][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=0)
{
if(mazeValue[i-1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<n)
{
if(mazeValue[i][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<n && i+1<n)
{
if(mazeValue[i+1][j+1]=='O')
count++;
}
if (i+1<n)
{
if(mazeValue[i+1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j-1>=0 && i+1<n)
{
if(mazeValue[i+1][j-1]=='O')
count++;
}
if(i-1>=0 && j+1<n)
{
if(mazeValue[j+1][i-1]=='O')
count++;
}
// System.out.println("cout: "+count);
//totalOpeness = totalOpeness +count;
System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "] IS " + count);
TotalCounts++;
}//END OF else CONDITION
}//End of J loop
float Average = ((float)TotalCounts/(float)n) * 100;
System.out.println("Average of O's in this row is : " + Average+ "%");
}//End of I loop
}
public static void printMaze(char mazeValue[][],int n) {
System.out.println("MAZE");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%5c", mazeValue[i][j]);
}
System.out.printf("\n");
}
}
public static void main(String[] args) {
// TODO code application logic here
// TODO code application logic here
Scanner kbd = new Scanner(System.in);
System.out.println("ENTER A SINGLE INTEGER: ");
int n = kbd.nextInt();
char[][] mazeValue = new char[n][n];
System.out.println("ENTER A PATH: ");
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
//if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
// mazeValue[i][j] = 'X';
// else {
mazeValue[i][j] = kbd.next().charAt(0);
// }
}
}
printMaze(mazeValue,n);
openfactor(mazeValue, n);
}
}

Categories