【发布时间】:2014-12-12 00:41:51
【问题描述】:
所以我已经找到了三个必需的代码。它们是二次公式的代码、ISBN 检查器的代码和牛顿法的代码。我应该制作一个包含选项 1、2 和 3 的菜单,每个选项分别包含这些代码。
我想这意味着我需要不同的方法来解决这个问题,对吧?我从来没有真正被教导过——有人告诉我,我们必须始终将所有内容都放在 public class void main (String []args){ 中,而我只是被告知这有一些变化?
对于二次公式,信息是:返回 - void 和三个双精度数的参数,牛顿法:返回 - 双精度数和 1 个双精度数的参数,以及 ISBN 检查器:返回:布尔值和 1 个字符串的参数。我也不太了解参数的事情。帮助将不胜感激。我知道这在美学上看起来很糟糕,但是因为我现在的代码相对较短,所以我只是在完成后编辑样式。我也知道很多事情都是错误的,我花了很多时间试图弄清楚它们。
import Java.util.Scanner;
public class HelperMethod{
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();
if (input = 1){
}else if (input = 2) {
private class NewtonsMethod {
public static void NewtonsMethod(String[] args) {
Scanner userInputScanner = new Scanner (System.in);
double guess, fX, fPrimeX, newGuess;
System.out.println ("enter in a value give");
guess = userInputScanner.nextDouble();
System.out.println ("Your guess is " + 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);
}
}
}else{
private class BookNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char f;
int e, g, h;
int result = 0;
System.out.println ("Pleas 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");
}
}
}
}
}
}
【问题讨论】: