Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to programming and would appreciate any experts to provide suggestions.
Based on the given flowchart, am I on the right track?
How do I make improvement to my code to ensure robustness?
Flowchart
Code
// r is row, c is column
int r = 1, c = 1;
do {
if (r <= 4)
{
if (c <= 10)
{
System.out.print("*");
c += 1;
}
else {
r += 1;
c = 1;
System.out.println();
}
}
else {
System.exit(1);
}
}while (c != 12);
Output
**********
**********
**********
**********
while (c != 12);
The flow chart doesn't have this condition.
Notice how your if (r <= 4) statement is directly nested in the do...while. This means you can reduce to a single while:
while (r < = 4) {
...
}
Similarly c <= 10 should be implemented as a loop instead of just an if.
The flow chart doesn't say to do start a new line like you do with System.out.println();. So taken literally, this line is incorrect. However, I suspect the flow chart omitted this detail and is in error.
According to the algorithm proposed, its code is seen to follow or comply with the guideline.
Your code looks great, it just tries to name the variables in a more similar way to what you are trying to replicate.
I have tried to shorten the number line of coding.
int row = 1, column = 1;
while (row <= 4)
{
if (column <= 10) {
System.out.print("*");
column += 1;
}
else {
row += 1;
column = 1;
//System.out.println();
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a logical error code and I can not find out where it would be happy to solve the code
public static int nameCheck(char[] names) {
int blankCount = 0;
for (int i = names.length; i > 1; i--) {
if (names[i - 1] == ' ') {
blankCount++;
}
}
return blankCount;
}
When a for loop 'loops', java first executes the 'incrementor' expression (here, i--), and then checks the condition (here, i > 1), and will abort the loop then and tehre if the condition no longer holds.
Thus, eventually i is 2, you check names[i - 1] (and java is 0-indexed, so names[1] returns the second character). Then, i is decremented to 1, and the loop ends.
Thus, this code fails to count the first character if it is blank.
Simply make that i > 0 to fix it.
You're actually writing your loop wrongly.
Perhaps your question isn't clear enough
It should be this way i guess.
public static int nameCheck(char[] names){ int blankCount = 0;
for (int i = 0; i > names.length; i--) {
if (names[i-1] == ' ') {
blankCount++;
}
}
return blankCount;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
The below pattern code is not working as expected.
public class pattern_print {
public static void main (String args[]){
int i = 1, j = 5, n = 5;
while (i <= n) {
while (j >= i) {
System.out.print("*");
j--;
}
System.out.print("\n");
i++;
}
}
}
Who can help me?
What are you expecting?
The code that you wrote is displaying the following chars:
*****
If you want to display something like:
*****
****
***
**
*
Then the correct code is:
public class pattern_print {
public static void main (String args[]) {
int i = 1, j = 5, n = 5;
while (i <= n) {
while (j >= i) {
System.out.print("*");
j--;
}
System.out.print("\n");
j=5;
i++;
}
}
}
Now depends what you expect to be displayed.
If the following triangle pattern is expected:
*****
****
***
**
*
the value of j needs to be reset to n as Andreea Frincu suggested.
However, for loops may be more preferable when printing patterns.
Also, since Java 11 released back in Sep 2018 there is method String::repeat which allows to avoid redundant nested loops and the same triangle pattern may be printed as simply as:
for (int i = 5; i >= 0; i--) {
System.out.println("*".repeat(i));
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm new to R programming and I am struggling to find the next prime number given some number x. I have tried the logic in Java and it works. So what I really did was to translate some Java code to R. So here is my Java version that works.
public static int nextPrime(int n) {
int i,j;
for( i= n+1;;i++) {
for(j=2;j<i;j++) {
if(i % j == 0)
break;
}
if(j == i) {
// System.out.println(i);
break;
}
}
return i;
}
and here is my R version
nextPrime <- function(x) {
i = x+1
repeat {
for(j in 2 : (i-1)) {
if((i %% j) == 0 ) {
break
}
# j = j+1
# print(j)
}
if( i == j){
break
}
i = i+1
}
print(i)
}
nextPrime(11)
I don't know about Java, but here is a function - while loop - that gives you what you want.
f <- function(x){
# #IsmailMüller
temp <- x+1
test <- 2:x
# while temp is divisible by at least one precedent value (not prime)
while( any( (temp %% test) == 0 ) ){
# increase the value of temp by 1
temp <- temp+1
}
temp
}
f(11)
Following your code, both are not same.
Replace
for(j in 2 : (i-1))
With
for(j in 2 : i)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am fairly new to Java, but my for loop is instantly skipping to the highest possible value in the following code:
System.out.println(i);
for(i = 0; i <= (difficulty - 2); i++);{
System.out.println(i);
nextMineX = (int) (10*Math.random());
nextMineY = (int) (10*Math.random());
for(y = 0; y <= 14; y++){
System.out.println(y);
if(nextMineX == minesX[y] && nextMineY == minesY[y]){
i = i-1;
} else{
minesX[i] = nextMineX;
minesY[i] = nextMineY;
}
}
}
The first for loop is screwing up, while the nested one is running fine. the variable i is initialized as 0, and difficulty is at 16. the output of this excerpt is as follows:
0
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
If anyone can help me with his that would be extremely appreciated. Since I'm new, it is probably something small and basic that I'm overlooking.
The problem is the semicolon at the end of the second line. It's valid to have a for loop with no body. It's also valid to have standalone a block of code inside brackets (this defines a scope for variables--if you defined a variable inside the brackets, it wouldn't be available outside). So Java is interpreting the beginning of your code like this:
for(i = 0; i <= (difficulty - 2); i++); // for loop is done, so i = difficulty - 2
{
System.out.println(i);
...
Your for loop statement ends with the semicolon you have
for(i = 0; i <= (difficulty - 2); i++); <- incorrect
for(i = 0; i <= (difficulty - 2); i++){ <- correct
//body
}
For loop is terminating cause of semicolon ;
for(i = 0; i <= (difficulty - 2); i++); //semicolon terminating loop
{...}
so you should use for loop like this
for(i = 0; i <= (difficulty - 2); i++) //remove semicolon prevent to terminate
{...}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to write a section of code that outputs the binary value up to a user defined number (0-7).
We can't use .toBinaryString it has to be using loops (for loops preferably).
The output should be three columns with filler zeros.
Ex) User enters 7
001
010
100
101
110
111
It seems like it should be so simple but I cant seem to get it right.
for (int i = 1; i <= input; i++) {
String line = "";
for (int k = 2; k >= 0; k--) {
line += ((i >> k) & 1) == 1 ? "1" : "0";
}
System.out.println(line);
}
That uses two for loops.
I would create your own toBinary() function:
int toBinary(int x){
StringBuilder sb = new StringBuilder("");
while(x >= 1){
sb.append(x%2);
x /= 2;
}
return Integer.parseInt(sb.reverse().toString());
}
Then just use that function to print:
for(int i=1; i<=7; i++)
System.out.println( String.format("%03d", toBinary(i)) );