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
}
Related
my project to create a triangle in Java with numbers starting from the right
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// loop
for (int i=1; i <= 6; i++) {
// space
for(int j=1; j <= 6-i; j++)
System.out.print(" ");
// numbers
for(int k= 1; k <= i; k++)
System.out.print(k);
// new line
System.out.println();
}
}
}
Here is one way. It uses String.repeat() to pad with leading spaces.
outer loop controls the rows.
Then print the leading spaces
inner loop iterates backwards, printing the value and a space
then print a newline
int rows = 6;
for (int i = 1; i <= rows; i++) {
System.out.print(" ".repeat(rows-i));
for (int k = i; k > 0; k--) {
System.out.print(k+ " ");
}
System.out.println();
}
prints
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
If you don't want to use String.repeat() then use a loop.
Well the only thing you have to do is change order like you are first adding space and then number instead add number and then space.
code -
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// loop
for (int i=1; i <= 6; i++) {
// numbers
for(int k= 1; k <= i; k++)
System.out.print(k);
// space
for(int j=1; j <= 6-i; j++)
System.out.print(" ");
// new line
System.out.println();
}
}
}
Now we get the result -
start k=i then decrement it till k>=1.
for(int k= i; k >=1 ; k--)
System.out.print(k+" ");
output
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int lines = scanner.nextInt();
for (int i = 0; i <= lines; i++) {
//space
int spaceNum = lines - I;
for (int j = 0; j < spaceNum; j++) {
System.out.print(" ");
}
//numbers
int numbers = lines - spaceNum;
for (int j = numbers; j > 0; j--) {
System.out.print(" " + j);
}
System.out.println();
}
}
}
This question already has answers here:
Creating a triangle with for loops
(13 answers)
Closed 3 years ago.
I started learning java, and that's my question :
How to write to the right side of the triangle?
import java.util.Scanner;
public class Triangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("please enter your siz triangle :");
int lines = scanner.nextInt();
for (int i = 0; i <= lines; i++) {
for (int j = 1; j < i; j++) {
System.out.println("*");
}
}
scanner.close();
}
}
for 5 i get:
*
*
*
*
*
You can do it like this, use two loops first one to print the number of spaces and then second to print the number of stars based on the number of rows inputted by user. This prints the complete triangle :
Scanner scanner = new Scanner(System.in);
System.out.println("please enter your siz triangle :");
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
// printing spaces
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
// printing stars
System.out.print("* ");
}
System.out.println();
}
scanner.close();
You are almost there, you need to know how to print. And small correction in for loop indexes. Starting j with 1 will lose one row. I set it to 0 and starting i with 1
for (int i = 1; i <= lines; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
Input:
3
Output:
*
**
***
I'm building out a user defined array as a game board. The characters used "O" and "." have to be randomized and the "O" has to appear more than once.
This is what I have thus far.
import java.util.Scanner;
public class PacMan {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Input total rows:");
int row = input.nextInt();
System.out.println("Input total columns:");
int column = input.nextInt();
boolean[][] cookies = new boolean[row+2][column+2];
for (int i = 1; i <= row; i++)
for (int j = 1; j <= column; j++);
cookies [row][column] = (Math.random() < 100);
// print game
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= column; j++)
if (cookies[i][j]) System.out.print(" O ");
else System.out.print(". ");
System.out.println();
}
}
}
The output, for example, produces a 5 x 5 grid, but the "O" only appears once and is at the bottom right of the grid.
Assistance randomizing the "O" and "." and having the "O" appear in random fashion throughout the board which is initialized by the user input via Scanner.
Here is the updated code which is producing the output that I'm looking for and is user defined.
import java.util.*;
public class PacManTest
{
public static void main(String[] args)
{
char O;
Scanner input = new Scanner(System.in);
System.out.println("Input total rows:");
int row = input.nextInt();
System.out.println("Input total columns:");
int column = input.nextInt();
char board[][] = new char[row][column];
for(int x = 0; x < board.length; x++)
{
for(int i = 0; i < board.length; i++)
{
double random = Math.random();
if(random >.01 && random <=.10)
{
board[x][i] = 'O';
}
else {
board[x][i] = '.';
}
System.out.print(board[x][i] + " ");
}
System.out.println("");
}
}
}
The main issue is the typo in the first loop:
cookies [row][column] = (Math.random() < 100);
should be
cookies [i][j] = (Math.random() < 100);
Second, Math.random() returns a value greater than or equal to 0.0 and less than 1.0 (doc). So, (Math.random() < 100); will always be true. If you want a 50% chance of an O or . use:
cookies[i][j] = Math.random() < 0.5;
Also, not sure what your motivation is for using a starting index of 1 but array indexes start at 0.
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);
I'm trying to create a nested for loop that uses two user input values where value 1 equals the number of rows and value 2 equals the number to increment by. If the user enters 5 for both values, the output should be like this:
0
5 10
15 20 25
30 35 40 45
50 55 60 65 70
So far I have this:
public static void main(String[] args) {
Scanner myScan = new Scanner(System.in);
System.out.println("Please enter the number of rows: ");
int number= myScan.nextInt();
System.out.println("Please enter the number to increment by: ");
int number2= myScan.nextInt();
for(int i = 0; i <= number; i++){
for(int j = 0; j < i; j++){
for(int k = 0; k <=number2 + number; k++)
System.out.print(number + " ");
System.out.println(" ");
}
}}}
I know that I messed up somewhere within my coding. Help will be appreciated.
You don't need the third for loop (the one with int k).
You can write it this way:
int incr = 0;
for(int i = 0; i <= number; i++){
for(int j = 0; j < i; j++){
System.out.print((incr++) * number2 + " ");
}
System.out.println("");
}
Assuming that 'inc' is the amount to increment by and that 'rows' is the number of rows
int num = 0;
for(int row = 0; row < rows; row++) {
for(int nIndex = 0; nIndex <= row; nIndex++) {
System.out.print(num + " ");
num += inc;
}
System.out.println("");
}
import java.util.Scanner;
public class lem;
{
public static void main(String[] args)
{
int i,j,n=1,m=0;
Scanner ab=new Scanner(System.in);
System.out.println("Enter the number : ");
int num=ab.nextInt();
for(i=1;i<=num;i++)
{
for(j=0;j
n=5*m;
m++;
System.out.print(n+" ");
}
System.out.println();
}}}