import java.util.*;
public class projectOneAdamYoung {
public static void main(String[] args){
System.out.println("Welcome to the AP Computer Science calculator!!");
Scanner kb = new Scanner(System.in);
System.out.print("Enter an expression, or quit to exit: ");
String line = kb.nextLine();
Scanner input = new Scanner(line);
int redLight = 0;
while(redLight != 1) {
if(line.equalsIgnoreCase("quit")){
System.out.println("Thanks for using this calculator!");
redLight = 1;
}else {
if (input.hasNextDouble()) {
String numFirst = input.next();
String opperator = input.next();
String numSecond = input.next();
double num1 = Double.parseDouble(numFirst);
double num2 = Double.parseDouble(numSecond);
if (opperator.equals("+")) {
calcAdd();
}
if (opperator.equals("*")) {
calcMult();
}
if (opperator.equals("-")) {
calcSub();
}
if (opperator.equals("/")) {
calcDiv();
}
if (opperator.equals("^")) {
calcExp();
}
}else {
String opperator = input.next();
String numFirst = input.next();
double num1 = Double.parseDouble(numFirst);
if (opperator.equals("|")) {
calcAbs();
}
if (opperator.equals("v")) {
calcSqrt();
}
if (opperator.equals("~")) {
calcRound();
}
if (opperator.equals("s")) {
calcSin();
}
if (opperator.equals("c")) {
calcCos();
}
if (opperator.equals("t")) {
calcTan();
}
}
}
}
}
public static void calcAdd() {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
public static void calcSub() {
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
}
public static void calcMult() {
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
}
public static void calcDiv() {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
}
public static void calcExp() {
System.out.println(num1 + " ^ " + num2 + " = " + (pow(num1, num2)));
}
public static void calcAbs() {
System.out.println(num1 + " | " + " = " + (abs(num1)));
}
public static void calcSqrt() {
System.out.println(num1 + " v " + " = " + (sqrt(num1)));
}
public static void calcRound() {
System.out.println(num1 + " ~ " + " = " + (round(num1)));
}
public static void calcSin() {
System.out.println(num1 + " s " + " = " + (sin(num1)));
}
public static void calcCos() {
System.out.println(num1 + " c " + " = " + (cos(num1)));
}
public static void calcTan() {
System.out.println(num1 + " t " + " = " + (tan(num1)));
}
}
我正在为学校开发一个计算器项目,需要帮助。这是我的所有代码,它没有编译,因为我用来完成操作的变量是不同的方法。我无法弄清楚如何让这项工作看着别人的工作。工作
答案 0 :(得分:2)
我同意Luiggi的评论全心全意(但还没有直接添加到该主题的观点)。
尝试调用,例如,calcAdd,如:
calcAdd(num1, num2);
并将其定义为:
public static void calcAdd(double num1, double num2) {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
验证这是否修复了calcAdd的编译错误,然后对calcSub等进行了类似的修复。这将需要几分钟的打字,但它将更近一步。
答案 1 :(得分:0)
您需要阅读passing parameters and accepting arguments。理解变量scope对学习也很重要。在此之前,您可以松散地使用:您在一组{
和}
内声明的变量将是该区域的本地变量。
此示例可能会帮助您了解更多信息。
public class Pass {
public static void main(String[] args) {
// variables to pass.
// scope : everything inside of the main method.
double one = 1.0;
double two = 2.0;
for (int a = 0; a < one; a++)
{
// scope of a = this for loop
double three = 3.0; // variable three declared in this loop, scope = this loop.
Pass.methodOne(one); // passing a parameter (double one) to methodOne.
Pass.methodTwo(two); // passing a parameter (double two) to methodTwo.
}
// a = 2; This will not work because this isn't inside the scope of a.
}
static double c; // double c declared in class, scope whole class.
// Note static keyword for access in static methods such as main.
public static void methodOne(double d) { // accepting a parameter and calling it d
//double d declared as argument variable, scope = this method.
c = d; // local variable d is assigned to class variable c.
System.out.println("The number methodOne received is " + c + ".");
}
public static void methodTwo(double d) { // accepting a parameter and calling it d
c = d; // same c used in methodOne, different d.
System.out.println("The number methodTwo received is " + c + ".");
}
}
答案 2 :(得分:0)
import java.util.*;
public class ProjectOneAdamYoung {
private double num1;
private double num2;
public static void main(String[] args) {
new ProjectOneAdamYoung().calculate();
}
public void calculate() {
System.out.println("Welcome to the AP Computer Science calculator!!");
Scanner kb = new Scanner(System.in);
System.out.print("Enter an expression, or quit to exit: ");
String line = kb.nextLine();
Scanner input = new Scanner(line);
int redLight = 0;
while (redLight != 1) {
if (line.equalsIgnoreCase("quit")) {
System.out.println("Thanks for using this calculator!");
redLight = 1;
} else {
if (input.hasNextDouble()) {
String numFirst = input.next();
String opperator = input.next();
String numSecond = input.next();
num1 = Double.parseDouble(numFirst);
num2 = Double.parseDouble(numSecond);
if (opperator.equals("+")) {
calcAdd();
}
if (opperator.equals("*")) {
calcMult();
}
if (opperator.equals("-")) {
calcSub();
}
if (opperator.equals("/")) {
calcDiv();
}
if (opperator.equals("^")) {
calcExp();
}
} else {
String opperator = input.next();
String numFirst = input.next();
num1 = Double.parseDouble(numFirst);
if (opperator.equals("|")) {
calcAbs();
}
if (opperator.equals("v")) {
calcSqrt();
}
if (opperator.equals("~")) {
calcRound();
}
if (opperator.equals("s")) {
calcSin();
}
if (opperator.equals("c")) {
calcCos();
}
if (opperator.equals("t")) {
calcTan();
}
}
}
redLight = 1;
}
}
public void calcAdd() {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}
public void calcSub() {
System.out.println(num1 + " - " + num2 + " = " + (num1 - num2));
}
public void calcMult() {
System.out.println(num1 + " * " + num2 + " = " + (num1 * num2));
}
public void calcDiv() {
System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));
}
public void calcExp() {
System.out.println(num1 + " ^ " + num2 + " = " + (Math.pow(num1, num2)));
}
public void calcAbs() {
System.out.println(num1 + " | " + " = " + (Math.abs(num1)));
}
public void calcSqrt() {
System.out.println(num1 + " v " + " = " + (Math.sqrt(num1)));
}
public void calcRound() {
System.out.println(num1 + " ~ " + " = " + (Math.round(num1)));
}
public void calcSin() {
System.out.println(num1 + " s " + " = " + (Math.sin(num1)));
}
public void calcCos() {
System.out.println(num1 + " c " + " = " + (Math.cos(num1)));
}
public void calcTan() {
System.out.println(num1 + " t " + " = " + (Math.tan(num1)));
}
}
我改变了你的课程,做了你想做的事。但是,这不是更好的方法。