SaraMaeBee
SaraMaeBee

Reputation: 95

Can someone figure out whats wrong with my code?

I've run a debugger and everything, but I can't find out whats wrong! I run the code, and it accepts a as an asnwer, but if i put b, it runs the code saying "This is not an A or 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");
        }   
    }
}

Upvotes: 0

Views: 98

Answers (1)

iluxa
iluxa

Reputation: 6969

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

parens are a bit off. Try this:

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

Upvotes: 4

Related Questions