Java output lines with each lines containing two string - java

I want to outpot the N lines with each lines containing either Mary or Jobert. I only know how to loop it but I don't know how it would stop based on the N number
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
int n= 5;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j < i; j++) {
System.out.println("Mary ");
}
for (int k = 1; k < i; k++) {
System.out.println("Jobert");
}
}
}
}
The output is
Mary
Jobert
Mary
Mary
Jobert
Jobert
Mary
Mary
Mary
Jobert
Jobert
Jobert
Mary
Mary
Mary
Mary
Jobert
Jobert
Jobert
Jobert
but what I want is that it would stop at 5 words since n is 5
Mary
Jobert
Mary
Mary
Jobert

Try below code :
class Test {
public static void main(String[] args) {
int n = 7;
int counter = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if(j > 1) {
counter++;
System.out.println("Mary ");
}
if(counter == n) {
break;
}
}
for (int k = 1; k < i; k++) {
if(k > 1) {
counter++;
System.out.println("Mary ");
}
if(counter == n) {
break;
}
}
}
}
}

you can try this. it will solve your problem.
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
int n= 5;
for (int i = 1; i <= 5; i=i+2) {
for (int j = 1; j < i; j=j+2) {
System.out.println("Mary ");
}
for (int k = 1; k < i; k=k+4) {
System.out.println("Jobert");
}
}
}
}

It can be done in this way using 2 loops only
int n=5;
int counter = 0;
for(int i = 1;i<=5;i++){
int twice = i*2;
for(int j = 1 ; j<=twice;j++){
if(counter == n)
return;
if(j<=twice/2) {
System.out.println("Mary");
counter++;
}
else{
System.out.println("Jobert");
counter++;
}
}
}

Related

I want to print the following format in java program for the pryamid

1**
2**
3***
4****
Till then I have got this snippet of code
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
You can print your index before the loop :
for (int i = 1; i <= 4; i++) {
System.out.print(i);//<<----------Print the index i
for (int j = 0; j < i; j++) {
System.out.print(i == 1 ? "**" : "*");//check if i == 1 then print 2 stars else 1
}
System.out.println("");
}
In case you mean 1* you can replace System.out.print(i == 1 ? "**" : "*"); with System.out.print("*");
you just need to add i index to print
here you go
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
system.out.println(i);
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
Java 8 simplifies it:
public class triangles {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
System.out.print(i);
System.out.println(String.join("", Collections.nCopies(i, "*")));
}
}
}

Can't print the pattern

I created the code that should print a pattern like
12345
2345
345
45
5
I have the code written below, the logic works fine in python but in java the output is different.
class Testing{
public static void main(String args[])
{
for (int i = 1; i<6;i++)
{
for (int j =0; j<i-1;j++)
{
System.out.print(" ");
}
while (i < 6){
System.out.print(k);
System.out.println();
i++;
}
}
}
}
The output is just 12345. I don't understand why does it iterate over first for loop for only once.
Use another variable for while control.
public class Testing {
public static void main(String args[]) {
int k;
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
k = i;
while (k < 6) {
System.out.print(k);
k++;
}
System.out.println();
}
}
}
You can see this in this link
this will show you :
12345
2345
345
45
5
Note: When 'while loop' increases. That increases i value bigger than 6. So next time it terminates the outer loop. That was your mistake.
package com.appointment.api;
class Testing {
public static void main(String args[]) {
for (int i = 1; i < 6; i++) {
System.out.println();
for (int j = 0; j < i - 1; j++) {
System.out.print(" ");
}
int x = i;
while (x < 5) {
System.out.print(i);
x++;
}
}
}
}
Following is a java-8 implementation of the problem:
IntStream.rangeClosed(1, MAX)
.forEach(i -> IntStream.rangeClosed(1, MAX)
.mapToObj(j -> j == MAX ? j + "\n" : j >= i ? j : " ")
.forEach(System.out::print)
);
Set MAX = 5 and it will print your pattern.
Output:
12345
2345
345
45
5

Using nested for loops to produce multiple lines of String output

I am learning Java for myself and have following problem:
Task: Write (explicitly) nested for loops to produce the following output:
5
5 5
5 5 5
5 5
5
My idea:
public class Exercises {
public static void main (String [] args) {
for (int line = 1; line <= 3; line++) {
for (int i = 1; i<= -2*line+6; i++){
System.out.print(" ");
}
System.out.println("5");
}
for ( int line = 4; line <= 5; line ++){
for (int i = 1; i <= 2*line-6; i++){
System.out.print(" ");
}
System.out.println("5");
}
}
}
My output:
5
5
5
5
5
I don't know how to get nested loops there. Can you give me not the answer, but some tips?
Try this solution:
/**
*
* #author Adil
*/
public class Exercises {
public static void main (String [] args) {
for (int i = 1; i <= 3; i++) {
for(int s = 4; s > i; s--) {
// add spacing
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
for (int i = 1; i <= 3; i++) {
for (int s = 1; s < i; s++) {
// add spacing
System.out.print(" ");
}
for (int j = 4; j > i; j--) {
//display/add star
System.out.print("5");
}
// add new line
System.out.println("");
}
}
}
It will produce the following output:
5
55
555
55
5
Try this solution give below:
public class Exercises {
public static void main(String[] args) {
int noOfColumns = Integer.parseInt(args[0]); // Enter no of 5's in last
// column
int noOfRows = noOfColumns;
int index = noOfColumns;
int mid = noOfRows / 2;
for (int i = 0; i < noOfRows; i++) {
if (i <= mid) {
index = index - 2;
} else {
index = index + 2;
}
for (int j = 0; j < noOfColumns; j++) {
if (j % 2 == 0 && j >= index) {
System.out.print("5");
} else {
System.out.print(" ");
}
} // End of inner loop
System.out.println("");
} // End of outer loop
}
}

How do I print this pattern in java?

I'm not able to print this pattern:-
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
I,ve already tried this:
class loop1
{
public static void main(String args[])
{
for(int i=1;i<10;i++)
{
for(int j=9;j<10;j++)
{ System.out.print(j);
j--;
}
System.out.println("");
}
}
}
but get an output of unlimited 9, a never ending loop.
try this
public static void main(String args[]) {
for (int i = 1; i < 10; i++) {
for (int j = i; j > 0; j--) {
System.out.print(10 - j);
}
System.out.println(0);
}
}
Try this its very simple
working example is here
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.insert(0, i);
System.out.println(s);
Result
90
890
7890
67890
567890
4567890
34567890
234567890
1234567890
for(int i=1;i<10;i++) {
for(int j=10-i;j<10;)
System.out.print(j++);
System.out.println("0");
}
Try this:
StringBuffer s = new StringBuffer("0");
for(int i = 9; i > 0; i--) {
s.append(i);
System.out.println(s);
}
Your outer loop is looping the wrong way; for a working solution, it should be going from 9 down to 1. Then, your inner for loop would be looping up to 9:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
System.out.print(j);
}
System.out.println(0);
}
The 0 can't be part of the loop because it doesn't fit with the 1-9 pattern. However, you could print the last digit of each number and cut the 0 part of the println statement:
for(int i = 9; i > 0; i--) {
for(int j = i; j < 10; j++) {
String number = j + "";
System.out.print(number.substring(number.length() - 1));
}
System.out.println();
}
Maybe i am late but i have to post this because of my love for loops
public class SeriesLoop {
public static void main(String a[]){
for(int i=9;i>=1;i--){
for(int j=i;j<=10;j++){
System.out.print(j%10);
}
System.out.println();
}
}
}
DEMO
Cheers

core java printing a butterfly structure

package mypack;
public class Loop {
public static void main(String[] args) {
for(int i=0;i<=8;i++){
for(int j=1;j<=i+1;j++){
System.out.print(j);
}
System.out.println();
}
for(int i=8;i>=1;i--){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
output::
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678
1234567
123456
12345
1234
123
12
1
i want the mirror image of it on right side.
can someone help me with this.
What you can do is to count the number of spaces before you add the reflection of the numbers to form a butterfly effect in the console, using the String.format
sample:
public static void main(String[] args) {
int j;
for (int i = 0; i <= 8; i++) {
for ( j = 1; j <= i + 1; j++) {
System.out.print(j);
}
System.out.print(String.format("%"+((10 - j)*2 != 0 ? (10 - j)*2 : "")+"s", ""));
for ( j = i + 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
for (int i = 8; i >= 1; i--) {
for ( j = 1; j <= i ; j++) {
System.out.print(j);
}
System.out.print(String.format("%"+((10 - j)*2 != 0 ? (10 - j)*2 : "")+"s", ""));
for ( j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
results:
1 1
12 21
123 321
1234 4321
12345 54321
123456 654321
1234567 7654321
12345678 87654321
123456789987654321
12345678 87654321
1234567 7654321
123456 654321
12345 54321
1234 4321
123 321
12 21
1 1
one easy way is to do:
public static void main(String[] args) {
int limit = 10;
String left = "%-9s";
String right = "%9s";
for (int i = 0; i < limit; i++) {
StringBuilder b = new StringBuilder();
for (int j = 1; j < i + 1; j++) {
b.append(j);
}
System.out.println(String.format(left, b.toString())
+ String.format(right, b.reverse().toString()));
}
// skipping the redundant line from last syso in above loop
for (int i = limit - 2; i > 0; i--) {
StringBuilder b = new StringBuilder();
for (int j = 1; j < i + 1; j++) {
b.append(j);
}
System.out.println(String.format(left, b.toString())
+ String.format(right, b.reverse().toString()));
}
}
idea is to print the sequence (left aligned) and then print its reverse(right aligned) in each iteration.
package mypack;
public class Loop {
public static void main(String[] args) {
for(int i=0;i<=6;i++){
for(int j=1 ;j<=i+1;j++ ){
System.out.print(j);
}
System.out.print("\t");
for (int j=0;j<6-i;j++)
System.out.print(" ");
for (int k=1;k<=i+1;k++)
System.out.print(k);
System.out.println();
}
for(int i=6;i>=0;i--){
for(int j=1;j<=i+1;j++){
System.out.print(j);
}
System.out.print("\t");
for (int j=0;j<6-i;j++)
System.out.print(" ");
for (int k=1;k<=i+1;k++)
System.out.print(k);
System.out.println();
}
}
}
got it by myself,thank you all..

Categories

Resources