程序无法看到正确的输入,非常新的java

时间:2015-02-08 22:19:49

标签: java java.util.scanner

请帮助,这将是一个简单而明显的问题,但我是新的编码和我自己在线教学。我的程序应该要求用户输入金额,然后应该要求用户输入帐户类型,然后应该输出具有年利息的金额值。

import java.io.IOException;
import java.util.Scanner;


public class BankInvestment {

    private static final String Account = null;

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        int S = 3;
        double D = 0.5;
        double C = 1.5;
        int L = 4;
        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in);
        @SuppressWarnings("resource")
        Scanner user_input = new Scanner( System.in );
        int n = input.nextInt();
        int Output = 0;
        String Accounttype = null;

        System.out.println("Please Enter Account Type(S,D,C,L): ");
        Accounttype = user_input.next();

        if (Accounttype == "S"){
            Output = n / 100 * S + n;
            System.out.println(Output);
        }
        else if (Account == "D"){
            Output = (int) (n / 100 * D + n);
            System.out.println(Output);
        }
        else if (Account == "C") {
            Output = (int) (n / 100 * C + n);
            System.out.println(Output);
        }
        else if (Account == "L"){
            Output = n / 100 * L + n;
            System.out.println(Output);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

不要将String或Java中的任何其他对象与==进行比较。这只是比较参考。使用.equals()方法进行有意义的比较。

更改此

if (Accounttype == "S"){
    Output = n / 100 * S + n;
    System.out.println(Output);
}

到这个

if (Accounttype.equals("S")){
    Output = n / 100 * S + n;
    System.out.println(Output);
}

答案 1 :(得分:0)

你需要在java中使用正确的比较,String是一个Object,这就是为什么你应该使用Accounttype.equals("L")的原因。 ==检查这是否是相同的参考不是..那些2将是2个不同的对象