Print pyramid of * in Java - java

I'm wondering if you could help me out. I'm trying to write a nested for loop in java that displays a number pyramid triangle that looks like
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#
This is what I have so far:
class Triagle {
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
printTriagle(12);//I want to set the triangle to be height of 12
}
}
My result is not equal to the expected output:
___________*#
__________*_*#
_________*_*_*#
________*_*_*_*#
_______*_*_*_*_*#
______*_*_*_*_*_*#
_____*_*_*_*_*_*_*#
____*_*_*_*_*_*_*_*#
___*_*_*_*_*_*_*_*_*#
__*_*_*_*_*_*_*_*_*_*#
_*_*_*_*_*_*_*_*_*_*_*#
*_*_*_*_*_*_*_*_*_*_*_*#

I have updated your code and added comments so that you can understand. Refer to the code below:
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print("_");
}
String s = "_";
if (i + 1 >= n) // check if it is the last line
s = "*"; // change to print * instead of _
for (int j = 0; j <= i; j++) {
// printing stars
if (j == i)
System.out.print("*#"); // check if the last print of the line
else if (j == 0)
System.out.print("*" + s); // check if the first print of the line
else
System.out.print(s + s);
}
System.out.println();
}
}
Result:
___________*#
__________*_*#
_________*___*#
________*_____*#
_______*_______*#
______*_________*#
_____*___________*#
____*_____________*#
___*_______________*#
__*_________________*#
_*___________________*#
***********************#

Try this
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
if(i == (n-1)){
System.out.print("**");
}
else{
System.out.print((j == 0 || j == i) ? "* " : " ");
}
}
System.out.println();
}
}

Your issue is here:
for (int j=0; j<=i; j++){
// printing stars
System.out.print("* ");
}
Here, it prints a star for each number between 0 and i, but it only should print a star if it is exactly 0 or i.
Try something like this:
for (int j=0; j<=i; j++){
if ( i == n ) {
System.out.print("* ");
} else {
System.out.print(j == 0 || j == i ? "* " : " ");
}
}
EDIT: You may still have to adapt your code to get the bottom line printed correctly, in case that line has to be all stars

This is what you need to do:
public static void printTriagle(int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2*n; j++) {
if(i == n-1) {
System.out.print((j != 2*n-1) ? "*" : "#");
}
else {
if(i+j == n-1) {
if(i == 0) {
System.out.print("*#");
break;
}
else {
System.out.print("*");
}
}
else if(j-i == n-1) {
System.out.print("*#"); break;
}
else {
System.out.print("_");
}
}
}
System.out.println();
}

Related

How to find common numbers or an anomaly in a 2D array and cause it to trigger something else

I have a 2D array (a matrix of 10x10) with values ranging from 0 to -5.
I want a method to be triggered when there is a sequence of a value found within the array.
For example, there is a sequence of two negative 2. I want it to trigger an event/method that will give a bonus score of 4. This should happen only when there are two -2's and not if there is just one -2.
I tried achieving something like that but I cant figure out how to tell the program to only trigger when 'n' number of a value is found within the matrix.
public class Test {
static int board[][] = new int[10][10];
public static void Test() {
int i, j;
board[0][0] = -1;
board[0][1] = -1;
board[1][1] = -2;
board[1][2] = -2;
board[1][3] = -2;
board[1][4] = -2;
for (i = 0; i < board.length; i++) {
System.out.println("");
for (j = 0; j < board.length; j++) {
//board[i][j] = 0;
System.out.print(board[i][j]);
}
}
System.out.println();
}
public static void scanBoard() {
int i, j;
for (i = 0; i < board.length; i++) {
for (j = 0; j < board.length; j++) {
if (board[i][j] == -1) {
System.out.println("Hello");
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(); //scans for
}
}
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 3 && j > 3) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && j > 5) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 3 < size)) {
if (matrix[i][j + 1] == -2 && matrix[i][j + 2] == -2 && matrix[i][j + 3] == -2) {
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
I am not sure if this is the result you wished to see according to your request. I hope this helps you. And I did some changes in your code so it will be easier to read (In my opinion lol).
public class Main {
static final int size = 10;
static int[][] matrix = new int[size][size];
public static void main(String[] args) {
System.out.println("The first matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 9 && (j == 0 || j == 1)) {
matrix[i][j] = -2; //-2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
System.out.println("\nThe second matrix.\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == 8 && (j == 5 || j == 6)) {
matrix[i][j] = 2; //changed it from -2 to 2
} else {
matrix[i][j] = 1;
}
System.out.print(matrix[i][j]);
}
System.out.println();
}
scanBoard();
}
static void scanBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == -2 && (j + 1 < size)) {
if (matrix[i][j + 1] == -2) {
//You can remove the '.toUpperCase()', it's just my personal preference
System.out.println("\nThere you go, a special effect!".toUpperCase());
}
}
}
}
}
}
From what I understood from the problem statement and comments, you want your scanBoard to behave like this:
public static void scanBoard(int value, int frequency) {
int i, j;
if (value <= 0 && value >= -5 && frequency >= 2 && frequency <= 10) {
for (i = 0; i < board.length; i++) {
int rowFrequency = 0;
for (j = 1; j < board.length; j++) {
if (board[i][j] == value && board[i][j - 1] == value) {
rowFrequency++;
} else {
rowFrequency = 0;
}
if (rowFrequency + 1 >= frequency) {
System.out.println("Hello");
}
}
}
}
}
public static void main(String[] args) {
Test(); //prints out whole array
scanBoard(-2, 4); //prints Hello once
scanBoard(-2, 3); //prints Hello twice
scanBoard(-2, 3); //prints Hello thrice
}

How to Flip the triangle?

How to flip this triangle?
So i was making aritmethic sequance triangle. It was upside down.
How do I turn it 180 degree?
for example:
1=1
1+2=3
1+2+3=6
etc...
my code:
package javaapplication4;
public class NewClass5 {
public static void main(String[] args) {
int i=5,a;
for(int j=i; j>=1; j--) {
for(a=1; a<=i; a++)
System.out.print(a +" + ");
int n = 0;
for(a = 1; a<=i; a++) {
n = n + a;
}
System.out.print(" = "+ n);
System.out.println();
i--;
}
}
}
You can do it for any n, by getting input from the user
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
int add = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
add += j;
}
System.out.println(add);
}
I think you just need to change the sequence of loop from descending to ascending, rest of the things should be the same:
for (int i = 1; i <= 5; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
sum += j;
}
System.out.println(sum);
}
When I run this, I'm able to see expected output:
src : $ java NewClass5
1=1
1+2=3
1+2+3=6
1+2+3+4=10
1+2+3+4+5=15

Printing message only once after iterating through the first column of a 2d array

import java.util.Scanner;
public class Grade{
public static void main(String[] args) {
String students[][] = new String[2][4];
Scanner input = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.print("\n\nStudent 00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[i][j] = input.nextLine();
}
}
System.out.print("\n\tRegistered Students : \n\n");
System.out.print("\tCODE\tFULL NAME\tMATHS\tFRENCH\n\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
System.out.print("\t" + students[i][j] + " ");
}
System.out.println();
}
//Ask for student code.
System.out.print("\n\tStudent Code : ");
String search= input.nextLine();
boolean found = false;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
// found= true;
if (search.equals(students[i][0])) {
found = true;
System.out.print("\n\tStudent Code Found!\n");
String math = students[i][2];
String french = students[i][3];
Double m = new Double(math);
double mathConv = m.doubleValue();
Double f = new Double(french);
double frenchConv = f.doubleValue();
double average = (mathConv + frenchConv) / 2;
System.out.print("\n\tMoyenne de l'etudiant : " + average + "\n");
if (average <= 40) {
System.out.print("\n\tFailure!\n");
} else if (average > 40 && average < 70) {
System.out.print("\n\tReprisal!\n");
} else {
System.out.print("\n\tSuccess!\n");
}
}
else if (!search.equals(students[i][0])) {
found = false;
System.out.print("\n\tCode incorrect!\n");
}
}
}
}
}
I need to display only one message after entering the code etudiant but istead it displays the message 4 times. The loop should only iterate through the first column of each line and compares it to what the user entered.
When you iterate for the search you don't need the second loop to iterate over the attributes, because you don't use it
System.out.print("\n\tStudent Code : ");
String search = input.nextLine();
boolean found = false;
for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 4; j++) { //you don't need this loop
Change this:
for (int j = 0; j < 4; j++)
to this:
for (int j = 0; j < 1; j++)
as we want to run that loop only once.
Alternatively, you can remove the j loop altogether.
it's recommended you use the length argument for those arrays, that way if you ever change the size, the loop won't break, in this case you can just get the first element of the array, and then iterate through that. So intead of
for (int i = 0; i < 2; i++) {
System.out.print("\n\nStudent 00" + (i + 1) + "\n\n");
for (int j = 0; j < 4; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[i][j] = input.nextLine();
}
}
you can say
System.out.print("\n\nStudent 001" + "\n\n");
for (int j = 0; j < students[0].length; j++) {
if (j == 0) {
System.out.print("\n\tStudent Code : ");
} else if (j == 1) {
System.out.print("\n\tName : ");
} else if (j == 2) {
System.out.print("\n\tMaths Grade : ");
} else if (j == 3) {
System.out.print("\n\tFrench Grade : ");
} else {
System.out.print("\n\tNonexistent field!\n");
}
students[0][j] = input.nextLine();
}

Creating a double mirrored triangle

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();
}
}

How to print this using loops only

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.

Categories

Resources