有人能弄明白我的代码有什么问题吗?

时间:2013-11-20 20:22:29

标签: java

我运行了一个调试器和一切,但我找不到什么错!我运行代码,它接受一个asnwer,但如果我把b,它运行代码说“这不是A或B”

import java.util.Scanner;
public class messingAround {
public static void main(String[] args){
    Scanner ques = new Scanner(System.in);
    String question;                
    System.out.println("Would you like to:");
    System.out.println("A: Solve a math problem");
    System.out.println("B: Display Pi");
    System.out.println("Type A or B (no caps)");        
    question = ques.next();     
    if(!(question.equals("a")) || question.equals("b")){
            System.out.println("Sorry that isn't an A or a B");
            System.out.println("Try running the code again");       
    }else if(question.equals("a")) {
    double fnum, snum, answer;
    String type;
    Scanner intString = new Scanner(System.in);
    System.out.println("Enter your first number: ");
    fnum = intString.nextDouble();
    System.out.println("Enter your second number: ");
    snum = intString.nextDouble();
    System.out.println("What would you like to do? (+ - * /)");
    type = intString.next();
    if(type.equals("*")){
        answer = fnum * snum;
        System.out.println("The product is: " + answer);
    }else if(type.equals("+")) {
        answer = fnum + snum;
        System.out.println("The sum is: " + answer);
    }else if(type.equals("-")) {
        answer = fnum - snum;
        System.out.println("The difference is: " + answer);
    }else if(type.equals("/")) {
        answer = fnum / snum;
        System.out.println("The dividend is: " + answer);
        }
    }else if(question.equals("b")) {
                    System.out.println("3.14159265359");
        }   
    }
}

1 个答案:

答案 0 :(得分:4)

if(!(question.equals("a")) || question.equals("b")){
pare有点偏。试试这个:

if(!(question.equals("a") || question.equals("b"))){