You should use System.out.print()
instead of System.out.println()
for the integer, and println()
for the boolean
.
PrintSteam.println()
ends the printing with a terminal newline '\n'
.
See this:
int z;
for (z = 10; z <= 30; z++) {
System.out.print(z);
System.out.print(' '); // Add spacing between number and boolean.
if ((z % z == 0) && (z % 1 == z))
System.out.println(true); //number is prime
else
System.out.println(false);//number is not prime
}
Here is an online ideone code demo!
But you can also print in a simpler loop:
for (int z = 10; z <= 30; z++)
System.out.printf("Is %s prime? %s\n", z, (z % z == 0) && (z % 1 == z));
However the algorithm for checking if it's a prime is incorrect, you can use a modified method from this thread:
for (int z = 10; z <= 30; z++)
System.out.printf("Is %s prime? %s\n", z,
!new String(new char[n]).matches("(..+?)\\1+"));
In source code of PrintStream.println()
:
/**
* Prints a boolean and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(boolean)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>boolean</code> to be printed
*/
public void println(boolean x) {
synchronized (this) {
print(x);
newLine();
}
}