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.
java
add a comment |
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.
java
What part do you need help with? Getting user input? Storing the random values form
,x
, andb
and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generatesm
,x
, andb
, 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 ofjava.util.Random
and using itsnextInt(bound)
method.
– Ted Hopp
Nov 11 at 0:34
A friend told me ofjava.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 asint x = nextInt(100) + 1;
?
– J Cruz
Nov 11 at 0:48
1
Since you want numbers between -100 and +100, you'd probably wantint x = rand.nextInt(201) - 100;
(assumingrand
is yourRandom
object and the bounds of -100 and +100 are inclusive).
– Ted Hopp
Nov 11 at 0:50
add a comment |
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.
java
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
java
asked Nov 11 at 0:25
J Cruz
183
183
What part do you need help with? Getting user input? Storing the random values form
,x
, andb
and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generatesm
,x
, andb
, 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 ofjava.util.Random
and using itsnextInt(bound)
method.
– Ted Hopp
Nov 11 at 0:34
A friend told me ofjava.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 asint x = nextInt(100) + 1;
?
– J Cruz
Nov 11 at 0:48
1
Since you want numbers between -100 and +100, you'd probably wantint x = rand.nextInt(201) - 100;
(assumingrand
is yourRandom
object and the bounds of -100 and +100 are inclusive).
– Ted Hopp
Nov 11 at 0:50
add a comment |
What part do you need help with? Getting user input? Storing the random values form
,x
, andb
and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generatesm
,x
, andb
, 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 ofjava.util.Random
and using itsnextInt(bound)
method.
– Ted Hopp
Nov 11 at 0:34
A friend told me ofjava.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 asint x = nextInt(100) + 1;
?
– J Cruz
Nov 11 at 0:48
1
Since you want numbers between -100 and +100, you'd probably wantint x = rand.nextInt(201) - 100;
(assumingrand
is yourRandom
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
add a comment |
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);
}
}
}
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
add a comment |
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)
add a comment |
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);
}
}
}
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
add a comment |
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);
}
}
}
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
add a comment |
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);
}
}
}
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);
}
}
}
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 11 at 0:33
KYL3R
1,086214
1,086214
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What part do you need help with? Getting user input? Storing the random values for
m
,x
, andb
and later using them? Something else? Your loops don't make a lot of sense. You probably want a single loop that repeatedly generatesm
,x
, andb
, 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 ofjava.util.Random
and using itsnextInt(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 asint 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;
(assumingrand
is yourRandom
object and the bounds of -100 and +100 are inclusive).– Ted Hopp
Nov 11 at 0:50