I want to create a pattern in java that looks something like this->
*
**
***
****
*****
****
***
**
*
(the number of stars depend on number entered by user.)
I know the first half half (increasing order), but please tell me how to make the rest half (decreasing order).
Thanks.
Here is my code for first part->
int y = Integer.parseInt(jTextField1.getText());
for (x = 0; x <= y; x++) {
jTextField2.setText(jTextField2.getText() + "*");
jTextArea1.append(jTextField2.getText() + "\n");
}
for decreasing order
for (x = y; x >= 0; x--) {
jTextField2.setText(jTextField2.getText() + "*");
jTextArea1.append(jTextField2.getText() + "\n");
}
You could use something like that:
int y = Integer.parseInt(jTextField1.getText());
for(x = y; x>=0; x--){
String s = "";
for(int i = 0; i < x ; i++) {
s += "*";
}
jTextField2.setText(s);
jTextArea1.append(jTextField2.getText()+"\n");
}
Note that if performance matters, you'd use a StringBuffer.
I assume you don't want to use external libraries. Guava's Strings.repeat woul be a better solution for your problem.
If you can use Guava, you could use this code:
int y = Integer.parseInt(jTextField1.getText());
for(x = 0; x<=y; x++){
String s = String.repeat("*", x);
jTextArea1.append(s+"\n");
}
for(x = y; x>=0; x--){
String s = String.repeat("*", x);
jTextArea1.append(s+"\n");
}
This solution is more straightforward, but it does require an external library
Try this code, i hope this will be use ful
int v = 1;
int totStars = 20;
for (int i = 1; i >= 0; i+=v) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
if(i >= totStars){
v = -1;
}
}
Here is a recursive solution which is worth taking the time to figure out how it works:
public void tristar(int n) {
tristar(n, "");
}
public void tristar(int n, String stars) {
if (n == 1) {
System.out.println(stars + "*");
} else {
System.out.println(stars + "*");
tristar(n-1, stars + "*");
System.out.println(stars + "*");
}
}
Hlo. Buddy.. try this code
int y = Integer.parseInt(jTextField1.getText());
for (x = 0,z=0; x <= (y+y-1); x++) {
if(x<=y){
z++;
}else{
z--;
}
for(int i=0;i<=z;i++){
jTextField2.setText(jTextField2.getText() + "*");
}
jTextArea1.append(jTextField2.getText() + "\n");
}
This will print the complete pattern
Related
I'm working on a couple of Project Euler problems and want to test my solution. My recursive function never ends even with reachable base cases.
in a 20x20 grid I am using x and y coordinates to navigate up and left to find the number of paths from (19,19) to (0,0). My base case is to return 1 when we reach (0,0). Otherwise I add the current count to the recursive call.
Function:
private static int numPaths(int x, int y, int pathsFound)
{
if(x == 0 && y == 0)
return 1;
else
{
if(x > 0)
{
pathsFound += numPaths(x - 1, y, pathsFound);
}
if(y > 0)
{
pathsFound += numPaths(x, y - 1, pathsFound);
}
}
return pathsFound;
}
Main:
int x = 19;
int y = 19;
System.out.println("Answer: " + numPaths(x, y, 0));
Is there a flaw in my recursive logic, or is just taking a very long time to compute? If you know the solution to this Euler problem, please do not post it.
https://projecteuler.net/problem=15
So if anyone is interested, I looked into memoization and came up with an elegant solution without recursion.
Function:
private static BigInteger numberPaths(ArrayList<ArrayList<BigInteger>> grid)
{
for(int i = 0; i <= 20; ++i)
{
for(int j = 0; j <= 20; ++j)
{
int x = j;
int y = i;
if(x - 1 < 0 || y - 1 < 0)
{
grid.get(x).set(y, BigInteger.ONE);
}
else
{
BigInteger topVal = grid.get(x - 1).get(y);
BigInteger leftVal = grid.get(x).get(y - 1);
grid.get(x).set(y, topVal.add(leftVal));
}
}
}
return grid.get(20).get(20); //the solution
}
Main:
ArrayList<ArrayList<BigInteger>> grid = new ArrayList<>();
for(int i = 0; i <= 20; ++i)
{
ArrayList<BigInteger> column = new ArrayList<>();
for(int j = 0; j <= 20; ++j)
{
column.add(BigInteger.valueOf(0));
}
grid.add(column);
}
System.out.println("Answer: " + numberPaths(grid));
Can't figure it out. Need to use a for/while statement. First month learning Java so please keep it simple. Thanks
This is what I have so far:
public class prog6c
{
public static void main (String [] args)
{
int x = 1;
while (x<5000){
for (x=1; x<5; ++x)
System.out.print(" "+x);
}
}
}
Your problem is that after going through the for loop for the first time x<=5. But the next time you are setting it back to 1.
I would suggest a loop until 5000 which prints a line break when x can be divide by 5
for (x = 1; x < 5000; ++x)
{
System.out.print(" ");
System.out.print(x);
if (x % 5 == 0)
{
System.out.println();
}
}
If I am understanding the purpose of what you are trying to achieve, check out the following code sample. I haven't tested it out. But it should work on most part:
public class Prog6c {
public static void main(String[] args) {
int x = 1;
int y = 1;
while (x<5000){
for (y=1; y<=5; ++y)
System.out.print(x++ + " ");
System.out.print("\n");
}
}
}
Try doing:
int x = 1;
int columns = 1;
while(x < 5000){
for(x = 1; x < 5; x++){
while(columns < 5){
if(columns ==){
columns == 0
System.out.print("\n")
}
columns++;
System.out.print(" " + x);
}
}
}
This code count by 0 to 5000 aligning in 5 columns per row.
Frank.
I am trying to find the first 100 pentagonal numbers using methods. Below is my code, however, I keep getting missing return statement. I sure that I am not placing return statement at the appropriately, besides I wouldn't know if the process is right. I would therefore appreciate guidance. Thank you.
PS: yet to learn arrays and this is no homework
public class PentagonNumber {
public static void main(String[] args) {
int numberperline = 10;
int n = 1;
int count = 0;
int pent = getpentagonnumber(n);
count++;
if (count % numberperline == 0)
System.out.println();
else System.out.print(pent + "\t");
}
public static int getpentagonnumber(int x) {
for (int count = 0; count < 100; count++) {
for (x = 1; x <= 100; x++) {
int result;
result = x * (3 * x - 1) / 2;
return result;
}
}
}
}
Your code should be changed like this:
public class PentagonNumber {
public static void main(String[] args) {
int numberperline = 10;
//int n = 1; // you do not need N
//int count = 0; // you do not this either
for (x = 0; x < 100; x++) { // moved
int pent = getpentagonnumber(x+1); // +1 so it goes 1::100
//count++;
if (x % numberperline == 0)
System.out.println();
//else // you were skipping every tenth result.
System.out.print(pent + "\t");
}// close for
}
public static int getpentagonnumber(int x) {
//for (int count = 0; count < 100; count++) { // moved
//for (x = 1; x <= 100; x++) { // removed
int result; // no need to declare and then calculate
result = x * (3 * x - 1) / 2; // but not wrong either.
return result;
//}
//}
}
}
You only have a return value inside a for loop. You probably meant for it to be:
public static int getpentagonnumber(int x ){
int result = 0;
for (int count=0; count<100; count++){
for (x=1;x<=100;x++){
result = x*(3*x-1)/2;
}
} //this was missing
return result;
}
I'm still working on my Java Diamond Problem.
Here is the current state of the diamond
I'm thinking the problem is the for loop right here,
all I need is for those spaces to be printed and then I'm finished.
Can anyone see any obvious reasons why the for loop I labeled with the problem isn't being entered or running?
//Bottom half of the diamond
int middleSpaces = sides + 2;
int downPreSpaces = 1;
int dRows = sides + 1;
for (int x = 1; x <= dRows; x++) {
if (x >= dRows) {
for (int z = 1; z <= sides + 1; z++) {
System.out.print(" ");
}
System.out.print("v");
}
if (x != dRows) {
for(int y = 1; y <= x; y++) {
System.out.print(" ");
}
System.out.print("\\");
//PROBLEM IS HERE
for (int e = middleSpaces - 2; e <= 0; e += 2) {
System.out.print(" ");
}
System.out.print("/\n");
}
Step through your code with a debugger and examine the state of the variables e and dRows at the location that is causing problems.
You were not decrementing middleSpaces also it was not initialized properly:
int middleSpaces = sides * 2; //NOTICE
int downPreSpaces = 1;
int dRows = sides + 1;
for (int x = 1; x <= dRows; x++) {
if (x >= dRows) {
for (int z = 1; z <= sides + 1; z++) {
System.out.print(" ");
}
System.out.print("v");
}
if (x != dRows) {
for(int y = 1; y <= x; y++) {
System.out.print(" ");
}
System.out.print("\\");
for (int e = middleSpaces - 2; e >= 0; e -= 1) {
System.out.print(" ");
}
middleSpaces-=2; //NOTICE
System.out.print("/\n");
}
middleSpaces - 2 might be less than dRows, or else one of the outer loops doesn't work.
Notice that the values of dRows, middleSpaces and dmSpaces do not change in the code you've shown. This means the inner loop in question will always print the same number of spaces.
You probably mean something like the following:
for(int e = middleSpaces - 2; e >= x; e -= 2) {
System.out.print(" ");
}
That might be off by one or something but is closer to the result you're trying to get.
Goal:
1234
2345
3456
4567
5678
i have the pattern down but it doesnt println after length(4):
int i;
int a;
for (i = 1; i <= 5; i++)
{
for (a = i;a<=i+3;a++)
{
System.out.print(a);
}
}
My output is: 12342345345645675678
Just add it after the second loop:
int i;
int a;
for (i = 1; i <= 5; i++) {
for (a = i;a<=i+3;a++) {
System.out.print(a);
}
System.out.println();
}
int i;
int a;
for (i = 1; i <= 5; i++)
{
for (a = i;a<=i+3;a++)
{
System.out.print(a);
}
System.out.println(); // add this code
{
No need to have two for loops, try :
for (i = 1; i <= 5; i++) {
int j = i;
System.out.println(j++ + "" + j++ + "" + j++ + "" + j);
}
EDIT : I know this will limit the flexibility, but this is just a toy problem.
int i;
int a;
for (i = 1; i <= 5; i++)
{
for (a = i;a<=i+3;a++)
{
System.out.print(a);
}
System.out.println();
}
Add System.out.println() after the inner loop.
Try:
int i;
int a;
for (i = 1; i <= 5; i++) {
for (a = i;a<=i+3;a++) {
System.out.print(a);
}
System.out.println(); // this will print a new line.
}
Add System.out.Println() after the inner loop. This will move the cursor to next line
Java Solution
int f, g, h,T;
f = 12345;
h = 11111;
for (g = 1; g <= 5; g++)
{
T = f + ((g - 1) * h);
System.out.print( T + "\n")
}
for(int i =1;i<=4;i++) {
System.out.print(i);
}
System.out.println();
for(int i =2;i<=5;i++) {
System.out.print(i);
}
System.out.println();
for(int i =3;i<=6;i++) {
System.out.print(i);
}
System.out.println();
for(int i =4;i<=7;i++) {
System.out.print(i);
}
System.out.println();