I created a nested loop using For, here is the program code and the ouput, then I tried the while loop and get the different result:
For:
public class ForBersarang {
public static void main(String[] args){
int a = 5;
for(int i = 0; i<=a; i++){
for(int j = 0; j<=i; j++){
System.out.print("*");
}
System.out.println("");
}
}
While
public class WhileBersarang {
public static void main(String[] args){
int i = 0;
int a = 5;
int j = 0;
while (i<=a) {
while (j <= i) {
System.out.print("*");
j++;
}
i++;
System.out.println("");
}
}
Your problem is where you define j:
public class MyClass {
public static void main(String args[]) {
int i = 0;
int a = 5;
while (i<=a) {
//here
int j = 0;
while (j <= i) {
System.out.print("*");
j++;
}
i++;
System.out.println("");
}
}
}
Related
I can make this:
for (int i=0; i<6; i++){
for (int j=0; j<i; j++){
System.out.print("*");
}
System.out.println("");
}
but I cannot find out how to write a static void method that receives a positive integer and uses nested for-loops to display a right triangle made up of integers 1 to the number received
you can create static private method with size parameter and call from main method like below -
public static void main(String[] args) {
makeTriangle(6);
}
private static void makeTriangle(int size){
for (int i=0; i<size; i++){
for (int j=0; j<i; j++){
System.out.print("*");
}
System.out.println("");
}
}
Just define static method like this:
public class Test {
public static void printRightTriangle(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
printRightTriangle(12);
}
}
Hope it can help.
I think you're looking to print a right-angled triangle made up of integers in a method that takes a positive int.
public static void printTriangle(int maxVal) {
for (int i=1; i<=maxVal; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println("");
}
Try this way:
public static void main(String[] args) {
printRightTriangle(6);
}
public static void printRightTriangle(int rows) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < i; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
I am trying to output a triangle like this one:
19283765
2837465
37465
465
5
However, my actual output looks like this instead:
98765
8765
765
65
5
public class JavaNumber2 {
public static void main(String[] args) {
int r = 9;
for (int g = 9; g <= r; g--) {
for (int j = g; j >= 5; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
How can I modify this so that I get the first output instead?
The condition in the first for loop is wrong g<=r should be g>=0, and one if condition to print in reverse order.
public static void main(String[] args) {
int r = 9;
for (int g = 9; g >=0; g--) {
for (int j = g; j >= 5; j--) {
if(g==9) {
if(j>6) {
System.out.print(r+1-j);
}
}else {
if(j>5) {
System.out.print(r+1-j);
}
}
System.out.print(j);
}
System.out.println();
}
}
I have this answer:
public class T2Tree {
public static void main(String[] args) {
for (int i = 5; i >0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(j+1+5-i);
if (j<i-1) {
System.out.print(9-j+i-5);
}
}
System.out.println();
}
}
}
Or you can use a recursion (works with any start and end as well):
class Ideone
{
public static void main (String[] args)
{
int left = 1;
int right = 9;
while(right >= left){
r(left, right);
System.out.println();
left++;
right--;
}
}
private static void r(int left, int right){
System.out.print(left);
if(left != right){
System.out.print(right);
}
if(right > left){
r(left + 1, right - 1);
}
}
}
I am building a Java Array where the odd columns will output the #1 and the even columns will output the #0. Here is what i have so far. I'm certain my mistake is trivial but if you could help it would be greatly appreciated!
import java.util.Scanner;
public class TestArray2 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int [][] a = new int[5][5];
for(int i=0; i<a.length;i++){
for(int j=0;j<a[0].length;j++){
int x = j;
if(x%2 == 0){
a[i][j] = 0;
}
else {
a[i][j] = 1;
}
}
input.close();
}
}
public class Array2 {
public static void printArray(int[][]a){
for(int i=0;i<a.length;i++){
for(int j=0; j<a[0].length;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
Array2.printArray(a);
}
}
Array Code
This should work, I skipped the second class
public class TestArray2 {
public static void main(String[] args){
int [][] a = new int[5][5];
for(int i=0; i<a.length;i++){
for(int j=0;j<a[i].length;j++){
int x = j;
if(x%2 == 0){
a[i][j] = 0;
}
else {
a[i][j] = 1;
}
}
}
printArray(a);
}
public static void printArray(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
Actually you have two different problems. The first one is that you are using a matrix not an array. The second one is that you are trying to call the method on the second class when it does not have the a variable defined for that.
Try the following:
import java.util.Scanner;
public class TestArray2 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int [] a = new int[input];
for(int i=0; i<a.length;i++){
if(i%2 == 0){
a[i] = 0;
} else {
a[i] = 1;
}
}
input.close();
printArray(a);
}
public static void printArray(int[]a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
I am working on a simple star pattern program in Java. I have the code running but it is not doing what it's supposed to. My code is:
public class q3 {
public static void main(String[] args) {
for (int i = 10; i >= 1; i--){
for (int j = i; j >= 1; j--){
System.out.print("*");
}
System.out.println("");
}
}
}
Output:
$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$
What I want is something like this below:
$
$$
$$$
$$$$
$$$$$
..........
$$$$$$$$$$
Can someone please help me figure out how I would get the above pattern. Thank You.
Try this :
public static void main(String[] args) {
final int length = 10;
for (int i = 1; i < length; i++) {
//Print spaces first
for (int j = length - 1; j > i; j--) {
System.out.print(" ");
}
//Then print "*"
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Output is for length = 10 :
*
**
***
****
*****
******
*******
********
We can achieve this pattern using a single for loop:
char [] mirrorascval = new char[5];
for (int i = 4; i >= 0; i--) {
mirrorascval[i] ='*';
System.out.println(mirrorascval);
}
class StarTriangle {
public static void main(String[] args) {
int i, j, k;
for (i = 8; i >= 1; i--) {
for (j = 1; j < i; j++) {
System.out.print(" ");
}
for (k = 8; k >= i; k--) {
System.out.print("*");
}
System.out.println();
}
}
}
public void starPrint(){
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
import java.util.*;
public class firstStar {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner S=new Scanner(System.in);
int n=S.nextInt();
int i=1;
while(i<=n) {
int j=1;
while(j<=i) {
System.out.print("*");
j=j+1;
}
System.out.println();
i=i+1;
}
}
}
package star.pattern;
import java.util.Scanner;
public class StarPattern {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int l;
System.out.print("Enter No Of Line You Want:- ");
l=scn.nextInt();
int x=1;
while(x<=l){
int j=x;
while(j<l){
System.out.print(" ");
j++;
}
int y=1;
while(y<=x){
System.out.print("*");
y++;
}
System.out.println("");
x++;
}
}
}
public class q3 {
public static void main(String[] args) {
for (int i = 1; i <=10; i++){
for (int j = 10; j >= i; j--){
System.out.print("*");
}
System.out.println("");
}
}
}
Try this...
int i, j, k=8;
for(i=0; i<5; i++)
{
for(j=0; j<k; j++)
{
System.out.print(" ");
}
k = k - 2;
for(j=0; j<=i; j++)
{
System.out.print("* ");
}
System.out.println();
}
I am trying to make a christmas tree using for loops and nested for loops. For me to do that I need to be able to make a pyramids with *. I have tried countless times and I am having problems making one. Here is my code:
for(int i=1;i<=10;i++){
for(int j=10;j>i;j--){
System.out.println(" ");
}
for(int k=1;k<=i;k++){
System.out.print("*");
}
for(int l=10;l<=1;l++){
for(int h=1;h<=10;h++){
System.out.print(" ");
}
}
System.out.println();
}
What I am trying to do is:
*
***
*****
*******
Try this much simpler code:
public class ChristmasTree {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10 - i; j++)
System.out.print(" ");
for (int k = 0; k < (2 * i + 1); k++)
System.out.print("*");
System.out.println();
}
}
}
It uses 3 loops:
first one for the number of rows,
second one for printing the spaces,
third one for printing the asterisks.
You can do it with simple logic
for (int i = 0; i < 4; i++)
System.out.println(" *******".substring(i, 4 + 2*i));
import java.util.Scanner;
public class cmastree{
public static void main (String[]args){
Scanner keyboard=new Scanner (System.in);
int j;
System.out.println ("Enter a number");
j=keyboard.nextInt();
/*take the above part out and change the j variable if you want to set
the size*/
for(int i=1; i<=j; i+=2){
int numSpaces = (j-i)/2;
for (int k=0; k<numSpaces; k++){
System.out.print(" ");
}
for(int k=0; k<numSpaces; k++){
System.out.print("*");
}
System.out.println();
}
}
}
public class ChrismasTree {
public static void main(String[] args) {
int sizeOfTree = 9;
double remainderVal = sizeOfTree % 2 ;
double ans = sizeOfTree / 2 ;
if (remainderVal == 0) {
System.out.println("Invalid number enter 9,19 calculat rest yourself u looser ..");
System.exit(0);
}
int middlePos = (int) Math.round(ans + .5);
for (int i = 0; i <= sizeOfTree; i++) {
int lStar = middlePos - i;
int rStar = middlePos + i;
if (lStar < 1) {
break;
}
printleaves(lStar, rStar, sizeOfTree);
}
}
public static void printleaves(int a,int b, int size){
System.out.println();
for (int i = 1; i <= size; i++) {
if (i > a && i < b ){
System.out.print("*");
}else System.out.print(" ");
}
}
}
public class Stars {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter Row/Column Value::");
int i,j,k,n;
n=s.nextInt();
for(i=1; i<n; i++){
for(j=n+(n/2); j>i; j--){
System.out.print(" ");}
for(k=1; k<=2*i-1; k++){
System.out.print("*");}
System.out.println("");
}
for(i=1; i<n+(n/2); i++){
for(j=n+(n/2); j>i; j--){
System.out.print(" ");}
for(k=1; k<=2*i-1; k++){
System.out.print("*");}
System.out.println("");
}
for(i=1; i<n-(n/2); i++){
for(j=n+(n/2); j>1; j--){
System.out.print(" ");}
for(k=n/2; k<=(n/2)+1; k++){
System.out.print("*");}
System.out.println("");
}
}
}
public class ChristmasTree {
public static void printStars(int number) {
for (int i = 1; i <= number; i++) {
System.out.print("*");
}
System.out.println("");
}
public static void printSpaces(int number) {
for (int i = 0; i < number; i++) {
System.out.print(" ");
}
}
public static void christmasTree(int height) {
for (int i = 1; i <= height; i++) {
printSpaces(height - i);
printStars(i + (i - 1));
}
}
public static void main(String[] args) {
// int x = pick some number, but not TOO big )))
christmasTree(x);
}
}
def fist(n)
k=2*n-2
for i in range(0,n):
for j in range(0,k):
k=k-1
print(end=" ')
for j in range(0,i+1):
print("*",end=" ")
print()
def second(n)
k=2*n-2
for i in range(0,n):
for j in range(0,k):
k=k-1
print(end=" ')
for j in range(0,i+1):
print("*",end=" ")
print()
def stem(m)
k=11
for i in range(0,5):
for j in range(0,k):
print(end=" ")
for j in range(0,3):
print("*",end=" ")
print()
first(7)
second(7)
steam(3)