I get a compile error with the simple output statement:
System.out.println(j);
But without it, it compiles fine.
Secondly, each time the for(condition) is true, it will iterate, but how would it ever be 4/2 (wouldn't J iterate to 3)?
Hope that makes sense.
public class FindFac {
public static void main(String args[]) {
for(int i = 2; i <= 50; i++) {
System.out.print("Factors of " + i + ": ");
for(int j = 2; j < i; j++)
***System.out.println(j);***
if((i%j) == 0) System.out.print(j + " ");
System.out.println();
}
}
}
Its a very silly mistake, you missed the opening and closing braces for the for loop :
public static void main(String args[]) {
for (int i = 2; i <= 50; i++) {
System.out.print("Factors of " + i + ": ");
for (int j = 2; j < i; j++) {
System.out.println(j);
if ((i % j) == 0) System.out.print(j + " ");
}
System.out.println();
}
}
}
Related
I have to make 2D array [10][10] to be filled like this:
(https://i.stack.imgur.com/WCRGu.png)
Please, can someone help me with this?
`
int j, i;
int n=1;
for (j=9; j>=0;j--) {
for (i=11-j; i>=9-j; i++) {
if(i<10) {
a[i][j]=n;
n++;
}else{
break;
}
}
}
for (i=0;i<=9;i++) {
for(j=0;j<=9;j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
`
Here the program for your problem. I've also added the printing of the 2d array. It accepts any 1:1 size of the array, you can modify the size of your wanting using the variable final int ARRAY_SIZE.
public class StackOverflow {
public static void main(String[] args) {
final int ARRAY_SIZE = 10;
int[][] array = new int[ARRAY_SIZE][ARRAY_SIZE];
//filling of values for 2d array
for (int i = 0, k = 1; i < array.length ; i++) {
for (int j = i + 3; j > i; j--, k++) {
if (j > array.length) {
k--;
continue;
}
array[j - 1][array.length - i - 1] = k;
}
}
//printing of 2d array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i][j] == 0)
System.out.print(array[i][j] + " ");
else if (array[i][j] > 9)
System.out.print(array[i][j] + " ");
else
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
So, this code is supposed to do the following:
Check if the neighboring members in a row of the square matrix and write them if their sum is an even number and also write the members which sum is a odd number. But it sends an error in the first if statement. But it only does the first row and outputs the Index out of bounds. Here is the code:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Ucitati broj clanova kvadratne matrice: ");
int n = scan.nextInt();
int niz[][] = new int[n][n];
System.out.println("Ucitati clanove matrice: ");
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
System.out.print("n[" + i + ", " + j + "]" + "=");
niz[i][j] = scan.nextInt();
}
}
System.out.println();
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
System.out.print(niz[i][j] + " ");
}
System.out.println();
}
System.out.println("Susedni clanovi cija je suma parna su: ");
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
if ((niz[i][j] + niz[i][j + 1]) % 2 == 0) {
System.out.print(niz[i][j] + " " + niz[i][j + 1] + "; ");
}
}
}
System.out.println("Susedni clanovi cija je suma neparna su: ");
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
if ((niz[i][j] + niz[i][j + 1]) % 2 != 0) {
System.out.print(niz[i][j] + " " + niz[i][j + 1] + "; ");
}
}
}
}
System.out.println are in Serbian so if u need a translate let me know, but that shouldn't be needed.
I want to print the following pattern in java:
a+1357+1
b+246+2
a+13+3
b+2+4
following is my code, but with this i can only print odd no. or only even no.s
public static void main(String[] args) {
int rows = 7;
for(int i = rows; i >= 1; i=i-2) {
for(int j = 1; j <= i; j=j+2) {
System.out.print(j + " ");
}
System.out.println();
}
}
DEMO
var rows = 4;
for (var i = 4; i > 0; i--) {
for (var j = 1; j <= i; j++) {
document.write((i % 2) + (2 * j) - 1 + " ");
}
document.write('<br>');
}
public static void main(String[] args) {
int rows = 4;
for(int i = rows; i > 0; i--) {
for(int j = 1; j <= i; j++) {
System.out.print((i%2)+(2*j)-1 + " ");
}
System.out.println();
}
}
You need to make a pattern for it. here you can use (i%2)+(2*j)-1
With only a few updates of your code (but not very readable):
int rows = 7;
for (int i = rows; i >= 1; i = i - 2) {
System.out.print((((i + 1) % 4) == 0 ? "a" : "b") + " + ");
for (int j = 1; j <= i; j = j + 2) {
System.out.print((j + ((i + 2) % 4) / 2));
}
System.out.println(" + " + (10 - i) / 2);
}
But instead of using my code, I suggest you write down exactly how the "pattern" is defined and write new code based on your specification. These loops are not optimal.
I feel like the code I wrote is wrong but I don't know why. looks very unprofessional to me.
/* 1234
2341
3412
4123
*/
public class pattern{
public static void main(String args[]){
for(i=1; i<=4; i++)
{for(j=1; j<=4; j++)
{System.out.print(i);
}
System.out.println();
while(i>4)
{ int i= 1;
i++;
System.out.print(i);}
System.out.println();
}
It's hard to tell what your asking for exactly, but—judging from the comment above your class—you're probably looking for something like this:
for (int i = 0; i < 4; i++) {
for (int j = i; j < i + 4; j++) {
System.out.print((j % 4) + 1);
}
System.out.println();
}
It goes without saying, but I have to; you should always try to follow the Java naming/formatting standards.
Take care at these:
for(int i=1; i<=4; i++)
{for(int j=1; j<=4; j++)
System.out.println();
You should initialize the variables i and j and System not system
And the parameter in main method should be like String args:
public static void main(String args[]){
Here is your code in nice format:
public static void main(String args[]) {
for (int i = 0; i < 4; i++) {
for (int j = i; j < i + 4; j++) {
System.out.print((j % 4) + 1);
}
System.out.println();
}
}
for(int i=1;i<=4;i++) {
for(int j=1;j<=4;j++) {
int k=i+j-1;
if(k>4) {
System.out.print(k-4 + " ");
}
else {
System.out.print(k + " ");
}
}
System.out.print(" ");
I'm having some trouble in a couple of areas here. First, when the array is printed to screen, the "P" is placed at [0][0] - which is OK, but it is surrounded by 'null' for any other cell. I'd like it to be filled with dashes " - ". I'm also needing to code in an infinite loop that asks the user to input either 'up', 'down', 'left', 'right' or 'exit'. Does this infinite loop go into the "Driver" class, or the "World" class, and would a switch statement work for this?
Second - the rows and columns are not being summed and displayed. The "World" class is:
import java.util.*;
public class World
{
private static final String P = "P";
private String[][] array;
public World()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter number of row: ");
int crow = input.nextInt();
System.out.println("Enter number of columns: ");
int ccol = input.nextInt();
array = new String[crow][ccol];
array[0][0]=P;
}
public void displayWorld()
{
System.out.println();
for(int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
System.out.print(array[i][j] + " - ");
}
System.out.println();
}
}
public void moveUp()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i - 1][j] = P;
}
return;
}
}
}
}
public void moveDown()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i + 1][j] = P;
}
return;
}
}
}
}
public void moveLeft()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i][j - 1] = P;
}
return;
}
}
}
}
public void moveRight()
{
for(int i= 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if ((array[i][j]) == " - ")
{
if (i < array.length - 1)
{
array[i][j] = " - ";
array[i][j + 1] = P;
}
return;
}
}
}
}
}
The "Driver" class is:
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
World world=new World();
world.moveUp();
world.moveDown();
world.moveLeft();
world.moveRight();
world.displayWorld();
}
}
You need to intialize every element in the array. Add this to the end of the World constructor:
for(int i=0;i<crow;i++){
for(int j=0;j<ccol;j++){
array[crow][ccol]="-";
}
}
You will need to add the infinite loop asking for input in the Driver class. In the infinite loop you need to:
Get input
Use switch statement to detect left, right, etc
Driver.java:
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
World world = new World();
Scanner s = new Scanner(System.in);
while (true) {
String input = s.nextLine();
switch (input) {
case "up":
world.moveUp();
case "down":
world.moveDown();
case "left":
world.moveLeft();
case "right":
world.moveRight();
}
}
s.close();
}
}