I am trying to draw a grid that looks like this:
1
12
123
1234
12345
123456
1234567
12345678
123456789
Here is my code:
public class shape {
public static void main(String[] args){
int number = 1, newNumber, zMax = 1;
String numString = "1";
for (int i = 1; i <= 9; i++){
for (int z = 0; z < zMax; z++){
System.out.print(numString);
number = number + 1;
numString += Integer.toString(number);
}
System.out.println("");
if (zMax <= 9)
zMax++;
}
}
}
It prints out something like this:
1
12123
121231234
12123123412345
etc
It is on the right track but I can't figure out what is going wrong... please help!
EDIT: Miss-understood the question, corrected it.
The reason your numbers repeat is your second loop. you either need to reinitialize numstring or reuse the old one and only add the new number.
public class shape { public static void main(String[] args){
String numString = "";
for (int i = 1; i <= 9; i++){
numstring = numstring + i;
System.out.println(numstring);
}
}
int start = 1;
int max = 10;
for(int i = 1; i < max; i++){
for(int j = 1; j <= i; j++){
System.out.print(j);
}
System.out.println("");
}
Related
I'm using Java 8 and I need to get this output:
XXXXX
XXXX
XXX
XX
X
where "X" is a string.
I wrote a simple code:
String s = new String ("X");
int j = 5;
for (int i = 0; i<5; i--)
{
System.out.println(s);
j--;
if (j < 1)
break;
Naturally, get this:
X
X
X
X
X
I understand that I need to somehow make Java repeat printing the string i times (in a loop I assigned for i), but don't know how (neither repeat nor \i didn't work).
What's the best way to do it?
Thanks!
All you need to do is:
final String s = "X";
for (int i = 5; i > 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(s);
}
System.out.println();
}
which outputs:
XXXXX
XXXX
XXX
XX
X
You can use a recursive print operation to achieve this without loops.
import java.util.Collections;
public class Test {
public static void main(final String[] args) {
recursiveTriangle(10);
}
public static void recursiveTriangle(final int length) {
if (length <= 0 ) {
return;
}
System.out.println(String.join("", Collections.nCopies(length, "X")));
recursiveTriangle(length-1);
}
}
You can use while loop inside for
String s = "X";
for (int i = 5; i > 0; i--) {
int j=0;
while(i>j++)
System.out.print(s);
System.out.println();
}
concat here is another option:
String s = "";
for (int i = 5; i>0; i--) {
String s1 = "";
for (int j = i; j > 0; j--){
s1 += s.concat("X");
}
System.out.println(s1);
}
My code is resulting in an infinite loop when i run it. Don't know whats wrong.
!(https://d2vlcm61l7u1fs.cloudfront.net/media%2F1aa%2F1aa1af9f-41ff-48d8-89e9-7b093a909045%2Fphpbrt5Fy.png)
I have tried the code below. It compiles properly.
public class Project3_1 {
//declare SEGMENTS and HEIGHT
public static final int SEGMENTS = 4;
public static final int HEIGHT = 4;
public static final int TOTAL = (2*(SEGMENTS)) + (2*(HEIGHT)) - 3;
public static void TopTree(int SEGMENTS, int HEIGHT) {
for (int s = 1; s <= SEGMENTS; s++) {
for (int h = 1; h <= HEIGHT; h++) {
int ASTERISKS = ((2*s) + (2*h) - 3);
int SPACES = ((TOTAL - ASTERISKS)/2);
//spaces
for (int b = 1; b <= SPACES; b++) {
System.out.print(" ");
}
//Asterisks
for (int a = 1; a <= ASTERISKS; a++) {
System.out.print("*");
}
System.out.println();
}
}
}
public static void TreeBase() {
int x = (TOTAL - 7)/2;
for (int i = 1; i <= (TOTAL-1)/2; i++){
System.out.print(" ");
}
System.out.println("*");
for (int i = 1; i <= (TOTAL-1)/2; i++){
System.out.print(" ");
}
System.out.println("*");
for (int i = 1; i <= x; x++){
System.out.print(" ");
}
for (int i = 1; i <= 7; i++) {
System.out.print("*");
}
for (int i = 1; i <= (TOTAL - (x + 7)); i++) {
System.out.print(" ");
}
System.out.println("");
}
public static void main (String[] args){
TopTree(SEGMENTS, HEIGHT);
TreeBase();
}
}
Check your third for loop in TreeBase(), you increment the wrong variable. The code you posted runs fine for me, it just doesn't make that bottom line of asterisks due to that loop I mentioned not being correct. Once you fix that, you should be good
I should Write a program Square.java that declares and initalizes a variable n (with n ≥ 2) and prints out a two dimensional n-by-n triangular pattern as shown below (where n=6).
public class Square {
public static void main (String[]args){
for (int Line=1; Line<= 6; Line ++){
for (int n=1; n<= (Line-1*1); n++) {
System.out.print ("# ");
}
for (int n=1; n<= (7-Line); n++){
System.out.print ("$ ");
}
System.out.println ("");
}
}
}
I can't manage to write the code with n>=6 or n=2
I am guessing you are forgetting to update the 7 when you change the 6. Put your size in a variable as shown below. Seems to work for me for 2 and any other size
public static void main(String[] args) {
int size = 6;
for (int Line = 1; Line <= size; Line++) {
for (int n = 1; n <= (Line - 1 * 1); n++) {
System.out.print("# ");
}
for (int n = 1; n <= ((size + 1) - Line); n++) {
System.out.print("$ ");
}
System.out.println("");
}
}
The size of the square must be an attribut of the class
The result :
public class Square {
public int size;
public Square(int size) {
this.size = size;
}
public void goSquare(){
for (int line=1; line<= size; line++){
for (int n=1; n<= (line-1); n++) {
System.out.print ("# ");
}
for (int n=1; n<= ((size+1)-line); n++){
System.out.print ("$ ");
}
System.out.println ("");
}
}
public static void main (String[]args){
Square square = new Square(6);
square.goSquare();
}
}
In your actual code 7-Line is what blocks values >=6 and for value =2 your code works great but it only prints one element(point).
Another thing if you use Line-1*1 you mean (Line-1)*1 which is equivalent to write Line-1.
Also in 7-Line the value 7 should be an attribute to be used in the first loop too in Line <= 7, you can see it in this working code:
int max = 2;
for (int Line = 1; Line <= max; Line++) {
for (int n = 1; n <= (Line - 1); n++) {
System.out.print("# ");
}
for (int n = 1; n <= (max - Line); n++) {
System.out.print("$ ");
}
System.out.println("");
}
And this is an Ideone Working Example of the code.
I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern
1
121
12321
1234321
My code 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");
}
Your code prints only the suffix of each line, you are missing to write 12....i for each line.
In addition, the loop should start from i, not from rows-i+1.
for (int i = 1 ; i <= rows ; i++) {
//add an inner loop that prints the numbers 12..i
for (int j = 1 ; j < i ; j++ ) {
System.out.print(j);
}
//change where j starts from
for (int j = i ; j > 0 ; j-- ) {
System.out.print(j);
}
System.out.println(""); //to avoid inconsistency between different OS
}
First note that 11*11 = 121, 111*111=12321, etcetera.
Then that 10n - 1 is a number that consists of n 9's, so (10n - 1)/9 consists of n 1's.
So we get:
int powerOfTen = 1;
for (int len = 0; len < 5; len++)
{
powerOfTen = powerOfTen*10;
int ones = (powerOfTen-1)/9;
System.out.println(ones*ones);
}
Code explains everything!
public static void main(String[] args) {
String front = "";
String back = "";
int rows = 5;
for (int i = 1; i <= rows; i++) {
System.out.println(front+i+back);
front += i;
back = i + back;
}
}
Try this one: it may seems too much looping, but yet easy to understand and effective.
public static void main(String[] args) {
int rows=5;
int i,j;
for(i=1;i<=rows;i++)
{
/*print left side numbers form 1 to ...*/
for(j=1;j<i;j++)
{
System.out.printf("%d", j);
}
/*Print the middle number*/
System.out.printf("%d", i);
/*print right numbers form ... to 1*/
for(j=i-1;j>0;j--)
{
System.out.printf("%d", j);
}
System.out.println("");
}
}
int n=0;
for(int m =0; m<=5; m++){
for(n= 1;n<=m;n++){
System.out.print(n);
}
for(int u=n;u>=1;u--){
System.out.print(u);
}
System.out.print("");
}
I am trying to make a triangle of prime numbers .. the number of rows will be user defined
for example if user gives "4" as number of rows then the program should show a triangle of prime numbers containing 4 rows
input :4
output :
2
3 5
7 11 13
17 19 23 29
These are two tasks: get the list of primes (or just something like nextPrime(int)) and get the triangle, both are very simple.
For the implementation of first, you may look at Next Prime number Java only working with certain numbers
For the implementation of second, just do something like:
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
nextPrime = ...
System.out.print (nextPrime + " ");
}
System.out.println ();
}
Let say you already have your primes (sufficient amount).
int[] primes = ...;
Then for N strings create array is StringBuilders and fill them up with your numbers:
StringBuilder[] builders = new StringBuilder[N];
int count = 0;
for(int row = 0; row < N; row++) {
int size = row + 1;
builders[row] = new StringBuilder();
for(int i = 0; i < size; i++) {
builders[row].append(primes[count++]);
builders[row].append(' ');
}
}
And then you may print them
for(StringBuilder sb : builders)
System.out.println(sb);
//please try these
import java.util.*;
public class primePtt1
{
public static boolean isPrimeNumber(int num) {
int c=0;
for (int i = 1; i <= num; i++) {
if (num % i == 0)
c++;
}
if (c==2)
return true;
else
return false;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter no. of rows : ");
int rows = sc.nextInt();
int counter = 2;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
/* Try to find next prime number by incrementing counter and testing it for primality */
while(!isPrimeNumber(counter)){
counter++;
}
System.out.print(counter+" ");
counter++;
}
System.out.println();
}
}
}