I want to create a two dimensional array. I am able to compile but not able to run
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; k++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
You have an endless loop: for(j=0;j<5;k++), you have to write for(j=0;j<5;j++)
You increment k instead of j
You have an endless loop. You are incrementing k instead of j:
for(j=0;j<5;k++)
You should change it both times to
for(j=0;j<5;j++)
Here... this should work. Just change your sub-loops making it j++ instead of k++ both top and bottom
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
}
I think you've mixed up the k and j variables in the second for-loop "block". When I alter it to:
...
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(test[i][j] + " ");
System.out.println();
}
...
I get the following printed to my console:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
Is it what you wanted?
public class Arraytest1 {
public static void main(String[] args) {
int i, j, k = 0;
int test[][] = new int[4][5];
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
test[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++) {
System.out.print(test[i][j] + " ");
System.out.println();
}
}
}
}
you can resolve this problem
Related
I'm currently trying to get this output using nested loops:
For the life of me I cannot figure out how to get the # signs to increment by +2 each time. Any help would be greatly appreciated!
public class PrintPatterns {
public static void main(String[] args) {
pattern1();
}
private static void pattern1() {
for (int i = 1; i <= 10; i++) {
for (int j = 0; j < 10; j += 2); {
System.out.print("# ");
}
for (int j = 0; j < 2; j++) {
System.out.print(". ");
}
for (int j = 1; j < 10 - i; j++) {
System.out.print("x ");
}
System.out.println();
}
}
}
This code does what you want:
private static void pattern1() {
StringBuilder stringBuilder = new StringBuilder();
for (int ats = 2; ats <= 10; ats += 2) {
for (int j = 0; j < ats; j++) {
stringBuilder.append("# ");
}
stringBuilder.append(". . ");
for (int j = 0; j <= 10 - ats; j++) {
stringBuilder.append("x ");
}
stringBuilder.append("\n");
}
System.out.println(stringBuilder.toString());
}
One mistake you did in your code is that you put a ; after your for loop, this will end the loop right there.
Also do not use System.out.println() in loops. As using IO will slow down your application. Use StringBuilder to build strings and then output all at once.
Instead of:
for (int j = 0; j < 10; j += 2); {
System.out.print("# ");
}
Try:
for (int j = 0; j < 2 * i; j += 1) {
System.out.print("# ");
}
public class PrintPatterns
{
public static void main(String[] args)
{
pattern1();
}
private static void pattern1()
{
for(int i = 1; i <= 10; i++)
{
for(int j = 1; j < i+2; j++)
{
System.out.print("# ");
}
for(int j = 0; j < 2; j++)
{
System.out.print(". ");
}
for(int k = 10-i; k > 0; k--)
{
System.out.print("x ");
}
System.out.println();
}
}
}
This is my code:
for (int i = 4; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= 4; k++) {
System.out.print(k+"");
}
System.out.println();
}
Current output:
4
34
234
1234
Desired output:
1
21
321
4321
What changes are necessary in order for me to get the desired output as shown above?
Let the first loop (i) run from 1 to 4 and the second (j) from 4 to i.
This reverses your output.
You did every thing right, just the last for should have a very minor change:
for (int k = 5-i; k >= 1; k--){
Here you go:
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 4; j > i; j--) {
System.out.print(" ");
}
for (int k = i; k >= 1; k--){
System.out.print(k + "");
}
System.out.println();
}
}
Your loops are incorrect, you can refer the below code with inline comments:
for (int i = 1; i <= 4; i++) { //iterate from 1 to 4
//Loop from i+1 to insert spaces first
for (int j = i+1; j <= 4; j++) {
System.out.print(" ");
}
//Loop from i to insert the number next to each other
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
System.out.println(); //insert a new line
}
for (int i = 1; i <= 4; i++)
{
for (int k = i; k <= 4; k++)
{
System.out.print(" ");
}
for (int j = 1; j < i; j++)
{
System.out.print(j);
}
System.out.println();
}
Here is what the shapes should look like :
Here is my code so far:
public class Diamonds {
public static void main(String[] args) {
for (int i = 1; i < 10; 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");
}
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");
}
}
}
I am having trouble getting the second shape
In order to not spoil your fun with this problem, I will explain what you need to do without writing any code.
To get the second shape in there, you would need to add two additional nested for loops into each of the two "outer" loops that you already have.
Loops number three will produce a fixed number of spaces. Note that the distance between the right edge of the first shape and the left edge of the second shape is constant, so your third loops will be easy to code up.
Loops number four will loop like your first loop, but they would change places: the first inner loop from the first outer loop will be the forth inner loop in the second outer loop, and vice versa.
By examining the shape on the right, we can notice that for each N asterisks on the line in the left shape, the right one has 10 - N, so, taking your code and extending it, we can get:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
for (int i = 1; i < 10; 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(SPACE);
for (int j = 0; j < 10 - 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(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
And if we extract some common code:
public class Diamonds {
public static final String SPACE = " ";
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
drawLine(i);
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
drawLine(i);
System.out.print("\n");
}
}
private static void drawLine(int i) {
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(SPACE);
for (int j = 0; j < 10 - i; j++)
System.out.print("*");
}
}
public class ReverseDiamond {
public static void main(String[] ar) {
int rows = 10;
ReverseDiamond diamond = new ReverseDiamond();
for(int i = 0; i < rows; i++)
diamond.printLine(rows, i);
for(int i = rows - 2; i >= 0; i--)
diamond.printLine(rows, i);
}
private void printLine(int rows, int currRow) {
for(int space = 1; space <= currRow; space++)
System.out.print(" ");
for(int star = 1; star < 2 * (rows - currRow); star++)
System.out.print("*");
System.out.println();
}
}
You may like that :
public class Diamonds {
public static void main(String[] args) {
int totalStars = 9;
int rows = 9;
for (int r = 0,stars=-1,gap=totalStars; r < rows; r++ ) {
stars+= (r<=rows/2) ?2:-2;
gap=totalStars-stars;
printChars(' ', gap);
printChars('*', stars);
printChars(' ', gap);
int gap2=stars+1;
int stars2=gap+1;
printChars(' ', gap2);
printChars('*', stars2);
printChars(' ', gap2);
System.out.println();
}
}
private static void printChars(char c ,int times) {
for (int i = 0; i < times; i++) {
System.out.print(c);
}
}
}
try this :
public static void main(String[] args) {
for (int i = 9; 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");
}
for (int i = 2; i < 10; 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 :
*********
*******
*****
***
*
***
*****
*******
*********
I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern 1
54321
5432
543
54
5
Pattern 2
1
12
123
1234
12345
Pattern 3
12345
2345
345
45
5
Pattern 4
1
123
12345
123
1
My code that almost matched pattern 1 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");
}
public class PrintPattern {
public static void main(String[] args){
printPattern1();
printPattern2();
printPattern3();
printPattern4();
}
public static void printPattern1(){
for (int i = 0; i<5; i++){
for(int j = 5; j>i; j--)
System.out.print(j);
System.out.println();
}
}
public static void printPattern2(){
for (int i = 0; i<5; i++){
for(int k = 0; k<4-i; k++)
System.out.print(" ");
for(int j = 1; j<=i+1; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern3(){
for (int i = 0; i<5; i++){
for(int k = 0; k<i; k++)
System.out.print(" ");
for(int j = i+1; j<=5; j++)
System.out.print(j);
System.out.println();
}
}
public static void printPattern4(){
for (int i = 0; i<5; i++){
for(int k = 0; k<Math.abs(2-i); k++)
System.out.print(" ");
for(int j = 1; j<=5-2*Math.abs(2-i); j++)
System.out.print(j);
for (int p = 0; p<Math.abs(2-i); p++)
System.out.print(" ");
System.out.println();
}
}
}
Your inner for loop
for (int j = (rows + 1 - i) ; j > 0 ; j-- ){
System.out.print(j);
}
will always count down to 1, because it keeps going until j is zero. Also, the number that the current row starts on will depend on the current row, because you used i in your assignment to j. To get pattern 1 both of those things will have to change.
Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -
int rows = 5; // <- Start at 5.
for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
System.out.print(j);
}
System.out.println();
}
Output is pattern 1 in your question,
54321
5432
543
54
5
Try this:
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int k = 2; k >= i; k--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 2; i++) {
for (int k = i; k > 0; k--) {
System.out.print(" ");
}
if (i % 2 != 0) {
for (int j = 1; j <= (2 * i + 1); j++) {
System.out.print(j);
}
} else {
for (int j = 1; j <= (i / 2); j++) {
System.out.print(j);
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
int rows = 5;
System.out.println("------ PATTERN 1 ------");
for (int i = 1 ; i <= rows ; i++){
for (int j = rows; j >= i ; j--){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 2 ------");
for (int i = 1 ; i <= rows ; i++){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 3 ------");
for (int i = rows ; i >= 1 ; i--){
int k;
for (k = rows ; k > i; k--){
System.out.print(" ");
}
for (int j = 1; j <= k ; j++){
System.out.print(j);
}
System.out.println();
}
System.out.println("\n------ PATTERN 4 ------");
int whitespaces = rows/2;
for (int i = 1 ; i <= rows; i++){
// absolute value of whitespaces
int abs_whitespaces =
(whitespaces < 0 ? -whitespaces : whitespaces);
for (int j = 0 ; j < abs_whitespaces ; j++){
System.out.print(" ");
}
for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){
System.out.print(j);
}
whitespaces-=1;
System.out.println();
}
}
The first program is:
class timepass {
public static void main() {
for (int a = -1;a<=5;a++) {
for(int b = 5; b >= a;b--) {
System.out.print("*");
}
System.out.println();
// enter code here
}
}
}
The second program is:
class timepass {
public static void main() {
for(int i = 1;i<= 6;i++) {
for(int j = 1;j<= i ;j++) {
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Triangle {
public static void main(String[ ] args){
Scanner scan = new Scanner(System.in);
System.out.println("enter no.of line you need");
int n = scan.nextInt();
for(int i=1;i<=n;i++){
for(int j=5;j>=i;j--){
System.out.print(j);
}
System.out.println(" ");
}}}
Examples of input:
3
4
Examples of output (assume that spaces = new lines.)
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
So far, the fragment of code that attempts to print this is (Assume that all variables are pre-defined):
public int getSize()
{
for (int i = size; i > 0; i--){
for (int j = 1; j < size; j++){
out.print("Q");
out.print("H");
}
out.println("");
}
return 0;
}
It just prints: (assume that spaces = new lines.)
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
For input of 5. I'm not quite sure how to make it print only the number of times of its respective integer value. Can someone explain?
You could break the inner loop it two, like this:
for (int i = size; i > 0; i--) {
for (int j = 0; j < i; j++) {
out.print("Q");
}
for (int j = i; j < size + 1; j++) {
out.print("H");
}
out.println();
}
Output:
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
Or if you don't want to break the loop, you can use the ternary operator:
for (int i = size; i > 0; i--) {
for (int j = 0; j < size + 1; j++) {
out.print(j < i ? 'Q' : 'H');
}
out.println();
}
Try this
for (int i = 0; i < size; i++) {
for (int j = 1; j <= size-i; j++) {
System.out.print("Q");
}
for (int k = 0; k <= i; k++) {
System.out.print("H");
}
System.out.println("");
}
try this code block instead:
int j=0;
for (int i = size; i > 0; i--)
{
j=0;
while(j < i)
{
out.print("Q");
j++;
}
j=i;
while(j < size+ 1)
{
out.print("H");
j++;
}
out.println();
}
Tested with sample inputs. Working fine
public int getSize() {
for (int i = 1; i < size+1; i++) {
for (int j = 0; j < size+1; j++) {
int Qtimes = size-i;
if(j <= Qtimes) {
System.out.print("Q");
} else{
System.out.print("H");
}
}
System.out.println("");
}
return 0;
}
This works if the input is 4 - for example -change it to any number
public int getSize()
{
int cnt = 0;
int i,j,k = 0;
for ( i = 4; i > 0; i--){
for ( j = 0; j < i; j++){
System.out.print("Q");
}
cnt ++;
for( k = 0 ; k <cnt ; k++) {
System.out.print("H");
}
System.out.println("");
}
return 0;
}
output is
QQQQH
QQQHH
QQHHH
QHHHH