Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Ground Controller Lvl 1
Message 1 of 2

Beginner Java - Help :)

Hello everyone!

I'm a beginner in Java and are struggeling with an exercise where I shall make a menu for the straight line equation and then plot a graph for positive values for x. 

The most of it works now but not if a choose higher values, then it doesn't work...

I'm not sure if it is allowed to paste code in here to get help but you can comment or delete it if it's not...

Here is my code so far..

 

 

package equation1;
import java.util.Scanner;

public class Equation1 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("The square line equation");
System.out.println("---------------------");
System.out.println("");

boolean run = true;
int val;
int k = 2;
int m = -5;
int n = 6;
int max = 0;
int min = 0;
int[] y = new int[n];

//as long as while = true while will be run but when you choose val = 0 it stops

while (run) {
System.out.println(" ");
System.out.println("1. Change the value for k (Present k = " + k + ")");
System.out.println("2. Change the value for m (Present m = " + m + ")");
System.out.println("3. Change the amount of dots which are plotted (Present " + n + ")");
System.out.println("4. Plot the line");
System.out.println("0. Quit");
System.out.println(" ");
System.out.print("Your choice: ");

val = input.nextInt();

switch (val) {
case 1:
System.out.print("Enter a new value for k (Present k = " + k + "):");
k = input.nextInt();
break;

case 2:
System.out.print("Enter a new value for m (Present m = " + m + ")");
m = input.nextInt();
break;

case 3:
System.out.print("How many dots should be printed (Present = " + n + "):");
n = input.nextInt();
while (n < 0){
System.out.print("Enter a acceptable value (must be positive): ");
n = input.nextInt();
}
break;

case 4:
// prints the equation
if (m > 0) {
System.out.println("The equation is: y(x)=" + k + "x+" + m);
} else {
System.out.println("The equation is: y(x)=" + k + "x" + m);
}
System.out.println("-------------------------");
// defines the value for max and min for y
if (k < 0) {
max = (k * 1) + m;
min = (k * n) + m;
} else {
max = (k * n) + m;
min = (k * 1) + m;
}
// calculates the y-value and store it in an array for y for all 0<=x<=n
for (int b = 0; b < n; b++) {
y[b] = (k * (b + 1)) + m;
}
// writes the beginning of the y-line which are the same for every function
System.out.println(" ^");
System.out.println(" |");

// writes the x-line
for (int i = max; i >= min; i--) {
// prints the x-line in the graph + (special code to print equation y[0])
if (i == 0) {
System.out.print(" --|");
for (int u = 0; u < n; u++) {
if (0 == y[u]) {
System.out.print("--*");
} else {
System.out.print("--+");
}
}
System.out.print("-->");
// print for values with 2 numbers
} else if (i >= 10 || (i < -1 && i > -10)) {
System.out.print(" " + i + " |");
// prints the values under the x-line + (special code to print equation y[-1])
} else if (i == -1) {
System.out.print(" " + i + " |");
for (int j = 1; j <= n; j++) {
if (j < 10) {
if (i == y[j - 1]) {
System.out.print(" *");
} else {
System.out.print(" " + j);
}
} else {
if (i == y[j - 1]) {
System.out.print(" *");
} else {
System.out.print(" " + j);
}

}
}
} else if (0 < i && i < 10) {
System.out.print(" " + i + " |");
} else {
System.out.print(i + " |");
}
// as long as i is not equal to 0 and -1 the dot will be plotted at the wright position
if (i != -1 && i != 0) {
for (int j = 1; j <= n; j++) {
if (i == y[j - 1]) {
System.out.print(" *");
} else {
System.out.print(" ");
}
}
}
System.out.println("");
}
System.out.println("");
break;
case 0:
System.out.println("Welcome back!");
vidare = false;
break;
}

}
}
}

1 Reply
Commander Lvl 2
Message 2 of 2

Re: Beginner Java - Help :)

>The most of it works now but not if a choose higher values, then it doesn't work...

1. What are input values which "don't work"?

2. In what way does it "not work"?

 

And ideally, what are a set of input values which "work", and what results do you get for those values?

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!