有三个程序可以运行不同的功能,我被告知根据用户输入的编号选项调用它们。首先,我在最后添加一个switch语句之前打开我的程序。我在switch语句中得到的错误各不相同 - 对于每种情况,我究竟需要在那些braquets中放入什么?参数?
我收到错误,包括预期的错误类。
import java.util.Scanner;
import java.lang.Math;
public class HelperMethod{
public static boolean BookNumber(String a) {
char f;
int e, g, h;
int result = 0;
System.out.println ("Please enter a thirteen digit number");
String a = scanner.nextLine();
if (a.length() == 13){
for (int i = 0; i < 13; i ++) {
f = a.charAt(i);
e = Character.digit(f, 10);
if (i % 2 == 0) {
g = e * 1;
result = result + g;
} else {
g = e * 3;
result = result + g;
}
}
System.out.println ("The added sum of you numbers is " + result);
if (result % 10 == 0) {
System.out.println ("This combination IS a ISBN number");
} else {
System.out.println ("This is NOT an ISBN number");
}
} else {
System.out.println ("This combination is not thirteen digits long");
}
}
public static boolean NewtonsMethod (double guess) {
double guess, fX, fPrimeX, newGuess;
System.out.println ("enter in a value give");
guess = userInputScanner.nextDouble();
System.out.println ("Your guess is " + guess);
double guess;
while (true) {
fX = (6 * Math.pow (guess,4)) - (13 * Math.pow (guess,3)) - (18 * Math.pow (guess,2)) + (7 * guess) + 6;
fPrimeX = (24 * Math.pow (guess,3)) - (39 * Math.pow (guess,2)) - 36 * guess + 7;
newGuess = guess - (fX / fPrimeX);
System.out.println ("A possible root is " + newGuess);
if (Math.abs(newGuess - guess) < 0.00001) {
break;
} else {
guess = newGuess;
}
}
System.out.println ("The root is: " + newGuess);
}
public static void QuadraticFormula (double a, double b, double c) {
Scanner keyboard = new Scanner(System.in);
// Input
System.out.println("Please enter an a value:");
double a = keyboard.nextDouble();
System.out.println("Please enter a b value:");
double b = keyboard.nextDouble();
System.out.println("Please enter a c value:");
double c = keyboard.nextDouble();
// Calculations
double discriminant = b * b - 4 * a * c;
double rootOne = 0, rootTwo = 0;
// Option A
if (discriminant < 0) {
System.out.println("There are no roots!");
} else if (discriminant > 0){
rootOne = (-b + Math.sqrt(discriminant)) / (2 * a);
rootTwo = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("Your roots are: " + rootOne + " and " + rootTwo);
} else {
rootOne = (-b + Math.sqrt(discriminant)) / (2 * a);
System.out.println("Your unique root is: " + rootOne);
}
// Option B
if (discriminant < 0) {
System.out.println("There are no roots!");
} else {
rootOne = (-b + Math.sqrt(discriminant)) / (2 * a);
rootTwo = (-b - Math.sqrt(discriminant)) / (2 * a);
if (discriminant > 0) {
System.out.println("Your roots are: " + rootOne + " and " + rootTwo);
} else {
System.out.println("Your unique root is: " + rootOne);
}
}
}
public static void main (String[] args) {
Scanner userInputScanner = new Scanner (System.in);
System.out.println ("You have three options. press one for the quadratic Formula, 2 for the newtons Method, and 3 for an ISBN checker.");
int input = userInputScanner.nextInt();
switch (input) {
case 1:
NewtonsMethod (double guess);
break;
case 2:
BookNumber(String a);
break;
case 3:
QuadraticFormula (double a, double b, double c);
break;
}
}
}
答案 0 :(得分:0)
是的,这里有很多语法错误:
NewtonsMethod(double guess);
您正在尝试调用方法,但这看起来像是一个声明。您可以在其他地方进行正确的方法调用,例如:Math.sqrt(discriminant)
。所以这应该是这样的:
NewtonsMethods(guess);
然而,这些方法看起来根本不应该是参数。
public static boolean NewtonsMethod (double guess) {
double guess, fX, fPrimeX, newGuess;
System.out.println ("enter in a value give");
guess = userInputScanner.nextDouble();
...
当您调用它们时,没有任何内容可以传递给它们,并且您在方法体内收集这些变量的用户输入。这会导致您声明的方法局部变量与这些参数冲突,从而导致更多语法错误。所以,只是不要将任何参数传递给它们,你的声明看起来像:
public static boolean NewtonsMethod (double guess) {
您的退货类型也存在问题。使用布尔返回类型声明方法,但永远不返回值。在这里似乎不需要返回一个布尔值,所以我只是给它们一个void返回类型:
public static void NewtonsMethod (double guess) {
之后,我只看到其他三个语法错误。您有两个地方尝试使用尚未初始化的Scanner
。您可以在QuadraticFormula
和main
初始化它们,因此请遵循该模式。此外,您已经在牛顿方法中将变量guess
声明了两次。修复它。我相信,至少应该让你编译。我主要给你留下逻辑错误,除了这个突然出现在我身上的错误:你告诉用户按下的数字不会按你所说的去做。