Java - 二元运算符的错误操作数类型' - '

时间:2017-04-27 23:26:17

标签: java bufferedreader

我遇到以下问题;

我定义sumwdouble,当我编译它时会出现以下错误:

C:\Users\Seth Wong\Desktop\cse110 project1\src\Assignment8.java:63: error: bad operand types for binary operator '-'
System.out.println("Ending balance:      $ "+b+sumd-sumw+sumi); //check here
                                                       ^
  first type:  String
  second type: double
1 error

代码:

import java.io.*;

public class Assignment8
 {
public static void main(String[] args)
    {try {
    //create an InputStreamReader obj first
    InputStreamReader ir = new InputStreamReader(System.in);

    //create a BufferedReader obj
    BufferedReader br = new BufferedReader(ir);

    //variales
    double b,a,d,w, interest;
    int count =0;
    double sumd =0.00;
    double sumw =0.00;
    double sumi =0.00; 

    //#1: prime read

    System.out.print("\nEnter the starting balance on the account: $");
    b = Double.parseDouble(br.readLine());

    System.out.print("\nEnter the annual interest rate on the account (e.g. .04): ");
    a = Double.parseDouble(br.readLine());

    for(int i=1; i<4; i++)
    {
        count++;


        //#3 read in the next data
        System.out.print("\nMonth"+i);
        System.out.print("\nTotal deposits for this month: $");
            d = Double.parseDouble(br.readLine());
            sumd += d;
        System.out.print("Total withdrawals for this month: $");
            w = Double.parseDouble(br.readLine());
            sumw += w;
            interest = (b+d)*a/12;
            sumi +=(b+d+interest)*a/12;
    }

    //avg = sum/count;
    System.out.println("\nQuarterly Savings Account Statement");
    System.out.println("\nStarting balance:    $ "+b);
    System.out.println("Total deposits:    + $ "+sumd);
    System.out.println("Total withdrawals: - $ "+sumw);
    System.out.println("Total interest:    + $   "+sumi);
    System.out.println(" ----------- ");
    System.out.println("Ending balance:      $ "+b+sumd-sumw+sumi); //check here sumw causes problem 


    }
    catch (IOException e)
    {
    System.out.println(e);
    }
    catch (ArithmeticException e)
    {
        System.out.println("Wrong, restart!");
    }
    }
}

1 个答案:

答案 0 :(得分:0)

在将结果与IOException连接之前添加几个括号应该可以解决问题。

String