Learning java and can't figure out y=m*x+b









up vote
1
down vote

favorite












Creating an Algebra Tutor trying to create random integers to solve y=m*x+b. Searched and found a lot of users have worked with this equation but not with generating a random question to require an answer and check if correct. This is what I have written so far;



import java.util.Scanner;
class AlgebraTutor
private static Scanner in = new Scanner(System.in);
public static void main(String args)

for (int x = 0; x < 1; x++)
System.out.println("X = " + ((int)(Math.random() * 200) -99));


for (int m = 0; m < 1; m++)
System.out.println("M = " + ((int)(Math.random() * 200) -99));


for (int b = 0; b < 1; b++)
System.out.println("B = " + ((int)(Math.random() * 200) -99));






I believe I'm on the right path with generating numbers between -100 and 100. I just don't know what to do to move forward with combining them so that I can input an answer to the numbers created. My best guess was



System.out.print("Y = " + m * x + b);


But that didn't work the way I thought it would. I want to be able to input an answer and have it check true or false. Thanks for any advice to get past this.










share|improve this question





















  • What part do you need help with? Getting user input? Storing the random values for m, x, and b and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generates m, x, and b, then obtains user input and checks the answer. (You'll also need a way for the user to indicate that the loop should exit.) Also, if all you need is integers, try creating an instance of java.util.Random and using its nextInt(bound) method.
    – Ted Hopp
    Nov 11 at 0:34











  • A friend told me of java.util.Random but I had never heard of that or how to use it. I'm assuming there is more benefit to it though. Would that mean instead of what I used to get x, m, and b I would just create them as int x = nextInt(100) + 1; ?
    – J Cruz
    Nov 11 at 0:48






  • 1




    Since you want numbers between -100 and +100, you'd probably want int x = rand.nextInt(201) - 100; (assuming rand is your Random object and the bounds of -100 and +100 are inclusive).
    – Ted Hopp
    Nov 11 at 0:50














up vote
1
down vote

favorite












Creating an Algebra Tutor trying to create random integers to solve y=m*x+b. Searched and found a lot of users have worked with this equation but not with generating a random question to require an answer and check if correct. This is what I have written so far;



import java.util.Scanner;
class AlgebraTutor
private static Scanner in = new Scanner(System.in);
public static void main(String args)

for (int x = 0; x < 1; x++)
System.out.println("X = " + ((int)(Math.random() * 200) -99));


for (int m = 0; m < 1; m++)
System.out.println("M = " + ((int)(Math.random() * 200) -99));


for (int b = 0; b < 1; b++)
System.out.println("B = " + ((int)(Math.random() * 200) -99));






I believe I'm on the right path with generating numbers between -100 and 100. I just don't know what to do to move forward with combining them so that I can input an answer to the numbers created. My best guess was



System.out.print("Y = " + m * x + b);


But that didn't work the way I thought it would. I want to be able to input an answer and have it check true or false. Thanks for any advice to get past this.










share|improve this question





















  • What part do you need help with? Getting user input? Storing the random values for m, x, and b and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generates m, x, and b, then obtains user input and checks the answer. (You'll also need a way for the user to indicate that the loop should exit.) Also, if all you need is integers, try creating an instance of java.util.Random and using its nextInt(bound) method.
    – Ted Hopp
    Nov 11 at 0:34











  • A friend told me of java.util.Random but I had never heard of that or how to use it. I'm assuming there is more benefit to it though. Would that mean instead of what I used to get x, m, and b I would just create them as int x = nextInt(100) + 1; ?
    – J Cruz
    Nov 11 at 0:48






  • 1




    Since you want numbers between -100 and +100, you'd probably want int x = rand.nextInt(201) - 100; (assuming rand is your Random object and the bounds of -100 and +100 are inclusive).
    – Ted Hopp
    Nov 11 at 0:50












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Creating an Algebra Tutor trying to create random integers to solve y=m*x+b. Searched and found a lot of users have worked with this equation but not with generating a random question to require an answer and check if correct. This is what I have written so far;



import java.util.Scanner;
class AlgebraTutor
private static Scanner in = new Scanner(System.in);
public static void main(String args)

for (int x = 0; x < 1; x++)
System.out.println("X = " + ((int)(Math.random() * 200) -99));


for (int m = 0; m < 1; m++)
System.out.println("M = " + ((int)(Math.random() * 200) -99));


for (int b = 0; b < 1; b++)
System.out.println("B = " + ((int)(Math.random() * 200) -99));






I believe I'm on the right path with generating numbers between -100 and 100. I just don't know what to do to move forward with combining them so that I can input an answer to the numbers created. My best guess was



System.out.print("Y = " + m * x + b);


But that didn't work the way I thought it would. I want to be able to input an answer and have it check true or false. Thanks for any advice to get past this.










share|improve this question













Creating an Algebra Tutor trying to create random integers to solve y=m*x+b. Searched and found a lot of users have worked with this equation but not with generating a random question to require an answer and check if correct. This is what I have written so far;



import java.util.Scanner;
class AlgebraTutor
private static Scanner in = new Scanner(System.in);
public static void main(String args)

for (int x = 0; x < 1; x++)
System.out.println("X = " + ((int)(Math.random() * 200) -99));


for (int m = 0; m < 1; m++)
System.out.println("M = " + ((int)(Math.random() * 200) -99));


for (int b = 0; b < 1; b++)
System.out.println("B = " + ((int)(Math.random() * 200) -99));






I believe I'm on the right path with generating numbers between -100 and 100. I just don't know what to do to move forward with combining them so that I can input an answer to the numbers created. My best guess was



System.out.print("Y = " + m * x + b);


But that didn't work the way I thought it would. I want to be able to input an answer and have it check true or false. Thanks for any advice to get past this.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 0:25









J Cruz

183




183











  • What part do you need help with? Getting user input? Storing the random values for m, x, and b and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generates m, x, and b, then obtains user input and checks the answer. (You'll also need a way for the user to indicate that the loop should exit.) Also, if all you need is integers, try creating an instance of java.util.Random and using its nextInt(bound) method.
    – Ted Hopp
    Nov 11 at 0:34











  • A friend told me of java.util.Random but I had never heard of that or how to use it. I'm assuming there is more benefit to it though. Would that mean instead of what I used to get x, m, and b I would just create them as int x = nextInt(100) + 1; ?
    – J Cruz
    Nov 11 at 0:48






  • 1




    Since you want numbers between -100 and +100, you'd probably want int x = rand.nextInt(201) - 100; (assuming rand is your Random object and the bounds of -100 and +100 are inclusive).
    – Ted Hopp
    Nov 11 at 0:50
















  • What part do you need help with? Getting user input? Storing the random values for m, x, and b and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generates m, x, and b, then obtains user input and checks the answer. (You'll also need a way for the user to indicate that the loop should exit.) Also, if all you need is integers, try creating an instance of java.util.Random and using its nextInt(bound) method.
    – Ted Hopp
    Nov 11 at 0:34











  • A friend told me of java.util.Random but I had never heard of that or how to use it. I'm assuming there is more benefit to it though. Would that mean instead of what I used to get x, m, and b I would just create them as int x = nextInt(100) + 1; ?
    – J Cruz
    Nov 11 at 0:48






  • 1




    Since you want numbers between -100 and +100, you'd probably want int x = rand.nextInt(201) - 100; (assuming rand is your Random object and the bounds of -100 and +100 are inclusive).
    – Ted Hopp
    Nov 11 at 0:50















What part do you need help with? Getting user input? Storing the random values for m, x, and b and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generates m, x, and b, then obtains user input and checks the answer. (You'll also need a way for the user to indicate that the loop should exit.) Also, if all you need is integers, try creating an instance of java.util.Random and using its nextInt(bound) method.
– Ted Hopp
Nov 11 at 0:34





What part do you need help with? Getting user input? Storing the random values for m, x, and b and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generates m, x, and b, then obtains user input and checks the answer. (You'll also need a way for the user to indicate that the loop should exit.) Also, if all you need is integers, try creating an instance of java.util.Random and using its nextInt(bound) method.
– Ted Hopp
Nov 11 at 0:34













A friend told me of java.util.Random but I had never heard of that or how to use it. I'm assuming there is more benefit to it though. Would that mean instead of what I used to get x, m, and b I would just create them as int x = nextInt(100) + 1; ?
– J Cruz
Nov 11 at 0:48




A friend told me of java.util.Random but I had never heard of that or how to use it. I'm assuming there is more benefit to it though. Would that mean instead of what I used to get x, m, and b I would just create them as int x = nextInt(100) + 1; ?
– J Cruz
Nov 11 at 0:48




1




1




Since you want numbers between -100 and +100, you'd probably want int x = rand.nextInt(201) - 100; (assuming rand is your Random object and the bounds of -100 and +100 are inclusive).
– Ted Hopp
Nov 11 at 0:50




Since you want numbers between -100 and +100, you'd probably want int x = rand.nextInt(201) - 100; (assuming rand is your Random object and the bounds of -100 and +100 are inclusive).
– Ted Hopp
Nov 11 at 0:50












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You need to store the values in variables for example:



int x = ((int)(Math.random() * 200) -99));


Also your for loops are unnecessary in that they only run once so you could just run the program instead of looping once.



so in the end your code should be something like this:



import java.util.Scanner;
class AlgebraTutor
private static Scanner in = new Scanner(System.in);
public static void main(String args)
int x = ((int)(Math.random() * 200) -99);
int m = ((int)(Math.random() * 200) -99);
int b = ((int)(Math.random() * 200) -99);
System.out.println("X = " + x);
System.out.println("M = " + m);
System.out.println("B = " + b);
System.out.println("Y = " + m*x+b);





And to have it check the answer if it is true or false you would have to do something like this:



import java.util.Scanner;
class AlgebraTutor
private static Scanner in = new Scanner(System.in);
public static void main(String args)
int x = ((int)(Math.random() * 200) -99);
int m = ((int)(Math.random() * 200) -99);
int b = ((int)(Math.random() * 200) -99);
int y = m*x+b;
System.out.println("X = " + x);
System.out.println("M = " + m);
System.out.println("B = " + b);
int INPUT = in.nextInt();
if(INPUT == y)
System.out.println("You Got It Right!");
System.out.println("Y = " + y);
else
System.out.println("You Got It Wrong :(");
System.out.println("The Answer was: Y = " + y);









share|improve this answer






















  • println("Y = " + m*x+b) won't work right, since both + operators are string concatenation. You need a parenthesis so the second + becomes an addition operator: println("Y = " + (m*x+b))
    – Andreas
    Nov 11 at 1:32

















up vote
0
down vote













The variables x,m and b in your example are in the scope of each for-loop but die after the } of the loops.



int x = (int)(Math.random() * 200) -99); // these variables will live long enough.
int m = (int)(Math.random() * 200) -99);
int b = (int)(Math.random() * 200) -99);

System.out.println("X = " + x);
System.out.println("M = " + m);
System.out.println("B = " + b);


System.out.print("Y = " + m * x + b);


This should work, but note to be careful when you try to divide these numbers like "x/b" - you will need a cast to float (google division by integer)






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244737%2flearning-java-and-cant-figure-out-y-mxb%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You need to store the values in variables for example:



    int x = ((int)(Math.random() * 200) -99));


    Also your for loops are unnecessary in that they only run once so you could just run the program instead of looping once.



    so in the end your code should be something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    System.out.println("Y = " + m*x+b);





    And to have it check the answer if it is true or false you would have to do something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    int y = m*x+b;
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    int INPUT = in.nextInt();
    if(INPUT == y)
    System.out.println("You Got It Right!");
    System.out.println("Y = " + y);
    else
    System.out.println("You Got It Wrong :(");
    System.out.println("The Answer was: Y = " + y);









    share|improve this answer






















    • println("Y = " + m*x+b) won't work right, since both + operators are string concatenation. You need a parenthesis so the second + becomes an addition operator: println("Y = " + (m*x+b))
      – Andreas
      Nov 11 at 1:32














    up vote
    1
    down vote



    accepted










    You need to store the values in variables for example:



    int x = ((int)(Math.random() * 200) -99));


    Also your for loops are unnecessary in that they only run once so you could just run the program instead of looping once.



    so in the end your code should be something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    System.out.println("Y = " + m*x+b);





    And to have it check the answer if it is true or false you would have to do something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    int y = m*x+b;
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    int INPUT = in.nextInt();
    if(INPUT == y)
    System.out.println("You Got It Right!");
    System.out.println("Y = " + y);
    else
    System.out.println("You Got It Wrong :(");
    System.out.println("The Answer was: Y = " + y);









    share|improve this answer






















    • println("Y = " + m*x+b) won't work right, since both + operators are string concatenation. You need a parenthesis so the second + becomes an addition operator: println("Y = " + (m*x+b))
      – Andreas
      Nov 11 at 1:32












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    You need to store the values in variables for example:



    int x = ((int)(Math.random() * 200) -99));


    Also your for loops are unnecessary in that they only run once so you could just run the program instead of looping once.



    so in the end your code should be something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    System.out.println("Y = " + m*x+b);





    And to have it check the answer if it is true or false you would have to do something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    int y = m*x+b;
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    int INPUT = in.nextInt();
    if(INPUT == y)
    System.out.println("You Got It Right!");
    System.out.println("Y = " + y);
    else
    System.out.println("You Got It Wrong :(");
    System.out.println("The Answer was: Y = " + y);









    share|improve this answer














    You need to store the values in variables for example:



    int x = ((int)(Math.random() * 200) -99));


    Also your for loops are unnecessary in that they only run once so you could just run the program instead of looping once.



    so in the end your code should be something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    System.out.println("Y = " + m*x+b);





    And to have it check the answer if it is true or false you would have to do something like this:



    import java.util.Scanner;
    class AlgebraTutor
    private static Scanner in = new Scanner(System.in);
    public static void main(String args)
    int x = ((int)(Math.random() * 200) -99);
    int m = ((int)(Math.random() * 200) -99);
    int b = ((int)(Math.random() * 200) -99);
    int y = m*x+b;
    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);
    int INPUT = in.nextInt();
    if(INPUT == y)
    System.out.println("You Got It Right!");
    System.out.println("Y = " + y);
    else
    System.out.println("You Got It Wrong :(");
    System.out.println("The Answer was: Y = " + y);










    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 0:41

























    answered Nov 11 at 0:33









    Dragsaw Dragon

    446




    446











    • println("Y = " + m*x+b) won't work right, since both + operators are string concatenation. You need a parenthesis so the second + becomes an addition operator: println("Y = " + (m*x+b))
      – Andreas
      Nov 11 at 1:32
















    • println("Y = " + m*x+b) won't work right, since both + operators are string concatenation. You need a parenthesis so the second + becomes an addition operator: println("Y = " + (m*x+b))
      – Andreas
      Nov 11 at 1:32















    println("Y = " + m*x+b) won't work right, since both + operators are string concatenation. You need a parenthesis so the second + becomes an addition operator: println("Y = " + (m*x+b))
    – Andreas
    Nov 11 at 1:32




    println("Y = " + m*x+b) won't work right, since both + operators are string concatenation. You need a parenthesis so the second + becomes an addition operator: println("Y = " + (m*x+b))
    – Andreas
    Nov 11 at 1:32












    up vote
    0
    down vote













    The variables x,m and b in your example are in the scope of each for-loop but die after the } of the loops.



    int x = (int)(Math.random() * 200) -99); // these variables will live long enough.
    int m = (int)(Math.random() * 200) -99);
    int b = (int)(Math.random() * 200) -99);

    System.out.println("X = " + x);
    System.out.println("M = " + m);
    System.out.println("B = " + b);


    System.out.print("Y = " + m * x + b);


    This should work, but note to be careful when you try to divide these numbers like "x/b" - you will need a cast to float (google division by integer)






    share|improve this answer
























      up vote
      0
      down vote













      The variables x,m and b in your example are in the scope of each for-loop but die after the } of the loops.



      int x = (int)(Math.random() * 200) -99); // these variables will live long enough.
      int m = (int)(Math.random() * 200) -99);
      int b = (int)(Math.random() * 200) -99);

      System.out.println("X = " + x);
      System.out.println("M = " + m);
      System.out.println("B = " + b);


      System.out.print("Y = " + m * x + b);


      This should work, but note to be careful when you try to divide these numbers like "x/b" - you will need a cast to float (google division by integer)






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        The variables x,m and b in your example are in the scope of each for-loop but die after the } of the loops.



        int x = (int)(Math.random() * 200) -99); // these variables will live long enough.
        int m = (int)(Math.random() * 200) -99);
        int b = (int)(Math.random() * 200) -99);

        System.out.println("X = " + x);
        System.out.println("M = " + m);
        System.out.println("B = " + b);


        System.out.print("Y = " + m * x + b);


        This should work, but note to be careful when you try to divide these numbers like "x/b" - you will need a cast to float (google division by integer)






        share|improve this answer












        The variables x,m and b in your example are in the scope of each for-loop but die after the } of the loops.



        int x = (int)(Math.random() * 200) -99); // these variables will live long enough.
        int m = (int)(Math.random() * 200) -99);
        int b = (int)(Math.random() * 200) -99);

        System.out.println("X = " + x);
        System.out.println("M = " + m);
        System.out.println("B = " + b);


        System.out.print("Y = " + m * x + b);


        This should work, but note to be careful when you try to divide these numbers like "x/b" - you will need a cast to float (google division by integer)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 0:33









        KYL3R

        1,086214




        1,086214



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244737%2flearning-java-and-cant-figure-out-y-mxb%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto