public class PA4 {
public static void main(String[] args) {
for (int line = 1; line <= 6; line++) {
for (int j = 1; j <= (line - 1); j++) {
System.out.print(".");
}
System.out.print(line);
for (int j = (line + 1); j <= 6; j++) {
System.out.print(".");
}
System.out.println();
}
}
}
This code produces this output:
1.....
.2....
..3...
...4..
....5.
.....6
I understand the first loop and how it prints the dots by subtracting one from each line but I can't understand how the second loop works and how it prints the dots, or how assigning "j" the value of "line + 1" does whatever it's doing.
Maybe this is clearer; it does the same thing.
public class PA4 {
public static void main(String[] args) {
for (int line = 1; line <= 6; line++) {
for (int j = 1; j <= 6; j++) {
if (j == line)
System.out.print(line);
else
System.out.print(".");
}
System.out.println();
}
}
}
Your inner portion (two loops plus), together, always count up to 6, printing dots and one number. But they do it by counting up to line-1, then printing the number, then printing more dots, counting from just after the number up to 6. I've done it above in a single loop, but it's the same thing.
Corrected indentation
for (int line = 1; line <= 6; line++) {
for (int j = 1; j <= (line - 1); j++) {
System.out.print(".");
}
System.out.print(line);
for (int j = (line + 1); j <= 6; j++) {
System.out.print(".");
}
System.out.println();
}
replacing with a hard-coded value
// for three it would be
for (int j = 1; j <= (2); j++) { // personally I would do j < 3
System.out.print(".");
}
System.out.print(3);
for (int j = (4); j <= 6; j++) {
System.out.print(".");
}
System.out.println();
output
..3...
Related
Would it be possible to produce this layout with nested loops? I'm still new to nested to Java/loops and cannot solve this issue.
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
I'm having trouble looping through five times with the "*" character without allowing the "+" to increment.
Here is my code:
class Main {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 1; m++) {
for (int n = 0; n < m; n++) {
System.out.print("+");
}
}
System.out.println();
}
System.out.print("*****+++++");
}
}
there are could be multiple approaches to this problem, here is how I would think about it:
you need to display 9 lines of "something", so lets have top level loop:
for (int i=0; i<9; i++) {...}
now, each iteration of this loop you need to display X stars, Y equal signs, Z plus signs:
for (int i=0; i<9; i++) {
for (int j=0; j< #X# ; j++) System.out.print("*");
for (int j=0; j< #Y# ; j++) System.out.print("=");
for (int j=0; j< #Z# ; j++) System.out.print("+");
System.out.println();
}
now you need to determine rules how X,Y,Z are changed, here is the logic I came up with:
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
so, final code will look like:
public static void main(String[] args) throws InterruptedException {
int stars = 5; // initial state
int equals = 4;
int pluses = 1;
for (int i=0; i<9; i++) {
for (int j=0; j<stars; j++) System.out.print("*");
for (int j=0; j<equals; j++) System.out.print("=");
for (int j=0; j<pluses; j++) System.out.print("+");
System.out.println();
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
}
}
If you notice there is a pattern.
It prints a * starting from left to right.
It prints a + for each increasing number starting from right to left.
It prints a = instead of * if you are past mid point (first the mid point of left to right, then the mid point of top to bottom).
The logic can be applied as following,
class Main {
public static void main(String[] args) {
int max = 10;
int switchPoint = max - 1;
for(int i = 1; i <= max -1; i++) {
for(int j = 1; j <= max; j++) {
if(j > switchPoint)
System.out.print("+");
else if( i > max/2 || j > max / 2)
System.out.print("=");
else
System.out.print("*");
}
switchPoint--;
System.out.println();
}
}
}
/* Output:
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
*/
As you may notice the behavior of the "*" and "=" is different in the first five lines than in the next five lines, so you may either divide the loop in two loops or make a single one and check wether you are printing the first five lines or the last ones, that check may be done either by an if statement or in the loops conditions.
So the code will look like:
class Main {
public static void main(String[] args) {
for (int i = 1; i < 10; i++){
for (int j = 1; i <= 5 && j <= 5; j++){
System.out.print("*");
}
for (int k = 5; (i <= 5 && k > i) || (i > 5 && k > i-5); k--){
System.out.print("=");
}
for (int l = 1; l <= i; l++){
System.out.print("+");
}
System.out.println();
}
}
}
public class A {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 5 - k; m++) {
System.out.print("+");
}
System.out.println();
}
System.out.print("*****+++++");
System.out.println();
for (int s = 4; s > 0; s--) {
for (int k = 0; k < s; k++) {
System.out.print("=");
}
for (int l = 0; l < 5; l++) {
System.out.print("+");
}
for (int m = 0; m < 5 - s; m++) {
System.out.print("+");
}
System.out.println();
}
}
}
instead of getting a full triangle with 5 lines of "*" how do I replace every second line with a blank line, e.g. to look like this
5
555
55555
This is my code:
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5");
}
System.out.println();
}
you need small modification in your code and you can get what you want.
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
if(i%2==0)
System.out.print("*");
}
System.out.println();
}
check it out.
Just test for i % 2
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print(i % 2 == 0 ? "*" : " ");
}
System.out.println();
}
edit
If you want to print 5 instead or * then modify the code to use 5
Check this:
int num= 5; // 'num' denotes the number of lines
int numOfSpaces;
for(int i = 1; i <= num; i++) {
if( i%2 != 0) // checks whether the line number is odd. If odd, prints '*'
{
numOfSpaces = (num-i) / 2; // find the number of blank spaces
for(int j = 1; j <= numOfSpaces; j++) {
System.out.print(" ");
}
for(int k = 1; k <= i; k++) { // number of '*'s to be printed = current line number = i
System.out.print("*");
}
System.out.println();
}
else{
System.out.println(); // leaves every second line blank (ie.,if line number is even.)
}
}
//try this code. It may help you to achieve your output
[Check screen shot for the output of the below code][1]
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
if(i%2!=0)
{
System.out.print(" ");
}
else{
System.out.print("* ");
}
}
System.out.println();
}
The Code below is Working now.
A simple if condition will solve your problem. Just check if the
value of i is divisible by 2. If it is true then print a
blank line. Else Print * and you are done!
* will be printed on 1st, 3rd and 5th lines.
Blank Space will be printed on 2nd and 4th lines.
public class PrintPattern{
public static void main(String []args){
int lineNo=0; // New Variable for Line Number (Can Use variable 'i' also)
for(int i = 0; i < 5; i++) {
lineNo++;
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
if(lineNo%2!=0){
for(int k = 0; k <= (i*2); k++)
System.out.print('*'); // Will print '*' (or '5') Sequence for Odd Lines - 1st, 3rd, 5th Lines
System.out.println();
}
else
System.out.println();// Will print Blank Line for Even Lines - 2nd and 4th Lines
}
}
}
Try this
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5"); //Use 5 instaed of * to draw
}
System.out.println();
System.out.println(); //This one will solve your problem
}
Output will be:
5
555
55555
5555555
555555555
I think that you can just enter one more print statement inside the loop:
for(int i = 0; i < 5; i++) {
System.out.println();
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("*");
}
System.out.println();
}
Output
*
***
*****
*******
*********
I need help making a mirrored triangle like this:
* *
** **
*** ***
********
I can get each one seperatly, but I can't combine them.
public static void main(String[] args){
for( int i = 1; i <= 5; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 6; i++)
{
for(int j = 5; j > 0; j--)
{
if(i < j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
You need to track the position from both sides to be able to show * at correct location. Here is the solution:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j <= i || j > 10 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
You can do it using this code.
public class Test {
public void sizeOfTri(int triSize) { //Number of lines
int line = 1;
for(int i = 0; i < triSize; i++) { //Handles Each line
int temp = triSize * 2;
for(int j = 1; j < temp; j++) { //Handles Each space on the line
if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
System.out.print("**");
} else if(j <= line || j >= temp - line) { //For normal lines
System.out.print("*");
} else if(j == triSize) {
System.out.print(" "); //For the second to last line because it needs an extra space to make it look mirrored
} else { //For normal lines
System.out.print(" ");
}
}
System.out.println();
line++;
}
}
public static void main(String[] args) {
new Test().sizeOfTri(4);
}
}
I commented on next to if statements on which part does what. This will produce an output which looks like the below when run
* *
** **
*** ***
********
Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.
package algorithms;
public class DrawMirror {
static void initialize(String[][] array){
for (int i=0; i<MAX_X; i++){
for (int j=0; j < MAX_Y; j++){
array[i][j] = " ";
}
}
}
static void draw(String[][] array, int x, int y){
for (int i=0; i < y; i++){
array[x][i] = "*";
array[x][MAX_Y-i-1] = "*";
}
}
final static int MAX_X = 4;
final static int MAX_Y = 8;
public static void main(String[] args) {
String[][] array = new String[MAX_X][MAX_Y];
initialize(array);
for (int i=0; i < MAX_X ; i++){
draw(array,i,i+1);
}
for( int i = 0; i < MAX_X; i++ ){
for( int j = 0; j < MAX_Y; j++ ){
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
The following code is a function with variable height.
public static void printDoubleTriangle(int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < 2*height; j++) {
if(j <= i || (2*height - j - 1) <= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
I am suppose to create a pyramid and multiply each number by two until it reach the middle then divide by two such as shown in example below.
However after writing my code I am unable to have the numbers double (i*2) then once it reaches the center it divides by two until the it becomes 1
My output:
package question5;
public class Pyramid {
public static void main(String[] args) {
int x = 5;
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = x; i > 0; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++)
{
System.out.print(j+" ");
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
this is the weird output of it
This is working... You can use math.pow method.
public class test {
public static void main(String[] args) {
int x = 5;
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = x; i > 0; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 0; j <= rowCount-1; j++)
{
System.out.printf("%2d", (int)Math.pow(2, j));
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--)
{
System.out.printf("%2d", (int)Math.pow(2, j-1));
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
Right now you're outputting j, a number from 1 to 5. You want to output 2j-1 instead, which is easy: 1 << (j - 1).
You also need to right-adjust the number. Your print statement then becomes:
System.out.printf("%4d", 1 << (j - 1));
I just want to print this in simple java program using loops only.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Break the problem in small steps.
Loops for printing correct rectangle without patterns
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
System.out.print("-");
}
System.out.println();
}
This would give following output.
---------
---------
---------
---------
---------
Create pattern using if condition.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if ((j - i) % 2 == 0) { // Condition for alternate stars
System.out.print("*");
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
-*-*-*-*-
*-*-*-*-*
Restrict pattern inside the triangular shape.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if (i + j > 3 && j - i < 5) { // Equations of straight lines in triangle
if ((j - i) % 2 == 0) { // Condition for alternate stars
System.out.print("*");
} else {
System.out.print("-");
}
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Refactor for condensed conditions
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 9; j++) {
if (i + j > 3 && j - i < 5 && (j - i) % 2 == 0) {
System.out.print("*");
} else {
System.out.print("-");
}
}
System.out.println();
}
This would give following output.
----*----
---*-*---
--*-*-*--
-*-*-*-*-
*-*-*-*-*
Hope this helps.
Good luck.
try this :D
public static void main(String[] argu) {
int index=8;
for(int i=0;i<5;i++) {
for(int j=index;j>0;j--) {
System.out.print("-");
}
index--;
if(i==4) {
System.out.print("*");
}else {
System.out.print(" ");
}
}
}
public static void main(String[] args){
for(int i=5;i>=1;i--){
int otherSide = 10-i;
int printStar = -1;
for(int j=1;j<=9;j++){
if(j>=i && j<=otherSide){
printStar*=-1;
if(printStar==1) System.out.print("*");
else System.out.print("-");
}
else
System.out.print("-");
}
System.out.println("");
}
}
Just find the start index of the * and the part where it is last printed. Print stars alternatingly inside the range.