Beginner in Java - Controlling output - Generating Asterix - java

I am having difficulties with completing this program. I am trying to make a program that creates asteriks, but then makes it into a triangle.
This is what I have already.
public class 12345 {
public static void main(String[] args) {
int n = 0;
int spaces = n;
int ast;
System.out.println("Please enter a number from 1 - 50 and I will draw a triangle with these *");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
for (int i = 0; i < n; i++) {
ast = 2 * i + 1;
for (int j = 1; j <= spaces + ast; j++) {
if (j <= spaces)
System.out.print(' ');
else
System.out.print('*');
}
System.out.println();
spaces--;
}
}
}
It is creating the asteriks, but how would I be able to continue them where they make a triangle... so they get bigger as they go, and then back smaller...
Thank you in advance!

Try moving
int spaces = n;
to AFTER the value of n is read from stdin.
This solves half your problem and hopefully gets you on the right track.

I added a few things to your code and got it to print the full triangle, where the number input in the scanner will be the number of asterisks printed in the bottom row. I.e. if the input is 3, the triangle will be two rows of 1->3; if the input is 5 then the triangle will be 3 rows of 1->3->5, and so on.
public static void main(String[] args) {
int ast;
int reverse = 1;
System.out.println("Please enter a number from 1 - 50 and I will draw a triangle with these *");
Scanner keyboard = new Scanner(System.in);
int spaces = keyboard.nextInt();
for (int i = 0; i < spaces; i++) {
ast = 2 * i + 1;
for (int j = 1; j <= spaces + ast; j++) {
if (j <= spaces) {
System.out.print(' ');
} else {
System.out.print('*');}
if (j > spaces + ast) {
for (int k = 0; k < spaces-(reverse-1); k++) {
System.out.print(' ');
}
}
int k = 0;
reverse++;
}
System.out.println();
spaces--;
}
}
}
I added another if statement after your if-else that triggers when the variable j exceeds the first loop condition. This triggers another loop that makes the output lines symmetrical by essentially repeating your first if statement.
I hope this helps =)

Related

What I am misunderstanding with code that prints a pyramid?

My code is supposed to make a pryamid. The example below, however, is supposed to have another A character at the end, like the second example.
ABCDDCBA
ABCCBA
ABBA
AA
ABCDDCBA
ABCCBA
ABBA
AA
A
This is my code. It starts with ABCDDCBA, and removes the characters in the middle each time. So ABCDDCBA would be ABCCBA as the Ds get removed. However, when there are two characters (always the same) the code is supposed to remove one, but it doesn't.
public static void pyramid(int n)
{
int i, j, num, gap;
// outer loop to handle number
// of rows n in this case
for (i = n; i >= 0; i--) {
// inner loop to create right triangle
// gaps on left side of pyramid
for (gap = n - 1; gap >= i; gap--) {
System.out.print(" ");
System.out.print(" ");
}
// initializing value corresponding to ASCII value of 'A'
num = 'A';
// loop to print characters on
// left side of pyramid
for (j = 1; j <= i; j++) {
System.out.print((char)num++ + " ");
}
// loop to print characters on
// right side of pyramid
for (j = i - 0; j >= 1; j--) {
System.out.print((char)--num + " ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
pyramid(n);
}
Please help me. :(
You have a typo in your code:
for (j = i - 0; j >= 1; j--) {
should be
for (j = i - 1; j >= 1; j--) {

Number Triangles with alternating number

Hello All,
I was wondering if someone could help me with making a number triangle in Java that looks like the one below using nested while loops. Would someone be able to help me out?
4
56
789
1234
56789
I have a variable 'i' on the outer loop determining how many rows the triangle will be and a variable 'j' on the inner loop determine which number the triangle will begin with. the numbers have to stay between [1-9].
Can anyone help me out?
Try This, It Will Work... It accepts rows & number through user and in first for loop it runs the loop till the number of rows and second loop print the number as per the value of I in pattern and if condition to check if the number is 10 then reset the number with 1 to start the numbering again.
import java.util.Scanner;
public class Pattern {
public static void main(String[] args) {
int rows, number = 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of rows");
rows = sc.nextInt();
System.out.println("Enter no to start with");
number = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
++number;
if (number == 10) {
number = 1;
}
}
System.out.println();
}
}
}
Try.. r is for number of rows and v is the value
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int v = sc.nextInt();
int i = v - 1;
int j = 1;
while(j != r + 1){
int k = 0;
int ans = 0;
while( k < j){
i = i + 1;
if(i == 10){
i = 1;
}
ans = ans * 10 + i;
k = k + 1;
}
System.out.println(ans);
j = j + 1;
}

How do I get my program to state an error and exit if input is < 1

So this program just prints a basic centered triangle. The user inputs how many lines they want the triangle to have in it. I'm just not sure how to insert an error message and exit the program with in this. What would be the best way to do it without disrupting anything?
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter the number of lines: ");
Scanner input = new Scanner(System.in);
int numLines = input.nextInt();
for (int i = 0; i < numLines; i++)
{
for (int j = 0; j <= numLines - i; j++)
{
System.out.print(" "); //prints the proper number of spaces so that it's centered
}
for (int k = 0; k <= 2*i; k++)
{
System.out.print("*"); //prints proper number of *
}
System.out.println(); //makes new row
}
}
}
After
int numLines = input.nextInt();
you add
if (numlines <= 0)
return;

Printing a word in a diamond format

My assignment is to print a word in the shape of a diamond like so:
*****s
****p*p
***i***i
**d*****d
*e*******e
r*********r
*e*******e
**d*****d
***i***i
****p*p
*****s
P.S. The asterisks are only there to show spacing, pretend one asterisk represent one space.
So far I have this:
public class DiamondWords
{
public static void main(String[] args)
{
Scanner kbReader = new Scanner(System.in);
System.out.print("Enter a word to be printed in diamond format: ");
String word = kbReader.nextLine();
int wordLength = word.length();
for(int i = 0; i<wordLength-1; i++)
{
System.out.print(" ");
}
wordLength = wordLength - 1;
System.out.print(word.charAt(0));
System.out.println();
int x =1;
int d =1;
for(int j =wordLength; j>0; j--)
{
wordLength = j;
for(int a =1; a<wordLength; a++)
{
System.out.print(" ");
}
System.out.print(word.charAt(x));
for(int q =0; q<d; q++)
{
System.out.print(" ");
}
d+=2;
System.out.print(word.charAt(x));
x++;
System.out.println();
}
//r*********r
//*e*******e
//**d*****d
//***i***i
//****p*p
//*****s
}
}
Which prints the first half of the diamond perfectly:
*****s
****p*p
***i***i
**d*****d
*e*******e
r*********r
The only part where I'm getting stuck is when I have to print the latter half of the diamond. Can anyone point me in the right direction? Please do not write the code for me, just try and give me some pointers based off the logic I've shown. Thank you.
Try to have only one loop. A very easy way to handle the "technicalities" of the problem is to work with an char array for the output. First you initialize it with the proper length, fill it with blanks (there is a library function for it), fill the two characters, and only then convert it to a String.
The only open question is where to put the characters, and I don't want (and should) to spoil that.
int fullLength = 2 * word.length() - 1;
for(int i = 0; i < fullLength; i++) {
char[] line = new char[fullLength];
Arrays.fill(line, ' ');
int k = ???;
char c = s.charAt(k);
line[word.length() - 1 - k] = c;
line[word.length() - 1 + k] = c;
System.out.println(new String(line));
}
Obviously, you want to calculate the position "from the middle" (so we have something like word.length() - 1 +- k), and for the first half of the word, k is equal to i.
Your task, should you decide to accept it, is to find out how to "bend k back" for the second half of the word.
import java.util.Scanner;
public class DiamondWords
{
public static void main(String[] args)
{
Scanner kbReader = new Scanner(System.in);
System.out.print("Enter a word to be printed in diamond format: ");
String word = kbReader.nextLine();
int wordLength = word.length();
int wordLength2 = word.length();
int wordSize = word.length();
int wordLengthReverse = word.length();
for(int i = 0; i<wordLength-1; i++)
{
System.out.print(" ");
}
wordLength = wordLength - 1;
System.out.print(word.charAt(0));
System.out.println();
int x =1;
int d =1;
for(int j =wordLength; j>0; j--)
{
wordLength = j;
for(int a =1; a<wordLength; a++)
{
System.out.print(" ");
}
System.out.print(word.charAt(x));
for(int q =0; q<d; q++)
{
System.out.print(" ");
}
d+=2;
System.out.print(word.charAt(x));
x++;
System.out.println();
}
System.out.print(" " + word.charAt(wordLength2-2));
int spaceLength =((wordLength2*2)-1) -4;
int u =spaceLength -2;
for(int i =0; i < spaceLength; i++)
{
System.out.print(" ");
}
System.out.print(word.charAt(wordLength2-2));
System.out.println();
int m=3;
for(int num =2; num<wordSize-1; num++)
{
wordLength2 = num;
for(int i =0; i<num; i++)
{
System.out.print(" ");
}
System.out.print(word.charAt(wordSize-m));
for(int b = 0; b<u; b++)
{
System.out.print(" ");
}
System.out.print(word.charAt(wordSize-m));
System.out.println();
m++;
u = u-2;
}
for(int r =0; r<word.length()-1; r++)
{
System.out.print(" ");
}
System.out.print(word.charAt(0));
}
}
I have finished. This is my final code. I understand it is not as efficient as possible, or easy to follow, but it is flexible and not hard-coded, so I am content.

Triangle of for loops

I am working on an assignment and I understand how to do the first part of the assignment but not the second.
Problem:
Write a program that ask the user to enter the size of a triangle (1 to 50), then print the triangle by printing a series of lines consisting of asterisks. The first line will have 1 asterisk, the next two will have two and so on, with each line having one more asterisk than the previous line up to the number entered by the user. On the next line print one less asterisk and continue by decreasing the number of asterisk by 1 for each successive line until any one asterisk is printed.
I can make the program print up ward however I don't know how to make it print downward. My professor says to use for loops.
import java.util.Scanner;
public class CS123Ass5ID5189 {
public static void main(String[] args) {
int size;
System.out.println("Enter Triangle size");
Scanner key = new Scanner(System.in);
size = key.nextInt();
System.out.println(size);
for (int i = 0; i < size; i++) {
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println("*");
}
for (int i = 0; i > size; i--) {
for (int f = 0; f > i; f--) {
System.out.println("*");
}
System.out.println("*");
}
}
}
Supposed to look something like
*
**
***
**
*
Your answer is almost there. You just need the second outer loop to count down from size to zero in the outer loop, and do the same thing.
for (int i = size - 1; i > 0; i--)
So the first loop goes from 0 to size, and this loop goes from (size - 1) to 0.
for (int i = size - 1; i > 0; i--) {
// This section is exactly the same as in the first loop
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println("*");
}
Edit
To limit the input number to 50, check it in another loop. While the user has entered an invalid number, tell them, and ask for another one. Add this just after you get the initial number:
while (size < 0 || size > 50)
{
System.out.print("Size must be between 0 and 50. Try again: ");
size = key.nextInt();
}
Your second loop is wrong, it should be for i>0. Also, start at 1, not 0 and print like this:
for (int i = 1; i <= size; i++) {
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println();
}
for (int i = size-1; i > 0; i--) {
for (int f = 0; f < i; f++) {
System.out.print("*");
}
System.out.println();
}
Your inner loop should still be counting up to the value of i. This should do what you need.
For loops can begin at any value, not just zero. This allows you to start at size and decrement all the way down to zero.
Here is an example of what you are looking for. (It links to an online executable to demonstrate the code below.)
public class Main
{
public static void main (String[] args)
{
int size = 10;
System.out.println(size);
for (int i=0; i<size-1; i++)
{
for (int f=0; f<i; f++)
{
System.out.print("*");
}
System.out.println("*");
}
for (int i=size-1; i>=0; i--)
{
for (int f=i; f>0; f--)
{
System.out.print("*");
}
System.out.println("*");
}
}
}
P.S. Your second inner for loop should have System.out.print not System.out.println to keep all the asterisks on one line.
Your first loop looks fine, but look at the second loop.
for (int i = 0; i > size; i--) {
for (int f = 0; f > i; f--) {
System.out.println("*");
If size is greater than 0, the outer for-loop won't be entered. Fix that by setting i = size - 1, then loop until i > 0 and i-- to decrease i in each loop.
The inner for loop has a similar problem, it should be the exact same and the previous inner loop. Also exact same in the usage of System.out.print instead of System.out.println. Otherwise, the triangle will print vertically for the bottom half.
FWIW - It's easier to read if you make small methods to clearly show your intention.
private static void printStarLine(int howManyStars) {
for (int i = 0; i < howManyStars; i++) {
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
int size = 3; // some number in the range 1-50
do {
System.out.println("Enter Triangle size (1-50)");
size = key.nextInt();
} while (size < 1 || size > 50);
for (int stars = 1; stars < size; stars++) {
printStarLine(stars);
}
printStarLine(size);
for (int stars = size - 1; stars > 0; stars--) {
printStarLine(stars);
}
}
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
for(i=n; i>=1; i--)
{
for(j=1; j<i; j++)
{
System.out.print("*");
}
System.out.println();
}
this works

Categories

Resources