I'm trying to make the output something like this:
I know the problem is in the third loop, but I dont know what to do to make the spacing work for this.
import java.util.Scanner;
public class Tester {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int x, y;
System.out.print("Enter the number of rows: ");
x = in.nextInt();
System.out.print("Enter the number of stars: ");
y = in.nextInt();
//loop for x lines
for(int i = 0; i < x; i++){
//loop for y stars
for(int j = 0; j < y; j++){
System.out.print("* ");
}
System.out.println();
for(int l = 0; l <= i; l--){
System.out.print(" ");
}
}
}
}
You need to do a couple things.
The first is move your last nested for loop (the one that prints spaces) to the beginning of the first for loop. You will also want to remove the space you have added to the for loop that prints the asterisks.
Then, for the output you have showed us, you need to start you main for loop at the end and go backwards.
Try the following:
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int x = in.nextInt();
System.out.print("Enter the number of stars: ");
int y = in.nextInt();
//loop for x lines
//This starts at x and goes toward 0
for(int i = x; i > 0; i--){
//Insert spaces based on line number
//This is at the beginning now
for (int s = 0; s < i; s++)
System.out.print(" ");
//Print y stars
//Removed the space after the asterisk
for(int j = 0; j < y; j++)
System.out.print("*");
System.out.println();
}
}
Tested here and matches the output in your first image
for (int i = 0; i < x; i++){
for (int j = x-1; j > i; j--) {
System.out.print(" ");
}
for(int j = 0; j < y; j++){
System.out.print("*");
}
System.out.println();
}
You have to reorder your for loops. Please note the change in condition in the second for loop in given ode below:
for(int i = 0; i < x; i++){
for(int l = 0; l <= x-i; ++l){
System.out.print(" ");
}
//loop for y stars
for(int j = 0; j < y; j++){
System.out.print("* ");
}
System.out.println();
}
Another way:
String stars = String.format("%0" + y + "d", 0).replace('0', '*');
for (int i=x; i > 0; i--)
{
System.out.println(String.format("%0$"+i+ "s", ' ')+stars);
}
System.out.println(stars);
Related
Help with this question:
Take a positive integer n and form n triangles from stars with their base down of size n each.
For example, for input 3, the following output will be obtained:
* * *
** ** **
*** *** ***
Here's what I've tried.
import java.util.Scanner;
public class ex3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter n");
int n = input.nextInt();
while (n <= 0) {
System.out.println("error");
n = input.nextInt();
}
for (int r = 1; r <= n; r++) {
for (int c = 1; c <= n - r; c++)
System.out.print(" ");
for (int c = 1; c <= r; c++)
System.out.print("*");
System.out.println();
}
}
}
Here's my solution, with explanations in comments
import java.util.Scanner;
class Main {
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in)) {
System.out.println("enter n");
int n = input.nextInt();
while (n <= 0) {
System.out.println("error");
n = input.nextInt();
}
for (int r = 1; r <= n; r++) { // <-- we will have to print n rows
printLine(n, r);
}
}
}
static void printLine(int n, int lineNumber) {
StringBuffer line = new StringBuffer();
for(int i = 0; i < n; i ++) { // <-- each line will have '*'s for n triangles
for(int j = lineNumber; j > 0; j--) { // <-- each line has as many '*'s as its line number, so print those first
line.append("*");
}
for(int j = 0; j < n - lineNumber + 1; j ++) { // <-- we then have to add enough spaces to leave room for the next triangle's '*'s
line.append(" ");
}
}
System.out.println(line.toString()); // <-- print the line we've built so far
}
}
EDIT:
Here's a replit that avoids one of the loops by using a modulo to print an entire line at once, and also uses recursion, for no real reason, in place of the outer-most loop: https://replit.com/#anqit/MicroExtrovertedTrace#Main.java
First, the blanks follow the stars, then, you have to repeat n times the two loops:
for (int r = 1; r <= n; r++) {
for (int t = 1; t <= n; t++) {
for (int c = 1; c <= r; c++)
System.out.print("*");
for (int c = 1; c <= n - r; c++)
System.out.print(" ");
}
System.out.println();
}
You have one for loop that prints out a single triangle of stars using nested for loops. The outer loop is responsible for the number of rows and the inner loops are responsible for printing the spaces and stars.
In my updated code, I added an outer loop that runs n times, and each time it runs, it prints out a triangle of stars. The inner loops are responsible for printing the stars of the triangle and spaces between the triangles.
The main difference is that in your first code, only one triangle is printed, while in my updated code, n triangles are printed. In addition, the indentation of the inner loops has been adjusted to align the triangles correctly on the same line.
import java.util.Scanner;
public class ex3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("enter n:");
int n = input.nextInt();
while (n <= 0) {
System.out.println("error: please enter a positive number:");
n = input.nextInt();
}
for (int r = 1; r <= n; r++) {
for (int t = 1; t <= n; t++) {
for (int c = 1; c <= r; c++) {
System.out.print("*");
}
for (int i = 0; i < n - r + 1; i++) {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Here is a 3 forloop variant
int n = 4;
for (int i = 1; i <= n; i++) {
StringBuilder b = new StringBuilder("");
for (int s = 0; s < n; s++) {
b.append(s < i ? "*" : " ");
}
String line = b.append(" ").toString();//space between
for (int j = 1; j < n; j++) {
System.out.print(line);
}
System.out.println();
}
produces
> * * *
> ** ** **
> *** *** ***
> **** **** ****
I can print a single pine tree of varying sizes, but how do I print an exact replica of it next to it and underneath it, in a grid, using only for loops? This project utilizes a scanner so that the User can vary the size and number of section each tree has. The last portion of the project allows the user to pick how many trees to print, horizontally(next to each other) and vertically(underneath it). The exact wording and an example: Program makes use of a third input to print a number of trees. Ex: When NUM_TREES is three, a 3x3 grid of trees is printed. Any tips or hints are helpful, thank you.
Here is the code I have to get a single tree:
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
printTree();
printStem();
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.println("\\");
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++) {
System.out.print(" ");
}
System.out.println("|");
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++) {
System.out.print(" ");
}
System.out.print("___");
}
}
}
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
static int gridSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
for(int x = 0; x < gridSize; x++){
printTree();
printStem();
System.out.println();
}
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
System.out.println("What grid size do you want?");
gridSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for(int x = 0; x < gridSize; x++){
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("\\");
int width = 2 * (sectionSize + numSections - 1) + 1;
if(x != gridSize - 1){
for(int y = sectionSize + numSections + k + i; y < width; y++)
System.out.print(" ");
System.out.print(" ");
}
}
System.out.println();
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++)
System.out.print(" ");
System.out.print("|");
if(x != gridSize - 1)
for (int j = 0; j <= sectionSize + numSections; j++)
System.out.print(" ");
}
System.out.println();
}
for (int i = 0; i < 1; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++)
System.out.print(" ");
System.out.print("___");
if(x != gridSize - 1)
for (int j = 0; j < sectionSize + numSections; j++)
System.out.print(" ");
}
}
}
}
width_of_double_pine_tree = 2 * width_of_single_pine_tree + buffer_between_trees
For loops don't have to define a variable, they can use an existing one.
int j = 0;
for(; j < (width_of_double_pine_tree - (width_of_single_pine_tree + buffer_between_trees)); j++)
//this is your first tree
for(; j < (width_of_double_pine_tree - width_of_single_pine_tree); j++)
//this is your buffer (S.o.p spaces)
for(; j < (width_of_double_pine_tree); j++)
//this is your second tree
Your canvas is always a rectangle, you subtract from it, the edge spaces you aren't using, void space with spaces (tabs ruin everything, because they aren't uniform, use spaces in everything!)
Use your i variable to keep track of the height that you're currently painting on your canvas so you can math in your pine needles and trunk. For the second tree, in the math portion, offset your j by the buffer_between_trees and width_of_single_tree if(i < (j + buffer + width)), etc.
Make sure you do the assignment before you tackle extra stuff.
Does anyone have an idea how to print X from given number?
e.g.:
given number 5.
So, I should print
X000X
0X0X0
00X00
0X0X0
X000X
This is my code, still missing something on it
public static void drawX(int number){
int[][] draw = new int[number][number];
for(int i = 0; i< number; i++){
for(int k = 0; k<=i; k++){
System.out.print(" ");
}
for(int j = number-1; j>i; j--){
if(j == number-1 || j == i+1)
System.out.print("X ");
else
System.out.print(" ");
}
System.out.println();
}
for(int i = 0; i< number; i++){
for(int v = number; v>i; v--){
System.out.print(" ");
}
for(int j = 0; j<i; j++){
System.out.print("X ");
}
System.out.println();
}
}
See comments on your code below, highlighting some issues I've noticed. It probably won't solve everything straight away, but it will give you a push in the right direction I hope.
public static void drawX(int number){
// draw is never used.
int[][] draw = new int[number][number];
for(int i = 0; i< number; i++){
for(int k = 0; k<=i; k++){
// Here we print a " " even for k == i.
// Are you sure you want k <= i ?
// If you change it, dont forget to also change
// stop clause in next loop.
System.out.print(" ");
}
for(int j = number-1; j>i; j--){
if(j == number-1 || j == i+1)
// j == number -1 prints X only in the last column
// Maybe you wanted number - i - 1?
// j == i+1 means you "skip" the ith element.
// Why the extra space after X?
System.out.print("X ");
else
// Why two spaces here?
System.out.print(" ");
}
System.out.println();
}
for(int i = 0; i< number; i++){
for(int v = number; v>i; v--){
System.out.print(" ");
}
for(int j = 0; j<i; j++){
// here you need to do very similar logic to
// what you did in previous loop, when printing first
// 'number' lines.
System.out.print("X ");
}
System.out.println();
}
}
I am trying to have the user enter a number and have the computer output a square with side of that number.
For example:
Enter a number:
4
XXXX
XXXX
XXXX
XXXX
I got this far, but don't know what else to do:
package blockmaker;
import java.util.*;
public class BlockMaker {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = scan.nextInt();
scan.nextLine();
for(int i = 0; i < number; i++){
System.out.println("X");
}
}
}
My current code outputs:
Enter a number:
4
X
X
X
X
Do I need to put a loop inside a loop?
Yes you need an inner loop for that:
for(int i = 0; i < number; i++){
for(int j = 0; j < number; j++) {
System.out.print("X");
}
System.out.println();
}
for(int i = 0; i < number; i++){
System.out.println("X"); // print X and new line
}
Above code prints "X" in each line.
Instead you need to print "X" number(n) times in each line. You need a nested loop.
for(int i = 0; i < number; i++){
for(int j = 0; j < number; j++)
System.out.print("X"); // print X - n times
System.out.println(); // print new line
}
So I'm beginning in the world of java programming language and I'm trying to print a christmas tree of X height. So far its working, but if for example the user input 4, it will print 4 rows + the christmas tree stump, wich mean 5. However, I would like it to be 4 INCLUDING the stump.So far I have this:
public class xmas {
public static void main(String[] args)
{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
int temp = scan.nextInt();
int x = (temp-1)*2 +1;
int y = x/2;
int z = 1;
for(int i=0; i<temp; i++)
{
for(int j=0; j<=y; j++)
{
out.print(" ");
}
for(int k = 0; k<z; k++)
{
out.print("*");
}
out.println();
y--;
z+=2;
}
for(int i =0; i<=x/2; i++)
{
out.print(" ");
}
out.println("*");
}
}
I don't know how to do that. Thanks!
Try using temp-- just after the input, like that:
int temp = scan.nextInt();
temp--;
Or decreasing your loop condition:
for(int i=0; i<temp-1; i++)
Output in both cases:
*
***
*****
*
If you just subtract one from the input, your christmas tree should be the right size. Here's what it would look like (using the Java style conventions):
public class ChristmasTree {
public static void main(String[] args) {
Scanner scanner = new Scanner(in);
out.print("Please enter a number: ");
int temp = scanner.nextInt() - 1; // note the `- 1`
int x = (temp - 1) * 2 + 1;
int y = x / 2;
int z = 1;
for(int i = 0; i < temp; i++) {
for(int j = 0; j <= y; j++) {
out.print(" ");
}
for(int j = 0; j < z; k++) {
out.print("*");
}
out.println();
y--;
z += 2;
}
for(int i =0; i<=x/2; i++) {
out.print(" ");
}
out.println("*");
}
}