Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new in Java.
I have next code:
public static void main(String[] args) {
for(int k = 10; k > 0; k--)
{
for(int l=0; l < k-1; l++)
{
System.out.print(' ');
}
for(int n=10; n > k-1; n--)
{
System.out.print('*');
}
System.out.println();
}
}
It prints this:
*
**
***
****
*****
******
*******
********
*********
**********
But I want to print it with empty inside like this:
*
**
* *
* *
* *
* *
* *
* *
* *
**********
Can anyone to explane me how to do it.
I understand that this is not a place where solve homework tasks. But can somebody tell me the algorithm for solving the problem in words.
I do not need a ready solution because I want to understand and solve it by myself. So how I can put spaces inside?
Hint: In the second loop you should print a '*' for the first and last values of n, not all of them. For the other positions, print a ' '.
Hint: in the second inner for loop, you could have an if statement where you check for n's greatest and lowest values, and k's lowest value, then print a '*', otherwise print a ' '. Like so:
for(int n = 10; n > k - 1; n--){
if(n == 10 || n == k || k == 1){
System.out.print('*');
} else {
System.out.print(' ');
}
}
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 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 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();
}
}
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 needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to create a method that will remove one random element in each row from a solved Sudoku matrix that I have generated. How can I achieved this?
Solved Sudoku Generator:
public static void generate_Sud (int n, int [][] S){ // Generates Sudoku method
for (int r=0; r<=n-1; r++){
int startNum = (int) (Math.sqrt(n) * (r % Math.sqrt(n)) + (r/Math.sqrt(n)));
for (int c=0; c<=n-1; c++){
S[r][c] = ((startNum + c) % n) + 1;
}
}
}
Sudoku Matrix Generated
for a 3x3 Sodoku:
for(int i = 0; i < 3; i++){
int randomPosition = (int) Math.floor(Math.random() * 3) //Random number between 0 and 2
S[i][randomPosition] = -1; // -1 or whatever you use to represent blank
}
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 6 years ago.
Improve this question
I tried to made a star pattern but I did not know how to do it, I know how to made start pattern when the start or the spaces are continuous, but when it is a start and spaces like the pattern that I show below, I do not how to do it.
* *
* *
*
* *
* *
You need to find the relationship between the corners of the cross/star. Take this star for example of size 5.
0 1 2 3 4
0 * *
1 * *
2 *
3 * *
4 * *
In a cross in the diagonal from (0,0) to (4,4), the indices are the same (in the code this means row == col).
Also, you can notice that in the diagonal from (0,4) to (4,0) indices always sum up to 4, which is size - 1 (in the code this is row + col == size - 1).
Therefore in the code, you will need to loop through rows and then through columns. Each time you have to check if the above conditions are fulfilled.
Code:
class Main {
public static void main(String[] args) {
printCross(5); // Vertical size of cross
}
public static void printCross(int size) {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (row == col || row + col == size - 1) {
System.out.print('*');
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}