How do you print out the division calculated for each pair of variables on the same line?

StackOverflow https://stackoverflow.com/questions/21844671

  •  13-10-2022
  •  | 
  •  

Вопрос

UPDATED

I am trying to make a grid that shows whether variable numOne and numTwo are divisible or not together. Basically, I want the outcome to look like this

       1          2          3         4        ....
1   tResult    tResult    tResult   tResult     ....
2   tResult    tResult    tResult   tResult      ....
3   tResult    tResult    tResult   tResult     ....
4   tResult    tResult    tResult   tResult    ....
5   tResult    tResult    tResult   tResult    .....

tResult represents (x%y) or (1%1) (2%1) and so on. And also, the grid can be larger or small depending on the input user writes for numOne and numTwo.

What I am trying to figure out is how layout this grid by using System.out.print() for each line, printing out the numOne in the beginning followed by the x amount of tResults, except the first one. This is probably not the best method out there, but since I am a beginner, the only thing I can do is print out stuff. Also, the problem I am having is that I am not getting the results I want.

The code I have is:

            int x=1;
    int y=1;
    int numOne; int numTwo;
    Scanner input=new
            java.util.Scanner(System.in);

    //First value
    System.out.println("First number:");
    numOne = input.nextInt();
    while (x<=numOne){
        System.out.print(" ");
        System.out.print(x+" ");
        x++;
    }

    //Second value
    System.out.println("Second number:");
    numTwo=input.nextInt();
    System.out.println(numTwo);
    int tResult=(x%y);
    while (y<=numTwo){
        System.out.print(" ");
        System.out.print(y);
        y++;
        System.out.print(tResult);
Это было полезно?

Решение

Updated answer:

Following is one way to achieve what you are trying to achieve using while loop.

public static void main(String[] args)
{
    int numOne; int numTwo;
    Scanner input= new java.util.Scanner(System.in);
    //Get First value
    System.out.println("First number:");
    numOne = input.nextInt();
    //Get Second value
    System.out.println("Second number:");
    numTwo=input.nextInt();
    input.close();
    int x=1;
    int y=1;
    System.out.print("     ");
    while(x<=numOne)
    {
        System.out.print(" [  x="+ x +" ] ");
        x++;
    }

    x=1;y=1;
    while(y<=numTwo)
    {
        System.out.println();
        System.out.print("[y="+ y +"]");
        while(x<=numOne)
        {
            int tResult=x%y;
            System.out.print(" [("+ x +"%"+ y +")"+tResult+"] ");
            x++;
        }
        y++;
        x=1;
    }
}

Sample output:

First number:
4
Second number:
5
      [  x=1 ]  [  x=2 ]  [  x=3 ]  [  x=4 ] 
[y=1] [(1%1)0]  [(2%1)0]  [(3%1)0]  [(4%1)0] 
[y=2] [(1%2)1]  [(2%2)0]  [(3%2)1]  [(4%2)0] 
[y=3] [(1%3)1]  [(2%3)2]  [(3%3)0]  [(4%3)1] 
[y=4] [(1%4)1]  [(2%4)2]  [(3%4)3]  [(4%4)0] 
[y=5] [(1%5)1]  [(2%5)2]  [(3%5)3]  [(4%5)4]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top