Adding multiple terms to the For Statement - java

My question is why output is 0 - 4? But not 0 - 9?
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
also next code doesn't compile?
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
Could you explain why?

int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
The above prints 0 to 4 since x < 5 && y < 10 can only be true when both inequalities are true. So as soon as x == 5, the loop exits.
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
The above doesn't compile because you have already defined x as an int. The first part of the loop tries to redefine it as a long

int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.print(y + " ");
}
The condition is not true when x == 5, x <5 && y <10, your condition is that x is less than 5, and y is less than 10, so the condition is not true when x == 5, if you change the condition to || will output 0-9
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.print(x + " ");
}
This does not compile because the x value has been defined, regardless of the type.The reason for this kind of error is because Java itself is not allowed, "Java designers think that doing so will cause program confusion"

Related

Calculate dots on cuboid's grid

I have an issue with my math skills here. So I need to calculate the positions (x, y, z) of a specific amount of dots per aixs. For example take this image
final double gapX = lengthX / dotsPerXAxis;
final double gapY = lengthY / dotsPerYAxis;
final double gapZ = lengthZ / dotsPerZAxis;
for (BlockFace blockFace : BlockHandler.DIRECT_RELATIVES) {
final DecimalVector3D minCorner = new DecimalVector3D(
blockFace.getModX(),
blockFace.getModY(),
blockFace.getModZ()
);
for (int x = 0; x < dotsPerXAxis || x == 0; x++) {
for (int y = 0; y < dotsPerYAxis; y++) {
for (int z = 0; z < dotsPerZAxis; z++) {
}
}
}
My question now is: how can I iterate over all the dots except those that are inside the cuboid and calculate their position and put them in an ImmutableList?
You need to treat point if at least one coordinate of it is zero or dotsPerZAxis.
So set flags - if X-coordinate lies on face, if Y-coordinate lies on face. If both flags are not set - get only the first and the last Z-coordinates, otherwise walk through all Z-coordinates.
Unchecked Java:
for (int x = 0; x < dotsPerXAxis; x++) {
bool atX = (x == 0) || (x == dotsPerXAxis - 1);
for (int y = 0; y < dotsPerYAxis; y++) {
bool atY = (y == 0) || (y == dotsPerYAxis - 1);
int zstep = (atX || atY)? 1: dotsPerZAxis - 1;
for (int z = 0; z < dotsPerZAxis; z+=zstep) {
treat(x,y,z)
}
}
}
Ideone Python working code as proof-of-concept gives n^3 - (n-2)^3 points (26 surface points for n=3, 56 for n=4, 98 for n=5)
Based on the help of MBo I figured this out
for (BlockFace blockFace : BlockHandler.DIRECT_RELATIVES) {
for (int x = 0; x < dotsPerXAxis || x == 0 || x == dotsPerXAxis - 1; x++) {
for (int y = 0; y < dotsPerXAxis || y == 0 || y == dotsPerYAxis - 1; y++) {
for (int z = 0; z < dotsPerXAxis || z == 0;
z += (y == 0 || y == dotsPerYAxis - 1) || (x == 0 || x == dotsPerXAxis - 1) ? 1 :
dotsPerZAxis - 1) {
results.add(new DecimalVector3D(
x,
y,
z
));
}
}
}
}

Breakpoint doesn't work

My Code :
class MixFor5 {
public static void main (String [] args) {
int x = 0;
int y = 30;
for (int outer = 0; outer < 3; outer++) {
for (int inner = 4; inner > 1; inner--) {
x = x + 3;
y = y - 2;
if (x == 6) {
break; // *Useless break;*
}
x = x + 3;
}
y = y - 2;
}
System.out.println(x + " " + y);
}
}
My output:
54 6
Can someone explain to me. Why when I remove break; my output data don't change at all.
You are never fulfilling the if(x==6)
lets take a look at the first loop:
int x = 0;
//....
x = x + 3; // x = 3;
if( x == 6 ) //false
break;
x = x + 3; // x = 6
now the second loop
x = x + 3 // x = 9
if( x == 6 ) //false x = 9
break;
x = x + 3; //x = 12
so you never are equal to 6 when comparing.

Why is my output messed up for printing out 2-d array?

public class chessboardUsingArray {
private Car[][] myBoard = new Car[8][8];
public chessboardUsingArray() {
// make sure at the end of this constructor, myBoard is 8x8 and each car
// in it is set to NULL
for (int x = 0; x < myBoard.length; x++) {
for (int y = 0; y < myBoard[x].length; y++) {
myBoard[x][y] = null;
// System.out.print(myBoard[x][y] + " ");
}
// System.out.println();
}
}
public void printBoard() {
for (int x = 0; x < myBoard.length; x++) {
for (int y = 0; y < myBoard[x].length; y++) {
Car defaultCar = myBoard[x][y];
if (defaultCar != null) {
System.out.println(" " + defaultCar.getMake() + " ");
} else {
System.out.print(" X ");
}
}
System.out.println();
}
}
public boolean placeCar(int y, int x, Car myCar) {
if (x >= 0 && x < 8 && y >= 0 && y < 8) {
if (myBoard[x][y] == null) {
myBoard[x][y] = myCar;
}
}
return false;
}
The place car function should place a car in the specified coordinates and return everything else with an X as specified. When I try to put a car in location (2, 3) this happens... The car is placed correctly as you can see with an H but do some of the other locations disappear?
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X H
X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
the five X 's to the right of H disappear as do three others below it. idk why its not being shown correctly. any ideas? thanks
You are using println for car make which results in X's after that to be shown on the next line

Android Minesweeper algorithm

I'm developing a game "Minesweeper" for Andoid on Java and I have a problem when opening the cells. How to make sure that I click on the cell opened adjacent empty cells? (How it is done in Miner for Windows).
Introduction: I have an array which i receive from bluetooth socket stream. Array like this :
1 9 1 0
1 1 1 0
0 0 0 0
0 0 0 0
9-is a mine
0-is blank cell
1-nearest mines count
After that i calculate game field
array = Model.getGameField();
int size = array.length;
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
{
((TableRow) table.getChildAt(i)).getChildAt(j).setTag(array[i][j] + "");
}
OnClick function :
if (iWantToSetFlag == 0)
{
tmpBtn = ((Button) v);
if (!(tmpBtn.getTag().equals("9")))
{
OpenButtons(tmpBtn.getId() / 10, tmpBtn.getId() % 10);
recreateTable();
}
else
startLose();
}
else
{
if (((Button) v).getText().equals("M"))
((Button) v).setText("");
else
((Button) v).setText("M");
}
I have a function
private void OpenButtons(int x, int y)
{
array[x][y] = -1;
for (int k = -1; k < 2; k++)
{
for (int k1 = 1; k1 >= -1; k1--)
{
if (x + k >= 0 && x + k < array.length && y - k1 >= 0 && y - k1 < array[x + k].length)
if (array[x + k][y - k1] == 0)
OpenButtons(x + k, y - k1);
}
}
}
which recursively open cells but i have a StackOverFlow error. Help please.
You should be calling your recursion with changed parameters:
if (array[x + k][y - k1] == 0)
OpenButtons(x + k, y - k1);
And of course, as has been mentioned in the comments of the question, you should check for the array bounds yourself instead of just ignoring the exceptions:
if (x + k >= 0 && x + k < array.length &&
y - k1 >= 0 && y - k1 < array[x + k].length) { ...
put before your other if-clause will only check fields which actually exist. Ridding you of your malicious empty try-catch.
Since the recursive algorithm will still cause a StackOverflowException for large fields, an iterative algorithm might be better suited here.
private void OpenButtons(int x, int y) {
Queue<Point> toOpen = new LinkedBlockingQueue<>();
toOpen.add(new Point(x, y));
array[x][y] = -1;
while (!toOpen.isEmpty()) {
Point p = toOpen.poll();
x = p.x;
y = p.y;
for (int k = -1; k < 2; k++) {
for (int k1 = 1; k1 >= -1; k1--) {
if (x + k >= 0 && x + k < array.length && y - k1 >= 0
&& y - k1 < array[x + k].length)
if (array[x + k][y - k1] == 0) {
array[x + k][y - k1] = -1;
toOpen.add(new Point(x + k, y - k1));
}
}
}
}
}

Threes, finding 23 sets of x y z values satisfy the condition x^3+y^3=1+z^3

Now I have completed the finding 23 sets of x y z values satisfy the condition x^3+y^3=1+z^3 & x
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long x = 1; setsFound < 23; x++) {
for (long z = x + 1; z<x*x; z++) {
long y = (long) Math.pow((1 + z*z*z - x*x*x), 1f/3f);
if (x * x * x == 1 + z * z * z - y * y *y && x<y && y<z) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
But the code I have is very inefficient, can anyone help me to fix this please?
Here is a working code:
int setsFound = 0;
System.out.println("The first 23 sets ordered by increasing x.");
for (long z = 1; setsFound < 23; z++) {
for (long y = z - 1; y > 0; y--) {
long x = (long) Math.pow((1 + z * z * z - y * y * y), 1f/3f);
if(y <= x) break;
if (x * x * x == 1 + z * z * z - y * y *y) {
setsFound++;
System.out.println("X: " + x + ", Y: " + y + ", Z: " + z);
}
}
}
Couple of problems in the old one:
1/3 == 0 (because it's integer division) //use 1f/3f
x and z are swapped - you want z > x, not the other way around
(long)Math.pow(4*4*4, 1.0/3) == (long)3.9999999999999996 == 3 // use round
If you start the other way, with X < Y < Z by incrementing Y and Z up to a limit, you can gain some efficiencies. Once Z^3 > X^3 + Y^3 + 1, you can skip to the next Y value due to the concavity of the cubic function.
This implementation in C# works pretty fast on a laptop:
UInt64 setsFound = 0;
UInt64 xlim = 10000;
UInt64 ylim = 1000000;
UInt64 zlim = 10000000;
//int ctr = 0;
Console.WriteLine("The first 23 sets ordered by increasing x.");
Parallel.For(1, (long)xlim, new ParallelOptions { MaxDegreeOfParallelism = 4 }, i =>
//for (UInt64 i = 0; i < xlim; i++)
{
UInt64 x = (UInt64)i;
UInt64 xCu = x * x * x;
int zFails = 0;
for (UInt64 y = x + 1; y < ylim; y++)
{
UInt64 yCu = y * y * y;
zFails = 0;
for (UInt64 z = y + 1; z < zlim & zFails < 1; z++)
{
UInt64 zCu = z * z * z;
if (xCu + yCu - zCu == 1)
{
Console.WriteLine(String.Format("{0}: {1}^3 + {2}^3 - {3}^3 = 1", setsFound, x, y, z));
setsFound++;
}
else if (zCu > xCu + yCu - 1)
{
zFails++;
}
}
}
}
);
Obviously you can take out the parallelization. Also, here are the first 19 elements in that set (computer is still running, I'll try to post the last 4 later):
output http://desmond.yfrog.com/Himg640/scaled.php?tn=0&server=640&filename=8qzi.png&xsize=640&ysize=640

Categories