I have 0-10 for loop I want to change the value to 100 when is only 4 and the rest will not be affected and no break how should I achieve that. And as for the petitioner, do not repel [him].
class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
int n = 0;
for (int i = 0; i < 10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
}
else
{
System.out.println(i);
}
}
}
}
Another Solution!!
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
for (int i = 0; i <=10; i++)
{
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if (i == 4)
{
i = 100;
System.out.println(i);
i=4;
continue;
}
else
{
System.out.println(i);
}
}
}
}
Try This one !!
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
for(int i = 0; i < 10; i++){
// for example
/*
0 0
1 1
2 2
3 3
4 = 100 100
5 5
6 6
7 7
8 8
9 9
10 10
*/
if(i == 4){
System.out.println ("100");
}else{
System.out.println(i);
}
}
}
Related
I am writing a program which adds two integer matrices together and I am running into an ArayIndexOutOfBounds exception when trying to process larger matrices. Any advice on how to fix this issue would be greatly appreciated.
ArrayOutOfBoundsException on cmd prompt
Here is the class in which the error is occuring:
import java.util.ArrayList;
public class ThreadMaker
{
public static int count=0;
public static void addmatrix(int[][] mat_A,int[][] mat_B,int[][] submatrix1,int[][] submatrix2,int N,int M)throws Exception
{
ThreadOperation m1;
ThreadOperation.initialize(N,M);
ArrayList<Thread> threads = new ArrayList<Thread>();
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for(k=0,i=c; i < c + N/2; i++, k++)
{
for(l=0,j=d; j < d + M/2; j++, l++)
{
submatrix1[k][l]=mat_A[i][j];
submatrix2[k][l]=mat_B[i][j];
}
}
m1=new ThreadOperation(mat_A,mat_B,submatrix1,submatrix2,N,M,c,d);
Thread thread =new Thread(m1);
thread.start();
threads.add(thread);
thread.join();
d=d+M/2;
}
c=c+N/2;
}
}
public static void devidematrix(int[][] mat_A,int[][] submatrix,int N,int M)
{
int c=0,d;
while(c<N)
{
d=0;
while(d<M)
{
int i=0,j=0,k,l;
for( k=0,i=c;i<c+N/2;i++,k++)
{
for( l=0,j=d;j<d+M/2;j++,l++)
{
submatrix[k][l]=mat_A[i][j];
}
}
d=d+M/2;
}
c=c+N/2;
}
}
public synchronized static void printmatrix(int[][] matrix,int row,int col)
{
System.out.print(" ");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
System.out.print(matrix[i][j]+" ");
}
}
System.out.print(" ");
}
}
Here is the martix from the matrix2.txt file:
4 7
2 3 1 2 5 1 2
3 1 2 2 2 4 4
1 2 3 2 7 2 1
3 6 1 5 1 3 5
6 5 4 1 4 3 1
3 3 2 2 1 1 2
7 5 4 3 2 5 3
2 1 8 4 8 4 4
If any more information is need please let me know.
This is what it should look like
9 8 7 6 5 4 3 2 1 0
8 8 7 6 5 4 3 2 1 0
7 7 7 6 5 4 3 2 1 0
6 6 6 6 5 4 3 2 1 0
5 5 5 5 5 4 3 2 1 0
4 4 4 4 4 4 3 2 1 0
3 3 3 3 3 3 3 2 1 0
2 2 2 2 2 2 2 2 1 0
1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
Here's my attempt.
public class Main {
public static void main(String[] args) {
int i = 9;
int count = -1;
while (i >= count) {
int j = i;
while (j > count) {
System.out.print(j + " ");
j--;
}
System.out.println();
count++;
}
}
}
Here's my actual output:
9 8 7 6 5 4 3 2 1 0
9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2
9 8 7 6 5 4 3
9 8 7 6 5 4
9 8 7 6 5
9 8 7 6
9 8 7
9 8
9
This obviously does not match the expected output. Can someone point out where the mistake is in the code?
This is a Solution that has the right output, but instead of using while-Loops I used for-Loops
public class Main {
public static void main(String[] args) {
int count1 = 9;
for (int i = count1; i >= 0; i--) {
int count2 = i;
if (count1 > count2) {
int tmp = count1 - count2;
for (int j = tmp; j > 0; j--) {
System.out.print(count2 + " ");
}
}
for (int j = count2; j >= 0; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
You can keep two outer variables, counter and multiplier, for the matrix size and repetitions' count respectively:
public class Main {
public static void main(String[] args) {
int counter = 15;
int multiplier = 1;
for (int i = counter; i >= 0; i--) {
for (int j = 0; j<multiplier; j++) {
System.out.printf("%3d", counter); //using %3d for spacing numbers nicely
}
for (int k = counter-1; k >= 0; k--) {
System.out.printf("%3d", k);
}
++multiplier;
--counter;
System.out.println();
}
}
}
For every horizontal line, where counter decreases, and multiplier increases (9 once on 1st line; 8 twice on the second line, etc.):
it will first print the counter, multiplier times;
it will then fill the rest of the line with counter-multiplier number of descending sequence integers, starting from counter-1;
at the end of outer loop's each iteration, a new line is printed.
Output would be:
9 8 7 6 5 4 3 2 1 0
8 8 7 6 5 4 3 2 1 0
7 7 7 6 5 4 3 2 1 0
6 6 6 6 5 4 3 2 1 0
5 5 5 5 5 4 3 2 1 0
4 4 4 4 4 4 3 2 1 0
3 3 3 3 3 3 3 2 1 0
2 2 2 2 2 2 2 2 1 0
1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
Since you already have your answer, here are a couple alternatives.
String str = "9 8 7 6 5 6 3 2 1 0";
System.out.println(str);
for (int i = 9; i > 0; i--) {
str = str.replace(i+"",(i-1)+"");
System.out.println(str);
}
Or use the String.repeatmethod.
for (int i = 9; i >= 0; i--) {
System.out.print((i+" ").repeat(9-i));
for(int k = i; k >= 0; k--) {
System.out.print(k + " ");
}
System.out.println();
}
With the help of Java8 stream you can write the code as below:
public static void main(String[] args) {
IntStream.range(0, 10)
.forEach(i -> {
IntStream.range(0, 10).forEach(j -> {
System.out.print((9- (j < i ? i : j)) + " " );
});
System.out.println("");
});
}
i just know how to get the multiplication table of one number.
i have tryied put another number in method argument but nothing.
input
public static void main(String[] args) {
table(2,10);
}
public static void table(int num,int a) {
if(a>=0) {
table(num,a-1);
System.out.println(num+" x "+a+" = "+num*a);
}
}
output
2 x 0 = 0
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
I do not know why you must use recursive,but maybe you expected is next.
public static void table(int num,int a) {
if(a >= 0 && num <= a) {
for(int i = 0;i <= a;i++){
System.out.println(num+" x "+i+" = "+num*a);
}
if(num<a){
table(num+1,a);
}
}
}
can someone please check my code as to why the last index is not working as intended? Any advice on how to improve this is much appreciated.
import java.io.*;
import java.util.*;
public class Solution {
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 0 ; i < ar.length-1; i++){
key = ar[i];
seen = i;
while (seen <ar.length-1 && ar[seen+1]<key){
ar[seen]= ar[seen+1];
seen = seen+1;
}
ar[seen]=key;
printArray(ar);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
int[] ar = new int[s];
for(int i=0;i<s;i++){
ar[i]=in.nextInt();
}
insertionSortPart2(ar);
}
private static void printArray(int[] ar) {
for(int n: ar){
System.out.print(n+" ");
}
System.out.println("");
}
}
Input (stdin) 6 1 4 3 5 6 2
Your Output (stdout) 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 2 6
Expected Output 1 4 3 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 3 4 5 6 2 1 2 3 4 5 6 Compiler Message Wrong Answer
You are doing it in wrong way. Read the part 1 again.
Assume that first element is sorted. Then try to insert element one by one in sorted list.
Try this:
public static void insertionSortPart2(int[] ar) {
int key;
int seen;
for (int i = 1 ; i < ar.length; i++){
key = ar[i];
seen = i;
while (seen > 0 && ar[seen-1] > key) {
ar[seen] = ar[seen-1];
seen = seen - 1;
}
ar[seen]=key;
printArray(ar);
}
}
I want my output to be like this e.g. if the user inputs 3:
without using 2d array
1 2 3
1 1 2 3
2 1 4 6
3 3 6 9
My code so far
public void matrixmutilplication() {
String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows ");
int i = Integer.parseInt(thenumberofmatrix);
int[] cloumnarray = new int[i];
int[] rowarray = new int[i];
for (int z = 0; z <= i - 1; z++) {
cloumnarray[z] = z + 1;
rowarray[z] = z + 1;
}
for (int j = 0; j < i; j++) {
System.out.println(cloumnarray[j] * rowarray[j]);
}
}
I tried different options and can't get this to work properly.
public static void matrixmutilplication() {
String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows ");
int i = Integer.parseInt(thenumberofmatrix);
for (int a = 0; a <= i; a++) {
for (int b = 0; b <= i; b++) {
// top corner, don't print nothing
if (a == 0 && b == 0) System.out.print("\t");
// top row 0-1, 0-2, 0-3 etc... just 1,2,3...
else if (a == 0) {
System.out.print(b + "\t");
// last line, print extra line break
if (b == i)
System.out.print("\n");
}
// first column 1-0, 2-0, 3-0... just a + space (tabulator)
else if (b == 0) System.out.print(a + "\t");
// any other cases, are candidates to multiply and give result
else System.out.print(a*b + "\t");
}
//look this is out of scope of nested loops, so,
// in each a iteration, print line break :)
System.out.print("\n");
}
}
public static void main(String[] args) throws Exception {
matrixmutilplication();
}
OUTPUT (3)
1 2 3
1 1 2 3
2 2 4 6
3 3 6 9
OUTPUT (5)
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
But problem (for me) is the numbers are not padded in the natural order, so, to achieve your goal, exactly as in your demo, will need a bit of padding like this
public static void matrixmutilplication() {
String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows ");
int i = Integer.parseInt(thenumberofmatrix);
for (int a = 0; a <= i; a++) {
for (int b = 0; b <= i; b++) {
if (a == 0 && b == 0) System.out.print("\t");
else if (a == 0) {
System.out.print(String.format("%3s", b));
if (b == i)
System.out.print("\n");
}
else if (b == 0) System.out.print(a + "\t");
else System.out.print(String.format("%3s", a*b));
}
System.out.print("\n");
}
}
public static void main(String[] args) throws Exception {
matrixmutilplication();
}
OUTPUT (7)
1 2 3 4 5 6 7
1 1 2 3 4 5 6 7
2 2 4 6 8 10 12 14
3 3 6 9 12 15 18 21
4 4 8 12 16 20 24 28
5 5 10 15 20 25 30 35
6 6 12 18 24 30 36 42
7 7 14 21 28 35 42 49
What looks quite good :)
So this should be pretty simple.
public void matrixmutilplication() {
String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows ");
int i = Integer.parseInt(thenumberofmatrix);
for (int a = 0; a < i; a++) {
for (int b = 0; b < i; b++) {
System.out.print(a*b + "\t");
}
System.out.print("\n");
}
}
Whenever you're working with a matrix involving two arrays (especially if you're trying to a solve a problem that deals with patterns), you want to have a nested for loop like so:
for(int row = 0; row < numSelected; row++) {
for(int col = 0; col < numSelected; col++) {
...
}
}
That way, each cell in the matrix will be covered. Now using that, you can try multiplying the row index and the col index and storing that to the correct cell.