How do you completely run an if statement before moving to the next one?
up vote
1
down vote
favorite
This question is very hard to put into concise wording. How would I modify my code to meet the constraints I'm given in this problem?
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.XShape(test));
public static String XShape(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res += test[r][c] + " ";
else if (c + r == 4)
res += test[r][c] + " ";
return res;
The point of this code is to put the integers that make an X pattern into a string and to print the string. This is the output I get whenever I run it:
1 5 7 9 13 17 19 21 25
But I want the output to look like this (using the first if statement first, adding all of those values into the string, then moving to the other else if statement and adding all of those values into the string):
1 7 13 19 25 21 17 13 9 5
java
add a comment |
up vote
1
down vote
favorite
This question is very hard to put into concise wording. How would I modify my code to meet the constraints I'm given in this problem?
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.XShape(test));
public static String XShape(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res += test[r][c] + " ";
else if (c + r == 4)
res += test[r][c] + " ";
return res;
The point of this code is to put the integers that make an X pattern into a string and to print the string. This is the output I get whenever I run it:
1 5 7 9 13 17 19 21 25
But I want the output to look like this (using the first if statement first, adding all of those values into the string, then moving to the other else if statement and adding all of those values into the string):
1 7 13 19 25 21 17 13 9 5
java
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question is very hard to put into concise wording. How would I modify my code to meet the constraints I'm given in this problem?
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.XShape(test));
public static String XShape(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res += test[r][c] + " ";
else if (c + r == 4)
res += test[r][c] + " ";
return res;
The point of this code is to put the integers that make an X pattern into a string and to print the string. This is the output I get whenever I run it:
1 5 7 9 13 17 19 21 25
But I want the output to look like this (using the first if statement first, adding all of those values into the string, then moving to the other else if statement and adding all of those values into the string):
1 7 13 19 25 21 17 13 9 5
java
This question is very hard to put into concise wording. How would I modify my code to meet the constraints I'm given in this problem?
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.XShape(test));
public static String XShape(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res += test[r][c] + " ";
else if (c + r == 4)
res += test[r][c] + " ";
return res;
The point of this code is to put the integers that make an X pattern into a string and to print the string. This is the output I get whenever I run it:
1 5 7 9 13 17 19 21 25
But I want the output to look like this (using the first if statement first, adding all of those values into the string, then moving to the other else if statement and adding all of those values into the string):
1 7 13 19 25 21 17 13 9 5
java
java
asked Nov 10 at 19:55
Excel
478
478
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
You don’t need an extra loop if you use 2 results, then join them at the end:
String res1, res2 = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res1 += test[r][c] + " ";
else if (c + r == 4)
res2 += test[r][c] + " ";
return res1 + res2;
I feel legally obligated to be incredibly pedantic and remind you that there are two loops in your code. But I see the point you're making and agree with it.
– Silvio Mayolo
Nov 10 at 20:39
@silvio it’s OK to be pedantic - this is a safe place - and you’re quite right too. I’ve improved the accuracy of my text, and it feels better too.
– Bohemian♦
Nov 10 at 20:55
add a comment |
up vote
1
down vote
You need two loops.
One where c = r and another where c = 4 - r
You don't need to use nested loops, just one each. You also don't need an if statement.
add a comment |
up vote
1
down vote
You have two nested for loops. They're going to run and iterate in column-major order. If you want to iterate in a different order, then you need to use different loops. There's nothing wrong with the if statement.
for (int c = 0; c < test.length; c++)
// Do it for (c, c)
for (int c = 0; c < test.length; c++)
// Do it for (c, test.length - c - 1)
Also, as a tangential note, you use 4 as a magic number in one place and test.length in others. If you always expect the array to be 5x5, then use 5 instead of test.length and put an assertion at the beginning. Otherwise (more likely), use test.length - 1 in place of the magic number 4.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242851%2fhow-do-you-completely-run-an-if-statement-before-moving-to-the-next-one%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You don’t need an extra loop if you use 2 results, then join them at the end:
String res1, res2 = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res1 += test[r][c] + " ";
else if (c + r == 4)
res2 += test[r][c] + " ";
return res1 + res2;
I feel legally obligated to be incredibly pedantic and remind you that there are two loops in your code. But I see the point you're making and agree with it.
– Silvio Mayolo
Nov 10 at 20:39
@silvio it’s OK to be pedantic - this is a safe place - and you’re quite right too. I’ve improved the accuracy of my text, and it feels better too.
– Bohemian♦
Nov 10 at 20:55
add a comment |
up vote
2
down vote
accepted
You don’t need an extra loop if you use 2 results, then join them at the end:
String res1, res2 = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res1 += test[r][c] + " ";
else if (c + r == 4)
res2 += test[r][c] + " ";
return res1 + res2;
I feel legally obligated to be incredibly pedantic and remind you that there are two loops in your code. But I see the point you're making and agree with it.
– Silvio Mayolo
Nov 10 at 20:39
@silvio it’s OK to be pedantic - this is a safe place - and you’re quite right too. I’ve improved the accuracy of my text, and it feels better too.
– Bohemian♦
Nov 10 at 20:55
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You don’t need an extra loop if you use 2 results, then join them at the end:
String res1, res2 = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res1 += test[r][c] + " ";
else if (c + r == 4)
res2 += test[r][c] + " ";
return res1 + res2;
You don’t need an extra loop if you use 2 results, then join them at the end:
String res1, res2 = "";
for (int c = 0; c < test[0].length; c++)
for (int r = 0; r < test.length; r++)
if (c == r)
res1 += test[r][c] + " ";
else if (c + r == 4)
res2 += test[r][c] + " ";
return res1 + res2;
edited Nov 10 at 20:52
answered Nov 10 at 20:11
Bohemian♦
293k63412550
293k63412550
I feel legally obligated to be incredibly pedantic and remind you that there are two loops in your code. But I see the point you're making and agree with it.
– Silvio Mayolo
Nov 10 at 20:39
@silvio it’s OK to be pedantic - this is a safe place - and you’re quite right too. I’ve improved the accuracy of my text, and it feels better too.
– Bohemian♦
Nov 10 at 20:55
add a comment |
I feel legally obligated to be incredibly pedantic and remind you that there are two loops in your code. But I see the point you're making and agree with it.
– Silvio Mayolo
Nov 10 at 20:39
@silvio it’s OK to be pedantic - this is a safe place - and you’re quite right too. I’ve improved the accuracy of my text, and it feels better too.
– Bohemian♦
Nov 10 at 20:55
I feel legally obligated to be incredibly pedantic and remind you that there are two loops in your code. But I see the point you're making and agree with it.
– Silvio Mayolo
Nov 10 at 20:39
I feel legally obligated to be incredibly pedantic and remind you that there are two loops in your code. But I see the point you're making and agree with it.
– Silvio Mayolo
Nov 10 at 20:39
@silvio it’s OK to be pedantic - this is a safe place - and you’re quite right too. I’ve improved the accuracy of my text, and it feels better too.
– Bohemian♦
Nov 10 at 20:55
@silvio it’s OK to be pedantic - this is a safe place - and you’re quite right too. I’ve improved the accuracy of my text, and it feels better too.
– Bohemian♦
Nov 10 at 20:55
add a comment |
up vote
1
down vote
You need two loops.
One where c = r and another where c = 4 - r
You don't need to use nested loops, just one each. You also don't need an if statement.
add a comment |
up vote
1
down vote
You need two loops.
One where c = r and another where c = 4 - r
You don't need to use nested loops, just one each. You also don't need an if statement.
add a comment |
up vote
1
down vote
up vote
1
down vote
You need two loops.
One where c = r and another where c = 4 - r
You don't need to use nested loops, just one each. You also don't need an if statement.
You need two loops.
One where c = r and another where c = 4 - r
You don't need to use nested loops, just one each. You also don't need an if statement.
answered Nov 10 at 19:58
Peter Lawrey
439k55557958
439k55557958
add a comment |
add a comment |
up vote
1
down vote
You have two nested for loops. They're going to run and iterate in column-major order. If you want to iterate in a different order, then you need to use different loops. There's nothing wrong with the if statement.
for (int c = 0; c < test.length; c++)
// Do it for (c, c)
for (int c = 0; c < test.length; c++)
// Do it for (c, test.length - c - 1)
Also, as a tangential note, you use 4 as a magic number in one place and test.length in others. If you always expect the array to be 5x5, then use 5 instead of test.length and put an assertion at the beginning. Otherwise (more likely), use test.length - 1 in place of the magic number 4.
add a comment |
up vote
1
down vote
You have two nested for loops. They're going to run and iterate in column-major order. If you want to iterate in a different order, then you need to use different loops. There's nothing wrong with the if statement.
for (int c = 0; c < test.length; c++)
// Do it for (c, c)
for (int c = 0; c < test.length; c++)
// Do it for (c, test.length - c - 1)
Also, as a tangential note, you use 4 as a magic number in one place and test.length in others. If you always expect the array to be 5x5, then use 5 instead of test.length and put an assertion at the beginning. Otherwise (more likely), use test.length - 1 in place of the magic number 4.
add a comment |
up vote
1
down vote
up vote
1
down vote
You have two nested for loops. They're going to run and iterate in column-major order. If you want to iterate in a different order, then you need to use different loops. There's nothing wrong with the if statement.
for (int c = 0; c < test.length; c++)
// Do it for (c, c)
for (int c = 0; c < test.length; c++)
// Do it for (c, test.length - c - 1)
Also, as a tangential note, you use 4 as a magic number in one place and test.length in others. If you always expect the array to be 5x5, then use 5 instead of test.length and put an assertion at the beginning. Otherwise (more likely), use test.length - 1 in place of the magic number 4.
You have two nested for loops. They're going to run and iterate in column-major order. If you want to iterate in a different order, then you need to use different loops. There's nothing wrong with the if statement.
for (int c = 0; c < test.length; c++)
// Do it for (c, c)
for (int c = 0; c < test.length; c++)
// Do it for (c, test.length - c - 1)
Also, as a tangential note, you use 4 as a magic number in one place and test.length in others. If you always expect the array to be 5x5, then use 5 instead of test.length and put an assertion at the beginning. Otherwise (more likely), use test.length - 1 in place of the magic number 4.
edited Nov 10 at 20:06
answered Nov 10 at 20:00
Silvio Mayolo
13.6k22252
13.6k22252
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242851%2fhow-do-you-completely-run-an-if-statement-before-moving-to-the-next-one%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown