I need help with my code issue. I have to write a program that displays a star pattern in a table format.
I am not exactly looking for the exact code, I would like to figure it out myself, so any suggestions and tips would be greatly helped.
// Pattern A Loop
for (int PatternA = 0; PatternA <= 9; PatternA++) { // outerLoop Pattern A
for (int PatternAI = 0; PatternAI <= PatternA; PatternAI++) { // Inner Loop
System.out.print("+");
}
System.out.println();
}
// Pattern B Loop
for (int PatternB = 0; PatternB <= 10; PatternB++) { // outer loop Pattern B
for (int PatternBI = 9; PatternBI >= PatternB; PatternBI--) { //Inner Loop
System.out.print("+");
}
System.out.println();
}
Since you said you don't want a full solution, here are some tips.
First, since your table will have to print material from both PatternAI and PatternBI on the same line, you should move those loops together. This will involve making the outer loop work for both. You can use more than one variable in a for loop:
for (int i = 0, j = 0; i < 10 && j < 2; i++, j++)
You also need some way to separate the patterns. You can use spaces, but they will vary in number (in fact the same way the +'s do, so you can use that). Tabs also work and are a bit simpler. You will have to change between the number of tabs you use when the line gets a certain length however.
That's about all there is to it. Let me know if my hints were helpful, or if there is a better way to phrase them.
Here's the full code, if you get stuck:
// Pattern is used for both PatternA and PatternB in this outer loop
// Outer Loop
for (int Pattern = 0; Pattern <= 9; Pattern++) {
// Inner Loop PatternA
for (int PatternAI = 0; PatternAI <= Pattern; PatternAI++) {
System.out.print("+");
}
if (Pattern < 7)
System.out.print("\t\t");
else
System.out.print("\t");
// Inner Loop PatternB
for (int PatternBI = 9; PatternBI >= Pattern; PatternBI--) {
System.out.print("+");
}
System.out.println();
}
The outer loop passes through the lines of the pattern and the inner loop passes through the elements of these lines. To combine multiple lines into one, you have to combine multiple inner loops within one outer loop.
Try it online!
For example, take these patterns:
Multiple inner loops within one outer loop:
int n = 5;
for (int i = -n; i <= n; i++) {
System.out.print(i == -n ? "a) " : " ");
for (int j = -n; j <= n; j++) {
if (i + j <= 0)
System.out.print("*");
else
System.out.print("-");
}
System.out.print(i == -n ? " b) " : " ");
for (int j = -n; j <= n; j++) {
if (i + j >= 0)
System.out.print("*");
else
System.out.print("-");
}
System.out.print(i == -n ? " c) " : " ");
for (int j = -n; j <= n; j++) {
if (i <= j)
System.out.print("*");
else
System.out.print("-");
}
System.out.print(i == -n ? " d) " : " ");
for (int j = -n; j <= n; j++) {
if (Math.abs(i) + Math.abs(j) <= n)
System.out.print("*");
else
System.out.print("-");
}
System.out.println();
}
Output:
a) *********** b) ----------* c) *********** d) -----*-----
**********- ---------** -********** ----***----
*********-- --------*** --********* ---*****---
********--- -------**** ---******** --*******--
*******---- ------***** ----******* -*********-
******----- -----****** -----****** ***********
*****------ ----******* ------***** -*********-
****------- ---******** -------**** --*******--
***-------- --********* --------*** ---*****---
**--------- -********** ---------** ----***----
*---------- *********** ----------* -----*-----
See also: How to print ASCII patterns in C# but using Java syntax?
Related
I'm a beginner at Java.
I was solving nested for loop questions... Then this question came up. After research and re-tries I couldn't get my head around it. It must be solved using only nested for loops.
This is what the question wants my code to output:
-----1-----
----333----
---55555---
--7777777--
-999999999-
This is as close as I got:
---------1
-------333
-----55555
---7777777
-999999999
This is my code:
for (int line = 1; line <= 9; line+=2) {
for (int j = 1; j <= (-1 * line + 10); j++) {
System.out.print("-");
}
for (int k = 1; k <= line; k++) {
System.out.print(line);
}
System.out.println();
}
You just need to add another for loop to print - on the right side.
Also now the first and the third loop will execute for half the number of times
for (int line = 1; line <= 9; line+=2) {
for (int j = 0; j <= (-1 * line + 10) / 2; j++) {
System.out.print("-");
}
for (int k = 1; k <= line; k++) {
System.out.print(line);
}
for (int j = 0; j <= (-1 * line + 10) / 2; j++) {
System.out.print("-");
}
System.out.println();
}
I am trying to make the following number line is Java
2,3,5,7,11,13,17 (Prime Numbers)
I tried this code
for(int i =0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < 1; j++) {
if(i % j == 0) {
break;
} else {
System.out.print(i + ",");
}
}
}
But it doesn't work
Anyone help please?
Your code is quite poor, but the minimal amount of changes needed to make it work yields this code:
outerLoop:
for(int i = 0; i <= 100; i++) {
if(i < 2) {
continue;
}
for(int j = 2; j < i; j++) {
if(i % j == 0) {
continue outerLoop;
}
}
System.out.print(i + ",");
}
But a further improvement would be to start the first loop at 2 right away:
outerLoop:
for(int i = 2; i <= 100; i++) {
for(int j = 2; j < i; j++) {
// and so on...
EDITED
There are a lot of errors in that code, first of all that second loop is a infinite loop and second one that the System.out.println line should not be in second loop it should be at end of first loop! If you place it in second it will print numbers hundreds of time.
This is the correct code :
for(int i = 2; i <= 100; i++)//begin loop from 2 instead of 0
{
boolean flag = true;
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = false;
break;
}
}
if(flag)System.out.print(i + ",");
}
You need to set a flag to check if a factor was found outside the loop.
I am trying to make my code print out the Asterisk in the image, you see below. The Asterisk are align to the right and they have blank spaces under them. I can't figure out, how to make it go to the right. Here is my code:
public class Assn4 {
public static void main(String[] args) {
for (int i = 0; i <= 3; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int x = 0; x <= 1; x++) {
System.out.println(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
}
System.out.println();
}
}
Matrix problems are really helpful to understand loops..
Understanding of your problem:
1) First, printing star at the end- That means your first loop should be in decreasing order
for(int i =7;i>=0; i+=i-2)
2) Printing star in increasing order- That means your second loop should be in increasing order
for(int j =0;j<=7; j++)
Complete code:
for(int i =7;i>=0; i=i-2){ // i=i-2 because *s are getting incremented by 2
for(int j =0;j<=7; j++){
if(j>=i){ // if j >= i then print * else space(" ")
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();// a new line just after printing *s
}
Starting loops with 1 can sometimes help you visualize better.
int stopAt = 7;
for (int i = 1; i <= stopAt ; i += 2) {
for (int j = 1; j <= stopAt; j++) {
System.out.print(j <= stopAt - i ? " " : "*");
}
System.out.println();
}
Notice, how each row prints an odd number of *s ending at the line with 7. So, you start with i at 1 and go through 3 1+2, 5 3+2, and then stopAt 7 5+2.
The nested for loop has to print 7 characters always to make sure *s appear right aligned. So, the loop runs from 1 to 7.
Here the complete code:
for(int i = 0; i < 8; i++){
if( i%2 != 0){
for(int x = 0; x < i; x++){
System.out.print("*");
}
}else{
System.out.println();
}
}
i want to print a triangle/pyramid style like:
1
323
54345
7654567
here is my code:
int lines = 5;
for (int i = 1; i < lines; i++) {
for (int j = 1; j < lines-i; j++) {
System.out.print(" ");
}
for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
for (int j = i; j < i+i; j++) {
System.out.print(j);
}
System.out.println();
}
what i got is
1
223
32345
4324567
i been studying codes while working at office and i think week long i still could not find a solution to this even i use search in Google.
i am only into enhancing my logic through conditionals and no heavy object oriented or recursion yet.
The problem in your first loop is a problem you figured out in your second one! (and it has something to do with the largest number in the loop)
for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
Look at the numbers on the left of the pyramid. They start where the ones on the right end (every line of the pyramid is symmetrical). And the general formula for that number is i + i - 1, where i is the line number from your outer loop.
The second row starts at 2 * i - 1 = 2 * 2 - 1 = 3. The third row starts at 2 * 3 - 1 = 5 etc.
Your second inner loop should therefore look like this:
for (int j = i + i - 1; j > i; j--) {
System.out.print(j);
}
Here is the complete fixed source.
You have to start at the i-th odd number. This is i*2-1. And you stop at i. This also fixes a spacing difference introduced by changing it to lines = 4.
int lines = 4;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j < lines-i+1; j++) {
System.out.print(" ");
}
for (int j = i*2-1; j > i; j--) { //this for loop is my problem. any solution?
System.out.print(j);
}
for (int j = i; j < i+i; j++) {
System.out.print(j);
}
System.out.println();
}
Run it here: http://ideone.com/AKsc1f
int lines = 4;
for (int i = 1; i <= lines; i++) {
for (int j = 1; j < lines-i+1; j++) {
System.out.print(" ");
}
//replace this
for(int j=0; j<i-1; j++) System.out.print(i*2-j-1);
System.out.print(i);
for(int j=; j<i-1;j++) System.out.print(i+j+1);
//==========
System.out.println();
}
You have an input of n and that represents half the rows that the diamond will have. I was able to make the first half of the diamond but I'm EXTREMELY frustrated with the second half. I just can't seem to get it. I'm not here to ask for specific code I need, but can you point me in the right direction and give me some tips/tricks on how to write this? Also, if I'm going about this program the wrong way, feel free to tell me and tell me on how I should approach the program.
The diamonds at the bottom represent an input of 5. n-1 represents the spaces to the left of each asterisk. Thank you for your help!
public static void printDiamond(int n)
{
for(int i=0;i<n;i++)
{
for(int a=0;a<(n-(i+1));a++)
{
System.out.print(" ");
}
System.out.print("*");
for(int b=0; b<(i*2);b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
}
** What I need ** What I have currently
*--* *--*
*----* *----*
*------* *------*
*--------* *--------*
*--------*
*------*
*----*
*--*
**
public static void main(String[] args) {
int n = 10;
for (int i = 1 ; i < n ; i += 2) {
for (int j = 0 ; j < n - 1 - i / 2 ; j++)
System.out.print(" ");
for (int j = 0 ; j < i ; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7 ; i > 0 ; i -= 2) {
for (int j = 0 ; j < 9 - i / 2 ; j++)
System.out.print(" ");
for (int j = 0 ; j < i ; j++)
System.out.print("*");
System.out.print("\n");
}
}
output
*
***
*****
*******
*********
*******
*****
***
*
Just reverse your loop :
for(int i=n-1;i>=0;i--)
{
for(int a=0;a<(n-(i+1));a++)
{
System.out.print(" ");
}
System.out.print("*");
for(int b=0; b<(i*2);b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
Since you have half the diamond already formed, simply run the loop again, in reverse, eg:
public static void printDiamond(int n)
{
for (int i = 0; i < n; i++)
{
for (int a = 0; a < (n - (i + 1)); a++)
{
System.out.print(" ");
}
System.out.print("*");
for (int b = 0; b < (i * 2); b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
for (int i = n-1; i >= 0; i--)
{
for (int a = 0; a < (n - (i + 1)); a++)
{
System.out.print(" ");
}
System.out.print("*");
for (int b = 0; b < (i * 2); b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
}
Whenever I see a symmetry of a kind, recursions ring to my head. I'm posting only for you and others interesting into learning more. When beginning, recursions can harder to grasp but since you already have loop based solutions, contrasting against recursion will clearly outline advantages and disadvantages. My advice, don't miss out on the chance to get into it :)
A recursive solution:
static int iteration = 0;
public static void printDiamond(int n) {
int numberOfBlanks = n - iteration;
int numberOfDashes = iteration * 2;
String blank = new String(new char[numberOfBlanks]).replace("\0", " ");
String dash = new String(new char[numberOfDashes]).replace("\0", "-");
String star = "*";
String row = blank + star + dash + star + blank;
// printing the rows forward
System.out.println(row);
iteration++;
if (iteration < n) {
printDiamond(n);
}
// printing the rows backward
System.out.println(row);
}
first, don't get confused with the strange new String(new char[numberOfBlanks]).replace("\0", " "); its a neat trick in java to construct a string with repeated chars, eg new String(new char[5]).replace("\0", "+"); would create the following String +++++
The recursion bit explained. The second println won't run until the recursion is stopped. The stop criteria is defined by iteration < n. Up until that point the rows up until that point will be printed. So something like this:
iteration 1. row = **
iteration 2. row = *--*
iteration 3. row = *----*
iteration 4. row = *------*
iteration 5. row = *--------*
than the recursion stops, and the rest of the code is executed but in reversed order. So only the second println is printed, and the value of row variable is like as follows
continuing after 5 iteration row = *--------*
continuing after 4 iteration row = *------*
continuing after 3 iteration row = *----*
continuing after 2 iteration row = *--*
continuing after 1 iteration row = **
I didn't go into mechanics behind it, plenty of resource, this is just to get you intrigued. Hope it helps, best