import java.util.*;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
sc.close();
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int num_i = arr[i];
for (int j = i + 1; j <= 98; j++) {
int num_j = arr[j];
for (int k = j + 1; k <= 99; k++) {
int num_k = arr[k];
if (num_i + num_j + num_k == Integer.parseInt(input))
System.out.printf("(%d, %d, %d)", num_i, num_j, num_k);
}
}
}
}
}
When I get a number input, I want to make a code that represents this number as the sum of three numbers.
The code is complete, but there are several combinations. I want to print out only one combination. How can I edit it?
First, some important suggestions:
Do not parse input inside the nested loop as it will hit the performance. Do it once outside the nested loops.
Do not close Sacnner for System.in as it also closes System.in and there is no way to open it again without restarting JVM. It means that if it is being used in some other part of your application, your application will crash.
Always follow Java naming conventions e.g. you could name numJ instead of num_j.
Coming back to your problem, there are many ways to solve it and I have listed below just a couple of them:
Use break <<label>> to exit the nested loops:
import java.util.Scanner;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int num = Integer.parseInt(input);
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
start: for (int i = 0; i <= 97; i++) {
int numI = arr[i];
for (int j = i + 1; j <= 98; j++) {
int numJ = arr[j];
for (int k = j + 1; k <= 99; k++) {
int numK = arr[k];
if (numI + numJ + numK == num) {
System.out.printf("(%d, %d, %d)", numI, numJ, numK);
break start;
}
}
}
}
}
}
A sample run:
Enter a number : 123
You entered: 123
(1, 22, 100)
Put the logic in a method and return:
import java.util.Scanner;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int num = Integer.parseInt(input);
printFirstCombination(num);
}
static void printFirstCombination(int num) {
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int numI = arr[i];
for (int j = i + 1; j <= 98; j++) {
int numJ = arr[j];
for (int k = j + 1; k <= 99; k++) {
int numK = arr[k];
if (numI + numJ + numK == num) {
System.out.printf("(%d, %d, %d)", numI, numJ, numK);
return;
}
}
}
}
}
}
You can create a seperate function for that and after you find a combination, print it and return there and then to the main function. In case you didn't find a combination you return 1 which can be handled in the main function,
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int res = printCombination(input);
if(res == 1) {
// Do something
}
sc.close();
}
private static int printCombination(String input) {
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int num_i = arr[i];
for (int j = i + 1; j <= 98; j++) {
int num_j = arr[j];
for (int k = j + 1; k <= 99; k++) {
int num_k = arr[k];
if (num_i + num_j + num_k == Integer.parseInt(input)) {
System.out.printf("(%d, %d, %d)", num_i, num_j, num_k);
return 0;
}
}
}
}
return 1;
}
}
This is the output of the following code:
1
23
345
4567
But I want the output to be in the opposite direction like this.
Not sure how to achieve that.
1
23
345
4567
Code:
import java.util.Scanner;
public class numPattern1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 1;
while (i <= n) {
int space = n - i + 1;
while (space <= n) {
System.out.print(" ");
space++;
}
int j = 1;
int p = i;
while (j <= i) {
System.out.print(p);
p++;
j++;
}
System.out.println();
i++;
}
}
}
For each line, there are two spaces times the length of the longest line minus the current line number. Then, you count from the current line number one plus the current line number values. Like,
int len = (n / 2) + 1;
for (int i = 0; i < len; i++) {
for (int j = 0; j < 2 * (len - i - 1); j++) {
System.out.print(' ');
}
for (int j = 0; j <= i; j++) {
System.out.print(j + i + 1);
}
System.out.println();
}
Outputs (as requested)
1
23
345
4567
Here it is:
public class numPattern1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 1;
int k = 0;
while (i <= n) {
int space = 2*n - i - k ;
k++;
int l=0;
while (l <= space) {
System.out.print(" ");
l++;
}
int j = 1;
int p = i;
while (j <= i) {
System.out.print(p);
p++;
j++;
}
System.out.println();
i++;
}
}
for one of my assignments I am required to print out stars in the shape of a diamond. When trying to print odd numbers, everything works out fine. However, when trying to print even numbered diamonds, the spacing in the middle lines is off, and a diamond is missing. Could someone please point out what I am doing wrong, and provide a possible fix?
public void printStarsDiamond(int d)
{
for (int i = 1; i < d; i = i + 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = d; i > 0; i = i - 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
Desired result, if the user enters 6 (d = 6)
If you notice your code, the second for loop is starting from
i = d in
for (int i = d; i > 0; i = i - 2)
but your first loop is not ending at d, you are doing i < d there which will stop before d, so it will go from 1 to 5 if your d value is 6.
Simply change your second main for loop to start from d-1, here is the code
public void printStarsDiamond(int d)
{
for (int i = 1; i < d; i = i + 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 0; k < i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = d-1; i > 0; i = i - 2)
{
for (int j = 0; j < (d - i) / 2; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
The code below will print a diamond pattern using a single loop:
private static void PrintDiamondSingleLoop(int row) {
row++;
int mid = row/2;
int midLeft = mid;
int midRight = mid;
int currentRow = 1;
int currentColumn = 1;
for (int i = 1; i <= row * row; i++)
{
if(i % row == 0)
{
System.out.println("");
if(currentRow < mid)
{
midLeft--;
midRight++;
}else
{
midLeft++;
midRight--;
}
currentColumn = 1;
currentRow++;
}
if (currentColumn >= midLeft && currentColumn <= midRight)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
currentColumn++;
}
}
(This code is taken from an article published on my website: https://programtown.com/how-to-print-diamond-pattern-using-single-for-loop-in-java/.)
I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern 1
54321
5432
543
54
5
Pattern 2
1
12
123
1234
12345
Pattern 3
12345
2345
345
45
5
Pattern 4
1
123
12345
123
1
My code that almost matched pattern 1 is the following but doesn't work like the example above.
for (int i = 1 ; i <= rows ; i++) {
for (int j = (rows + 1 - i) ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.print("\n");
}
public class PrintPattern {
public static void main(String[] args){
printPattern1();
printPattern2();
printPattern3();
printPattern4();
}
public static void printPattern1(){
for (int i = 0; i<5; i++){
for(int j = 5; j>i; j--)
System.out.print(j);
System.out.println();
}
}
public static void printPattern2(){
for (int i = 0; i<5; i++){
for(int k = 0; k<4-i; k++)
System.out.print(" ");
for(int j = 1; j<=i+1; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern3(){
for (int i = 0; i<5; i++){
for(int k = 0; k<i; k++)
System.out.print(" ");
for(int j = i+1; j<=5; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern4(){
for (int i = 0; i<5; i++){
for(int k = 0; k<Math.abs(2-i); k++)
System.out.print(" ");
for(int j = 1; j<=5-2*Math.abs(2-i); j++)
System.out.print(j);
for (int p = 0; p<Math.abs(2-i); p++)
System.out.print(" ");
System.out.println();
}
}
}
Your inner for loop
for (int j = (rows + 1 - i) ; j > 0 ; j-- ){
System.out.print(j);
}
will always count down to 1, because it keeps going until j is zero. Also, the number that the current row starts on will depend on the current row, because you used i in your assignment to j. To get pattern 1 both of those things will have to change.
Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -
int rows = 5; // <- Start at 5.
for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
System.out.print(j);
}
System.out.println();
}
Output is pattern 1 in your question,
54321
5432
543
54
5
Try this:
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 2; k >= i; k--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 2; i++) {
for (int k = i; k > 0; k--) {
System.out.print(" ");
}
if (i % 2 != 0) {
for (int j = 1; j <= (2 * i + 1); j++) {
System.out.print(j);
}
} else {
for (int j = 1; j <= (i / 2); j++) {
System.out.print(j);
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
int rows = 5;
System.out.println("------ PATTERN 1 ------");
for (int i = 1 ; i <= rows ; i++){
for (int j = rows; j >= i ; j--){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 2 ------");
for (int i = 1 ; i <= rows ; i++){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 3 ------");
for (int i = rows ; i >= 1 ; i--){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 4 ------");
int whitespaces = rows/2;
for (int i = 1 ; i <= rows; i++){
// absolute value of whitespaces
int abs_whitespaces =
(whitespaces < 0 ? -whitespaces : whitespaces);
for (int j = 0 ; j < abs_whitespaces ; j++){
System.out.print(" ");
}
for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){
System.out.print(j);
}
whitespaces-=1;
System.out.println();
}
}
The first program is:
class timepass {
public static void main() {
for (int a = -1;a<=5;a++) {
for(int b = 5; b >= a;b--) {
System.out.print("*");
}
System.out.println();
// enter code here
}
}
}
The second program is:
class timepass {
public static void main() {
for(int i = 1;i<= 6;i++) {
for(int j = 1;j<= i ;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Triangle {
public static void main(String[ ] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter no.of line you need");
int n = scan.nextInt();
for(int i=1;i<=n;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println(" ");
}}}
So I was assigned to make a diamond with asterisks in Java and I'm really stumped. Here's what I've come up with so far:
public class Lab1 {
public static void main(String[] args) {
for (int i = 5; i > -5; i--) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j >= i; j--) {
System.out.print(" ");
}
System.out.println("*");
}
}
}
In order to make a diamond you need to set spaces and stars in shape. I have made this simple program using only nested loops since I am a beginner.
public class Diamond {
public static void main(String[] args) {
int size = 9,odd = 1, nos = size/2; // nos =number of spaces
for (int i = 1; i <= size; i++) { // for number of rows i.e n rows
for (int k = nos; k >= 1; k--) { // for number of spaces i.e
// 3,2,1,0,1,2,3 and so on
System.out.print(" ");
}
for (int j = 1; j <= odd; j++) { // for number of columns i.e
// 1,3,5,7,5,3,1
System.out.print("*");
}
System.out.println();
if (i < size/2+1) {
odd += 2; // columns increasing till center row
nos -= 1; // spaces decreasing till center row
} else {
odd -= 2; // columns decreasing
nos += 1; // spaces increasing
}
}
}
}
As you can see nos is the number of spaces. It needs to be decreased until the center row, and the number of stars needs to be increased but after the center row it's the opposite, i.e spaces increase and stars decrease.
size can be any number. I set it to 9 over here so I will have a size 9 star that is 9 rows and 9 columns max... number of space (nos) will be 9/2 = 4.5 .
But java will take it as 4 because int can not store decimal numbers and the center row will be 9/2 + 1 = 5.5, which will result in 5 as int.
So first you will make rows... 9 rows hence
(int i=1;i<=size;i++) //size=9
then print spaces like I did
(int k =nos; k>=1; k--) //nos being size/2
then finally stars
(int j=1; j<= odd;j++)
once the line ends...
You can adjust stars and spaces using an if condition.
for (int i = 0; i < 5; i++)
System.out.println(" *********".substring(i, 5 + 2 * i));
for (int i = 5; i > 0; i--)
System.out.println(" **********".substring(i - 1, 5 + (2 * i) - 3));
Note: Using Count Global variable we can manage space as well as star increment and decrement.
import java.util.*;
public class ParamidExample {
public static void main(String args[]) {
System.out.println("Enter a number");
Scanner sc = new Scanner(System.in);
int no = sc.nextInt();
int count = 1;
for (int i = 1; i <= 2 * no - 1; i++) {
for (int j = count; j <= no; j++) {
System.out.print(" ");
}
for (int k = 1; k <= count * 2 - 1; k++) {
System.out.print("* ");
}
if (i < no)
count++;
else
count--;
System.out.println("");
}
}
}
public class MyDiamond {
public static void main(String[] args) {
//Length of the pyramid that we want.151 is just an example
int numRows = 151;
//midrow is the middle row and has numRows number of *
int midrow = (numRows + 1) / 2;
int diff = 0;
for (int i = 1; i < numRows + 1; i++) {
for (int j = 1; j < numRows + 1; j++) {
if (((midrow - diff) <= j && (j <= midrow + diff))) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
if (i < midrow) {
diff++;
} else {
diff--;
}
}
}
}
public class Diamond {
//Size of the diamond
private int diagonal;
public Diamond(int diagonal) {
this.diagonal = diagonal;
}
public void drawDiamond() {
int n = diagonal;
for (int i = n / 2; i >= -n / 2; i--) {
for (int k = 0; k < i; k++) {
System.out.print(" ");
}
for (int j = 1; j <= (n - i * 2) && i >= 0; j++) {
System.out.print("*");
}
for (int k = 1; k <= -i && i < 0; k++) {
System.out.print(" ");
}
for (int j = (n / 2) * 2 + 2 * i; j >= -(n % 2 - 1) && i < 0; j--) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
//You pass diamond size here in the constructor
Diamond a = new Diamond(21);
a.drawDiamond();
}
}
The main problem is parity of diagonal.
If it's even you can't properly draw top asterisk. So there is 2 types of diamonds - with even and odd diagonal (with 2 and 1 asterisk at the top).
I can see what you are trying to do and this is a pretty neat way to think about the diamond.
You will have some issues with the j counter when i goes negative..look at how to use Math.abs()
Also try writing some pseudo code in basic steps with comments to get the pattern clear:
//print 5 spaces + 1 star
//print 4 spaces + 2 stars
//print 3 spaces + 3 stars
//print 2 spaces+ 4 stars
.
.
.
//print 5 spaces + 1 star
Then, literally substitute variables (j and i) for the numbers.
You now have a model. This is often the hardest part in programming..getting the model right. Only jump into coding when you have a good idea for how the model works.
Once you have the variables substituted, you can try to convert the whole thing into an automated loop.
import java.util.Scanner;
public class MakeDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Let's Creat Diamonds");
System.out.println("If number increases Diamonds gets bigger. Please input number lager than 1 : ");
int user_input = sc.nextInt(); //gets user's input
System.out.println("");
int x = user_input;
int front_space = -5;
for (int i = 0; i < 2 * user_input + 1; i++) {
for (int a = front_space; a < Math.abs(i - user_input); a++) {
System.out.print(" "); //Change a bit if diamonds are not in good shape
}
if (i < user_input + 1) {
for (int b = 0; b < 2 * i + 1; b++) {
System.out.print("* "); //Change a bit if diamonds are not in good shape
}
} else if (i > user_input) {
for (int c = 0; c < 2 * x - 1; c++) {
System.out.print("* "); //Change a bit if diamonds are not in good shape
}
x--;
}
System.out.print('\n');
}
System.out.println("\nRun Again? 1 = Run, 2 = Exit : ");
int restart = sc.nextInt();
System.out.println("");
if (restart == 2) {
System.out.println("Exit the Program.");
System.exit(0);
sc.close();
}
}
}
}
When making diamonds with while or for loops.
I think using 'Math.abs' will be the simplest way to make it.
You can put number by Scanner, and when input number increases diamonds will get bigger.
I used Eclipse to make this program.
so, the Space will be differ by your running environment. like another IDE, CMD or Terminal. if diamonds are not in good shape. Just change spaces.
package com.DiamondPrintingProgram;
import java.util.Scanner;
public class DiamondPrintingProgram {
public static void main(String[] args) {
int num = getInput();
int middle = (int) num / 2 + 1;
printOutput(num,middle);
}
public static int getInput() {
Scanner sc = new Scanner(System.in);
int num;
System.out.print("Enter a odd number: ");
while (true) {
num = sc.nextInt();
if (num % 2 != 0) {
break;
}
System.out.print("Please Enter a ODD NUMBER: ");
}
return num;
}
private static void printOutput(int num, int middle){
char asterisk = '*';
for (int j = 0; j < num; j++) {
for (int i = 1; i <= num; i++) {
if (j < middle) {
if ((i < (middle - j) || i > (middle + j))) {
System.out.print(' ');
} else {
System.out.print(asterisk);
}
} else {
if ((i < (j - middle + 2)) || (i > (2 * num - j - middle))) {
System.out.print(' ');
} else {
System.out.print(asterisk);
}
}
}
System.out.println();
}
}
}
I have the exact classwork in my university which also requires me to finish it in 3 for loops.
And this is how I did it.
In a simple way of explanation, I divide the diamond into two parts.
no. of lines
no. of spaces
no. of stars
total no. of slots
1
4
1
5
2
3
3
6
3
2
5
7
4
1
7
8
5
0
9
9
6
1
7
8
7
2
5
7
8
3
3
6
9
4
1
5
I want to find the no. of slots and the no. of spaces with each line, then allocating the no. of stars would be really easy.
And considering the no. of slots, line 1 - 5 and line 6 - 9 would become two separate groups(i.e. middleLine).
The equation of the no. of slots of the first half would be numberOfLines(i.e. i) + (middleLine - 1) where middleLine - 1 would be 4 when the maxNumberOfLines is 9.
The equation of the no. of slots of the last half would be middleLine(i.e. replacement of I) + (middleLine - 1)(i.e. same as above) - (i - middleLine) where i - middleLine would be -1 when i = 6.
And for the space, the first half would be middleLine - i and last half would be i - middleLine, which are exactly in a negative relationship(or symmetrical regarding their slopes).
public class printOutDiamondWith3Loops {
public static void main(String[] args) {
int userInput = 9;
double maxNumberOfLines = userInput;
// double type is used instead of integer type in order to prevent removal of remainder when a division performed.
double middleLine = Math.ceil(maxNumberOfLines/2);
// Print out the diamond.
for (int i = 1; i <= maxNumberOfLines; i++) {
// Determine the number of lines, which is also the maximum number of slots (the line in the middle).
if (i <= middleLine) {
// Separate the whole diamond into two parts(as mentioned above).
for (int j = 1; j <= i + ((middleLine - 1)); j++) {
// Determine the no. of slots in each line from line 1 to 5.
if (j <= middleLine - i) {
// Determine the no. of spaces and stars.
System.out.print(" ");
} else {
System.out.print("*");
}
}
} else { // i > middleLine
for (int k = 1; k <= (middleLine + (middleLine - 1)) - (i - middleLine); k++) {
// Determine the no. of slots in each line from line 6 to 9.
// For better understanding, I did not simplify the above condition.
// Noticeably, the first middleLine in above for loop is a replacement of i.
if (k <= i - middleLine) {
// Determine the no. of spaces and stars.
System.out.print(" ");
} else {
System.out.print("*");
}
}
}
System.out.println();
}
}
With such a framework, it is much easier to make further changes, such as letting the user input the no. of lines they want.
Hope this answer could help you.
I could lend you a more detailed version of my work, though not necessarily in need(the above explanation already explains the concepts): print-Out-Diamond-With-3-Loops-Advanced-Version.java
You can print a diamond of asterisks (mathematical operators) as follows:
int m = 4;
int n = 4;
for (int i = -m; i <= m; i++) {
for (int j = -n; j <= n; j++) {
int val = Math.abs(i) + Math.abs(j);
System.out.print(val > Math.max(m, n) ? " " : "∗");
if (j < n) {
System.out.print(" ");
} else {
System.out.println();
}
}
}
Output:
∗
∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗
∗
Try this
public class Main {
public static void main(String[] args) {
int n = 50;
int space = n - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < space; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println("");
space--;
}
space = 0;
for (int i = n; i > 0; i--) {
for (int j = 0; j < space; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("* ");
}
System.out.println("");
space++;
}
}
}
java-11
Using String#repeat introduced as part of Java-11, you can do it using a single statement inside a single loop.
public class Main {
public static void main(String[] args) {
final int MID_ROW_NUM = 5;
for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((MID_ROW_NUM - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
By changing the space, you can also print a variant of the diamond:
public class Main {
public static void main(String[] args) {
final int MID_ROW_NUM = 5;
for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "* ".repeat((MID_ROW_NUM - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
import java.util.Scanner;
public class Diamond {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int input = in.nextInt();
int min = 1;
for (int i = 0; i < input; i++) {
for (int j = input - 1; j > i; j--) {
System.out.print(" ");
}
for (int k = 0; k < min; k++) {
if (k % 2 == 0) {
System.out.print("*");
} else {
System.out.print(".");
}
}
min += 2;
System.out.println();
}
int z = input + input - 3;
for (int i = 1; i < input; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int k = 0; k < z; k++) {
if (k % 2 == 0) {
System.out.print("*");
} else {
System.out.print(".");
}
}
z -= 2;
System.out.println();
}
}
}
This should work. You probably only need most of the methods and printDiamond(_);
import java.util.Scanner;
public class StarsTry {
public static void main(String[] args) {
int reader;
Scanner kBoard = new Scanner(System.in);
do {
System.out.println("Insert a number of rows: ");
reader = kBoard.nextInt();
printDiamond(reader);
} while (reader != 0);
}
public static void printStars(int n) {
if (n >= 1) {
System.out.print("*");
printStars(n - 1);
}
}
public static void printTopTriangle(int rows) {
int x = 1;
for (int j = (rows - 1); j >= 0; j--, x += 2) {
printSpaces(j);
printStars(x);
System.out.print("\n");
}
}
public static void printSpaces(int n) {
if (n >= 1) {
System.out.print(" ");
printSpaces(n - 1);
}
}
public static void printBottomTriangle(int rows, int startSpaces) {
int x = 1 + (2 * (rows - 1));
for (int j = startSpaces; j <= (rows) && x > 0; j++, x -= 2) {
printSpaces(j);
printStars(x);
System.out.print("\n");
}
}
public static void printBottomTriangle(int rows) {
int x = 1 + (2 * (rows - 1));
for (int j = 0; j <= (rows - 1) && x > 0; j++, x -= 2) {
printSpaces(j);
printStars(x);
System.out.print("\n");
}
}
public static void printDiamond(int rows) {
printTopTriangle((int) rows / 2 + 1);
printBottomTriangle((int) rows / 2, 1);
}
}
import static java.lang.System.out;
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
sc.close();
Diamond d = new Diamond();
d.upperDiamond(row);
d.lowerDiamond(row - 2);
}
public void upperDiamond(int a) {
for (int i = 0; i < a; i++) {
for (int j = a - 1; j > i; j--)
out.print(" ");
for (int k = 0; k < 2 * i - 1; k++)
out.print("*");
out.print("\n");
}
}
public void lowerDiamond(int b) {
for (int i = 0; i < b; i++) {
for (int j = 0; j <= i; j++)
out.print(" ");
for (int k = 0; k < 2 * (b - i) - 1; k++)
out.print("*");
out.print("\n");
}
}
}
public class Main {
public static void main(String[] args) {
int number = 23,l =1,diff = 1,rem = number/2,rep = 0;
for(int i=1;i<=number;i++){
if(i < number/2 +1){
for(int k=rem;k>=1;k--)
System.out.print(" ");
for(int j=1;j<=l;j++)
System.out.print("*");
diff = 2;
rem -= 1;
}
if(i >= number/2 +1){
for(int k=0;k<rep;k++)
System.out.print(" ");
for(int j=1;j<=l;j++)
System.out.print("*");
diff =3;
rep += 1;
}
System.out.println();
l = diff == 2 ? (l + 2) : (l - 2);
}
}
}
//Suitable for only Odd numbers...
public class Main {
private static int l =1;
public static void main(String[] args) {
int number =9;
for(int i=1;i<=2*number -1;i++){
if(i<=number){
for(int j=1;j<=(number-i);j++)
System.out.print(" ");
for(int j=1;j<=i;j++)
System.out.print("* ");
}
if(i>number){
for(int j=1;j<=i-number;j++)
System.out.print(" ");
for(int j=1;j<=number-l;j++)
System.out.print("* ");
l += 1;
}
System.out.println();
}
}
}
class Inc_Dec {
public static void main(String[] args) {
int le = 11;
int c = 0;
int j1 = (le / 2) + 1;
int j2 = le - j1;
for (int i = 1; i <= le; i++) {
if (c < j1) {
for (int k = (j1 - i); k > 0; k--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
c++;
System.out.println();
} else {
for (int k = (i - j1); k > 0; k--) {
System.out.print(" ");
}
for (int j = (le - i + 1); j > 0; j--) {
System.out.print("*" + " ");
}
System.out.println();
}
}
}
}
package practice;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
if (i <= 5) {
for (int k = 1; k <= 5 - i; k++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(" *");
}
}
if (i > 5) {
for (int k = 0; k <= i - 6; k++) {
System.out.print(" ");
}
for (int j = 0; j <= 10 - i; j++) {
System.out.print(" *");
}
}
System.out.println();
}
}
}