gpt4 book ai didi

java - 不同方法中的变量放在一起

转载 作者:行者123 更新时间:2023-12-02 05:16:25 26 4
gpt4 key购买 nike

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)));
}


}

我正在为学校开发一个计算器项目,需要帮助。这是我的所有代码,它无法编译,因为我用来完成操作的变量位于不同的方法中。我不知道如何通过其他人的工作来完成这项工作。

最佳答案

全心全意同意 Luiggi 的评论(但还没有直接添加到该线程的观点)。

尝试调用,例如 calcAdd,如下所示:

  calcAdd(num1, num2);

并将其定义为:

  public static void calcAdd(double num1, double num2) {
System.out.println(num1 + " + " + num2 + " = " + (num1 + num2));
}

验证这是否修复了 calcAdd 的编译错误,然后对 calcSub 等进行类似的修复。这将需要几分钟的打字时间,但会更近一步。

关于java - 不同方法中的变量放在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26913451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com