gpt4 book ai didi

java - 找不到符号(计算器)

转载 作者:行者123 更新时间:2023-12-01 11:35:02 29 4
gpt4 key购买 nike

我最近编写了一个计算器程序,但出现了三个错误。这是代码:

import java.util.Scanner;
public class mathyStuff {
public static void main(String[] args) throws InterruptedException {
Scanner raw = new Scanner(System.in);
String input = raw.nextLine();
int y = 0;
while (y < input.length()) {
if (input.substring(y, y+1) == "+" || input.
substring(y, y+1) == "-" || input.substring(y, y+1) == "/" || input.substring(y, y+1) == "*") {
String x = input.substring(y, y+1);
int z1 = Integer.parseInt(input.substring(0,y));
int z2 = Integer.parseInt(input.substring(y+1, 0));
}
else {
y = y + 1;
}
}
math(z1,x,z2);
}
public static void math (int num1, String op, int num2) throws InterruptedException {
if (op == "+") {
System.out.println(String.valueOf(num1 + num2));
}
if (op == "-") {
System.out.println(String.valueOf(num1 - num2));
}
if (op == "*") {
System.out.println(String.valueOf(num1 * num2));
}
if (op == "/") {
System.out.println(String.valueOf(num1 / num2));
}
}
}

错误如下:

Compilation Errors Detected

Line: 18
cannot find symbol
symbol: variable z1
location: class mathyStuff

Line: 18
cannot find symbol
symbol: variable x
location: class mathyStuff

Line: 18
cannot find symbol
symbol: variable z2
location: class mathyStuff

我目前正在使用一个名为 browxy 的网站,这是一个在线 java 编译器。是的,我知道。下载 eclipse 。我无法随身携带计算机,所以我用它来代替。

最佳答案

这是一个范围问题。

您无法访问 x、z1 和 z2,因为它们是在 while 循环内声明的,但您试图在 while 循环外访问它们。

您可能希望将 math 函数调用移至 while 循环内的 if block 内。

关于java - 找不到符号(计算器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30088516/

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