I am still a beginner in Java and Im really confused in nested loop and how to handle the rows and columns.
My goal is,
enter num: 5
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25.
This is my code so far,
System.out.print("Enter Number: ");
x = in.nextInt();
for(int a = 0; a < x; a++) //rows
{
for(int b = 0; b < x; b++) //columns
{
if(b % 2 == 0){
} else{
}
}
System.out.println();
}
To load a matrix into memory:
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}
System.out.println(Arrays.deepToString(matrix));
Related
Trying to figure out how to print a multiplication table.
The code I have right now is:
Scanner scan= new Scanner(System.in);
System.out.print("Please enter number 1-10:");
int n=scan.nextInt();
if(n<=10)
{
for(int m= 1;m<=n;m++)
{
for(int o= 1;o<=n;o++)
{
System.out.print(m*o+"\t");
}
System.out.println();
}
}
else
System.out.print("INVALID");
It prints out :
I'm trying to get it to print out as :
Please read the comments marked with // CHANGE HERE.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter number 1-10: ");
int n = scan.nextInt();
// CHANGE HERE - strict checking
if (n >= 1 && n <= 10)
{
// CHANGE HERE - added to print the first (header) row
for (int m = 0; m <= n; m++)
{
if (m == 0)
System.out.print("\t");
else
System.out.print(m + "\t");
}
System.out.println();
for (int m = 1; m <= n; m++)
{
// CHANGE HERE - added to print the first column
System.out.print(m + "\t");
for (int o = 1; o <= n; o++)
{
System.out.print(m * o + "\t");
}
System.out.println();
}
}
else
System.out.println("INVALID");
}
}
Try this.
Scanner scan = new Scanner(System.in);
System.out.print("Please enter number 1-10:");
int n = scan.nextInt();
if (n <= 10) {
for (int i = 1; i <= n; ++i)
System.out.print("\t" + i);
System.out.println();
for (int m = 1; m <= n; m++) {
System.out.print(m);
for (int o = 1; o <= n; o++) {
System.out.print("\t" + m * o);
}
System.out.println();
}
} else
System.out.print("INVALID");
output:
Please enter number 1-10: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
I need a code that reads the number of tests that will be done and then read values on a matrix for each test.
INPUT:
3
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
My attempt:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Number of tests");
int n = input.nextInt();
String[] name = new String[n];
int test[][] = new int[n][3];
for (int i = 0; i < n; i++) {
System.out.println("Name of the test" + i);
name[i] = input.nextLine();
System.out.println("Values");
for (int j = 0; j < 3; j++) {
test[i][j] = input.nextInt();
}
}
}
You need a 3-D array instead of a 2-D because for each of the test has a 2-D array under it.
Demo:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
final int ROWS = 3, COLS = 3;
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int test[][][] = new int[n][ROWS][COLS];
for (int i = 0; i < n; i++) {
System.out.println("Test " + (i + 1));
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
test[i][j][k] = input.nextInt();
}
}
}
// Display
System.out.println("Displaying...");
for (int i = 0; i < n; i++) {
System.out.println("Test " + (i + 1));
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
System.out.printf("%4d", test[i][j][k]);
}
System.out.println();
}
}
}
}
A sample run:
3
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
Displaying...
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
I have been trying to find a way to print an inverted pyramid that has a certain number sequence. The sequence needed is as follows as well as what I currently have.
The prompt asked to write a method that takes two numbers and create an inverted pyramid with the first row having a length of the first integer and starting with the number entered second. Then only have the sequence start at 1 after 9 is reached.
Needed: Currently Have:
1 2 4 7 2 7 4 1 2 3 4 5 6 7
3 5 8 3 8 5 8 9 1 2 3 4
6 9 4 9 6 5 6 7 8 9
1 5 1 7 1 2 3 4
6 2 8 5 6 7
3 9 8 9
1 1
static int plotTriangle(int a, int b){
int num = b;
for (int row = a; row >= 0; row--){
for (int i = a; i - row >= 0; i--){
System.out.print(" ");
num += (num+a-row);
num -= 2;
}
for (int i = 0; i <= row; i++){
num++;
while (num >= 10){
num -= 9;
}
System.out.print(num + " ");
}
System.out.println();
}
return 0;
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Enter length: ");
int l = in.nextInt();
System.out.print("Enter Start: ");
int s = in.nextInt();
int triangle = plotTriangle(l, s);
}
Try this:
public static void main(String[] args) {
int length = 7;
int[][] numbers = new int[length][length];
int count = 1;
for(int i = 0; i < numbers.length; ++i) {
for(int j = 0; j < (i+1); ++j) {
numbers[i][j] = count++;
if(count > 9)
count = 1;
}
}
for(int i = 0; i < numbers.length; ++i) {
for(int j = 0; j < numbers.length; ++j) {
if(numbers[j][i] == 0)
System.out.print(" ");
else
System.out.print(numbers[j][i]);
System.out.print(" ");
}
System.out.println();
}
}
This will give you your result. Note that I didn't include the dynamic part with the scanner. I used the length and the start-number as constants.
Explanation:
In the first loop I am basically just storing the numbers in an array. And in the second loop this array is printed in a different order.
Number Pattern
I am asked to enter a number rc, and based on rc construct this pattern. I am able to initialize the table but without the highlighted numbers:
int [][] num2 = new int [rc][rc];
counter = 1;
for(int i = 0; i < rc; i++){
if(i!=0)
counter--;
for(int j =0; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
}
Any hints or ideas?
You got it partially right. The numbers printed on each row are the same but the start point is incremented by 1 each time. Thus, you can use variable i again to shift it.
int [][] num2 = new int [rc][rc];
int counter = 1;
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
num2[i][(j + i) % rc] = counter++;
}
}
The following code is working fine for your problem.
int rc=5;
int [][] num2 = new int [rc][rc];
int counter = 1;
for(int i = 0; i < rc; i++){
for(int j =i; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
for(int k =0; k < rc; k++){
if(num2[i][k]==0){
num2 [i] [k] = counter;
counter++;
}
System.out.print(num2[i][k]+"\t");
}
System.out.println();
}
The logic behind my solution is:
First fill an array from 1 - N, where N is the user input (or
rc in this case):
Then, we check if it's not the first line, if it is, we simply print
the numbers in order.
Now, we have to know which numbers go first:
In the line 1 (remember it starts from 0), it must print the number at [1][4] in [1][0], so our loop substracts rc - i + j, this gives: 5 - 1 + 0, which in fact is index [4].
We know that after we've printed the last numbers first, we must continue the sequence, so we print index: [1][0] at [1][1] (Why 1, 2? Because otherwise we would get something like the example below, that's why we need to substract 1 to it
1 2 3 4 5
10 7 8 9 10
And that's it:
public class StrangePattern {
public static void main(String[] args) {
int rc = 5;
int number = 1;
int spaces = 0;
int[][] numbers = new int[rc][rc];
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
numbers[i][j] = number;
number++;
}
}
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
if (i != 0) {
if (j < i) {
System.out.print(numbers[i][rc - i + j] + "\t");
} else {
System.out.print(numbers[i][j - spaces] + "\t");
}
} else {
System.out.print(numbers[i][j] + "\t");
}
}
spaces++;
System.out.println();
}
}
}
Which provides this output:
1 2 3 4 5
10 6 7 8 9
14 15 11 12 13
18 19 20 16 17
22 23 24 25 21
And this one for rc = 3:
1 2 3
6 4 5
8 9 7
I added the plus 1 here: for (int i = 1; i < x + 1; i++) {
for (int j = 1; j < y + 1; j++)
so that the program would grab all the numbers except the one right before what I mean is..
If I enter 10 for the Maximum x value and 10 for the y (without the plus 1) this is what the output shows:
Would you like a power table or a multiplication table?
Press 1 for Power
Press 2 for Multiplication
2
Maximum x value?
10
Maximum y value?
10
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
I don't know why this wont work please help this is the code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int defaulVa = 1;
int defaulEx = 2;
System.out.println("Would you like a power table or a multiplication table?"
+ "\n Press 1 for Power"
+ "\n Press 2 for Multiplicatiion");
int a = sc.nextInt();
System.out.println("Maximum x value?");
int x = sc.nextInt();
System.out.println("Maximum y value?");
int y = sc.nextInt();
switch (a) {
case 1:
System.out.println("How would you like your base?"
+ "\nPress 1 for preset value"
+ "\nPress 2 for acending value");
int answer = sc.nextInt();
switch (answer) {
case 1:
System.out.println("What value?");
int value = sc.nextInt();
powerTable(x, y, value, defaulVa);
break;
case 2:
System.out.println("How would you like your exponent?"
+ "\nPress 1 for preset value"
+ "\nPress 2 for acending value");
int d = sc.nextInt();
switch (d) {
case 1:
System.out.println("What value?");
int d2 = sc.nextInt();
powerTable(x, y, defaulVa, d2);
break;
case 2:
powerTable(x, y, defaulVa, defaulEx);
break;
default:
break;
}
break;
default:
break;
}
break;
case 2:
multiplicationTable(x, y);
break;
default:
System.out.println("I see you can't follow simple instructions...");
System.exit(0);
break;
}
}
public static int[][] powerTable(int arrayDimensionX, int arrayDimensionY, int value, int value2) {
int[][] myArray;
myArray = new int[arrayDimensionX][arrayDimensionY];
for (int i = 1; i < myArray.length; i++) {
for (int j = 1; j < myArray[i].length; j++) {
if (value == 1) {
value = myArray[i][j] = i * j;
} else if (value != 1) {
value++;
}
myArray[i][j] = ((int) Math.pow(value, value2));
}
}
for (int i = 1; i < arrayDimensionX; i++) {
for (int j = 1; j < arrayDimensionY; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
return myArray;
}
public static int[][] multiplicationTable(int x, int y) {
int[][] myArray;
myArray = new int[x][y];
for (int i = 1; i < myArray.length; i++) {
for (int j = 1; j < myArray.length; j++) {
myArray[i][j] = i * j;
}
}
for (int i = 1; i < x + 1; i++) {
for (int j = 1; j < y + 1; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
return null;
}
}
Arrays are 0 indexed (they start at 0 and the last index is length - 1)
I would say...
for (int i = 1; i < x + 1; i++) {
for (int j = 1; j < y + 1; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
Should be...
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
You'd need to change the loop that builds the table to look something more like...
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray.length; j++) {
myArray[i][j] = (1 + i) * (1 + j); // <-- Modify the indices here instead...
}
}
Have a look at Arrays for more details
Why does your multiplicationTable function have an int[][] return type, when you return null? Would be better to just make it a void function, if you don't plan on having it return the array.
Anyway, since all the function does in print to the screen, the function is over complicated. Could be made much simpler, something like this:
public static void multiplicationTable(int x, int y)
{
for (int i = 1; i <= x; i++)
{
for (int j = 1; j <= y; j++)
System.out.print(i * j);
System.out.println();
}
}
But if you want it left as is, your ArrayIndexOutOfBounds exception is likely occuring here:
for (int i = 1; i < x + 1; i++) {
for (int j = 1; j < y + 1; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
Because x and y are the size of the array, making your loop go to i < x + 1 and i < y + 1 will throw the exception, because it is trying to access a non-existing index. If you change it to i < x and i < y it shouldn't throw the exception:
for (int i = 1; i < x; i++) {
for (int j = 1; j < y; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
However, even that will not give you the desired output, as it skips the first element of the array, because array indexes start at 0, not 1, meaning the multiplications for 1 will be missing from your table.