0
問題:當我試圖將int轉換爲double時,它顯示int無法解析爲變量的錯誤。該程序將輸入二次方程作爲輸入,並以此格式提取aX2-bX-c = 0的係數並求解二次方程。但是在從int到double的轉換中存在一些錯誤。int無法解析爲變量java
計劃:
public static String quad (final String equation)
{
final String regex = "([+-]?\\d+)X2([+-]\\d+)X([+-]\\d+)=0";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(equation);
if (matcher.matches()) {
int a1 = Integer.parseInt(matcher.group(1));
int b1 = Integer.parseInt(matcher.group(2));
int c1 = Integer.parseInt(matcher.group(3));
// System.out.println("a=" + a + "; b=" + b + "; c=" + c);
}
double a = (double) a1; // error message a1 cannot resolve into variable
double b = (double) b1; // error message b1 cannot resolve into variable
double c = (double) c1; // error message c1 cannot resolve into variable
double r1 = 0;
double r2 = 0;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0){
// r = -b/2 * a;
r1 = (-b + Math.sqrt(discriminant))/(2 * a);
r2 = (-b - Math.sqrt(discriminant))/(2 * a);
// System.out.println("Real roots " + r1 + " and " + r2);
}
if (discriminant == 0){
// System.out.println("One root " +r1);
r1 = -b/(2 * a);
r2 = -b/(2 * a);
}
if (discriminant < 0){
// System.out.println(" no real root");
}
String t1 = String.valueOf(r1);
String t2 = String.valueOf(r2);
String t3 ;
t3 = t1+" "+t2;
return t3;
}