我正在创建一个程序,您可以在其中输入格式为y = mx + c
的等式它将给出从-2到+2的y值。
用户可以输入的内容的示例是y = 2x + 5.
我该如何解决这个问题? 我想输入x的整数值 我不知道在哪里或如何开始。
答案 0 :(得分:0)
如果您只想输入x值的整数,可以使用下面的方法...该方法允许您选择渐变和y截距的值。
您可以使用此方法:
public static void rangeCalculator(int startPoint, int endPoint){
Scanner input = new Scanner(System.in);
System.out.print("Enter gradient:");
double gradient = input.nextDouble();
System.out.print("\nEnter intercept:");
double intercept = input.nextDouble();
for(int i=startPoint; i<=endPoint; i++){
System.out.println("y="+gradient+"x + " +intercept+"\t" + "input:"+i + " output:" + (gradient*i + intercept));
}
}
答案 1 :(得分:0)
import java.util.Scanner;
public class graphTester {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter equation: ");
String input = scanner.nextLine();
Character equalsChar = '=';
Character xChar = 'x';
int iEquals = 0;
int iX =0;
int iPlusOrSubtract = 0;
int[] xValues = {-2, -1, 0, 1, 2, 3, 4};
int[] yValues = new int[7];
int i = 0;
for (iEquals = 0; iEquals <= input.length(); iEquals++){
Character c1 = input.charAt(iEquals);
if (c1 == equalsChar){
System.out.println("Found equals at index: " + iEquals);
break;
}else{
}
}
for (iX = 0; iX <= input.length(); iX++){
Character c2 = input.charAt(iX);
if (c2 == xChar){
System.out.println("Found x at index: " + iX);
break;
}else{
}
}
String coEfficientString = input.substring(iEquals + 1, iX);
int coEfficient = Integer.parseInt(coEfficientString);
System.out.println("coEfficient: " + coEfficient);
String yInterceptString = input.substring(iX + 1, input.length());
int yIntercept = Integer.parseInt(yInterceptString);
System.out.println("Y-Intercept: " + yIntercept);
for (int value : xValues){
i++;
System.out.println("X-Value:" + value + " Y-Value:" + value*coEfficient);
yValues[i] = value*coEfficient;
}
System.out.println(yValues);
}
}
答案 2 :(得分:0)
import java.util.regex.*;
public class RegexTester {
public static void main(String[] args) {
String str2Check = "3x+2";
//Find x - \\d{1,}+[x-x]
//Find y-intercept [[\\+] | [\\-]]+\\d{1}
String regexStringCoefficient = "[[\\+] | [\\-]]+\\d{1}";
regexChecker(regexStringCoefficient, str2Check);
}
public static void regexChecker(String regexString, String str2Check){
Pattern checkRegex = Pattern.compile(regexString);
Matcher regexMacher = checkRegex.matcher(str2Check);
while (regexMacher.find()){
if (regexMacher.group().length() != 0){
System.out.println(regexMacher.group());
System.out.println("First Index: " + regexMacher.start());
System.out.println("Ending index: " + regexMacher.end());
}
}
}
}