gpt4 book ai didi

java - 必需的: variable Found:value Error Java Text-Based Calculator

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

你好堆栈溢出。

我的代码有问题。目标是创建一个基于文本的计算器,该计算器可读取输入文件并处理方程式。第一个数字告诉程序那里有多少行。我想我正确地设置了那部分,计数是行数。这是到目前为止我得到的。

Scanner equationScan;
int count;
double a=0;
double b=0;
double calc1;

equationScan = new Scanner(new File("calculator.txt"));

count = equationScan.nextInt();

while(count > 0)
{
String line = equationScan.nextLine();

if(line.equals("sin"))
{
a = equationScan.nextDouble(); *Error shows up here. Req: variable Found: value
Math.sin(a)= calc1;
}
}

目标是使程序遵循多个“if”语句。我了解这一部分,但我无法克服此错误。文本文件的第一行读取一个整数,我试图查看该文件的第二行,该行读取double的sin并对其进行计算并存储它。帮助将不胜感激!

最佳答案

更改已在注释中。

package calculator;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class calc {

public static void main(String[] args) {



Scanner equationScan = null;
int count;
double a=0;
double b=0;
double calc1;

try { //optional: add a try catch block to catch FileNotFoundException exception
equationScan = new Scanner(new File("calculator.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

count = equationScan.nextInt();
equationScan.nextLine(); //add a nextline() according to what TOM pointed

while(count > 0)
{
String line = equationScan.nextLine();

if(line.equals("sin"))
{
String value=equationScan.nextLine(); //Parse the double to a string
a=Double.parseDouble(value);

calc1 = Math.sin(a) ; //Assignment should be at the right sign of the = operator
}
}


}
}

关于java - 必需的: variable Found:value Error Java Text-Based Calculator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26444521/

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