**I am sharing the code for a third person perspective to where the problem may be lying.
I am trying to design a slanting rhombus using *. My output is a not returning slanting as the spaces before the * in each line might not be working.
**
The Desired output can be seen at the back. My output is also shown here
public class file{
public static void rhombus(int n){
for (int i=1; i<=n; i++){
//spaces
for(int j=1; j<=(n-1); j++){
System.out.print(" ");
}
//stars
for(int j=1; j<=n; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void main (String args[]){
rhombus(5);
}
}
The problem with the code is that in the first for loop, where spaces are being printed, the loop variable 'j' is only being compared to (n-1) instead of (n-i). This means that the same number of spaces are being printed on every row, resulting in a diamond shape instead of a slanting rhombus. To fix this, change the comparison in the first for loop to (n-i) so that the number of spaces printed decreases as the row number increases.
You can change it this way
public class file{
public static void rhombus(int n){
for (int i=1; i<=n; i++){
//spaces
for(int j=1; j<=(n-i); j++){
System.out.print(" ");
}
//stars
for(int j=1; j<=n; j++){
System.out.print("*");
}
System.out.println();
}
}
public static void main (String args[]){
rhombus(5);
}
}
Related
I am writing a code to indicate open and closed elements in a 2d array. An open element is represented with . and a closed element is represented with *. The first column in the array must be completely open whereas all the other columns must be closed. The output should be:
.**
.**
.**
The array is also going to vary in size as the user will determine how big the array is. I have managed to code a 2D array grid as this is not hard, however, I am trying to now use an if-statement to make the first column all open. I placed the if-statement within the nested loop:
import java.util.Arrays;
import java.util.Scanner;
public class trial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What size square grid do you want? ");
int m = sc.nextInt();
char[][] grid = new char[m][m];
int i;
int j;
for(i=0; i<grid.length; i++) {
for(j=0; j<grid[i].length; j++) {
grid[i][j] ='.';
if(grid[j].length > grid[0].length) {
System.out.println("");
}else {
System.out.print('*');
}
StdOut.print(grid[i][j]);
}
StdOut.println();
}
}
}
This, however, is giving me an output that looks like this:
*.*.*.
*.*.*.
*.*.*.
*.*.*.
I have tried placing the if-statement outside of this nested for-loop but I then get an error with my variables as they have not been defined. Any assistance as to how to fix this or make it that I can get values for my i and j variables outside of the for-loop would be greatly appreciated.
I slightly modify your code to make it fit the description.
i and j will vary when the loop proceeds, and should be accessed inside the loop only.
It seems that you want to change the content inside grid, which can be accessed outside the loop after processing.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What size square grid do you want? ");
int m = sc.nextInt();
char[][] grid = new char[m][m];
int i;
int j;
for (i = 0; i < grid.length; i++) {
for (j = 0; j < grid[i].length; j++) {
grid[i][j] = '.';
if(j > 0) {
grid[i][j] = '*';
}
System.out.print(grid[i][j]);
}
System.out.println();
}
// you cannot access i/j outside the loop scope. instead, you can access the grid
System.out.println("Loaded array:");
Arrays.stream(grid).forEach(System.out::println);
}
Just check if the current column is 0 or not
Just a minor change will work here
I am writing only the main loop below
for(i=0; i<grid.length; i++) {
for(j=0; j<grid[i].length; j++) {
if(j == 0){ //0th column make it dot(.)
grid[i][j] ='.';
}
else { //For other columns put asterisk(*)
grid[i][j] = '*';
}
}
}
Here's a different way to think about 2D arrays. They are simply a 1D array where each element is itself another 1D array (a complete "row").
With that in mind, here's a different way to initialize and print the contents of the 2D array using a mixture of enhanced and indexed for loops:
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("What size square grid do you want? ");
int m = sc.nextInt();
char[][] grid = new char[m][m];
for(char[] row : grid) {
for(int col=0; col<row.length; col++) {
row[col] = (col == 0) ? '.' : '*';
}
}
for(char[] row : grid) {
for(char c : row) {
System.out.print(c);
}
System.out.println();
}
}
Output:
What size square grid do you want?
5
.****
.****
.****
.****
.****
The following code gives me the results but I want to make it more simple so i can learn logic of different scenario
public class Pyramid {
public static void main(String[] args) {
for (int i = 0; i <= 5; i++) {
for (int x = 0; x < 5 - i; x++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
But now i want to make a triangle without inner stars and i am not getting a logic i am trying and thinking for hours i don't want full coding i just want a logic explanation so i can make it by my self it feels good
You want to print the first and last star in each row, so you need to check if k is the first or last value in your loop. Here's pseudocode:
for (int k = 0; k <= i; k++) {
[if k is first or last possible value]
[print star]
[else]
[print space]
}
Let say we want to see that shape in the output :
1
22
333
4444
666666
7777777
88888888
Code:
public class Main {
public static void main(String[] args) {
for((int i=1;i<=4;) && (int i=6; i<=8;) i++){
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}
}
First question is I don't know how to remove 55555 line to my program's output
Can somebody help me make the correct corrections with using continue;
Second question is instead of writing continue statement, can we write like this:
public class Main {
public static void main(String[] args) {
**for((int i=1;i<=4;) && (int i=6; i<=8;) i++){**
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}
}
I'm stuck :(
There's a simpler way to do what you are trying to do.
public static void main(String[] args) {
for(int i = 1; i < 9; i++){
if(i != 5) {
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}
}
Alternatively, just write two for loops, the first one running from 1 to 4, and the second one running from 6 to 8.
public static void main(String[] args) {
for(int i = 1; i < 5; i++){
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
for(int i = 6; i < 9; i++){
for(int j=1; j<=i; j++){
System.out.println(i);
System.out.println();
}
}
}
Short answer, no - you can't nest loops with a boolean && operator. However, you can use lambdas to implement this
IntStream.range(1, 8) // 1, 2, 3, 4, 5, 6, 7, 8
.filter(i -> i != 5) // remove 5
.forEachOrdered(i -> System.out.println(IntStream.range(0, i)
.mapToObj(j -> String.valueOf(i)) // repeat i i times
.collect(Collectors.joining())));
I need to create a 10x10 multiplication table. Now I have this but it has errors I would like to fix.
public static void main(String[] args) {
int n=10;
int i=1;
int j=1;
while (i <= n)
j=1;
while ( j <=n)
printf(" %3d", i * j);
j=j+1;
System.out.println();
i=i+1;
}
Well, printf() alone doesn't exist in java for starters. Second, you need to use brackets to contain loops. Try this code:
public static void main(String[] args) {
for (int i =1; i <= 10; i++){
//cycles through the first number to multiply
for (int b=1; b <=10; b++){
//cycles through second number to multiply
System.out.print(i*b + " ");
}
System.out.println();
}
}
This code worked for me, I hope it works for you!
I am trying to create some ASCII art in java using the code below.
The triangle should be as follows, this method should work for any number of inputted integer.
The triangle below is for an input of 3.
*
***
*****
This is my attempt.
public static void triangle(int n){
for (int i=n;i>0;i--){//number of lines
for(int j=i-1;j>0;j--){
System.out.print(" ");
}
for(int k=n;k>i;k--){
System.out.print("*");
}
System.out.print("\n");
}
try this code
public static void triangle(int n){
for (int i=n;i>0;i--){//number of lines
for(int j=i-1;j>0;j--){
System.out.print(" ");
}
for(int k=n;k>=i;k--){ //use k>=i
System.out.print("*");
}
System.out.print("\n");
}