为什么我的Java程序的最后一个输出行不显示?

时间:2011-10-23 22:07:37

标签: java eclipse

  

可能重复:
  In java, in this program it suddenly stops running properly at a certain point of code? but it compiles? any ideas what could be the issue?

import java.io.IOException;
import java.util.*;

public class task2 {
    public static void main (String [] args) throws IOException {
        int a;
        int b;
        String y;
        String x;
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter number A:");
        a = input.nextInt();

        System.out.println("\nPlease enter number B:");
        b = input.nextInt();

        System.out.println("\nLastly, enter A if you wish it to be the dividor and/or subtractor, or if you wish it to be B, please enter B :");
        y=input.next();

        System.out.println("\nWhat would you like to do? Multiply (*), Divide (/), Subtract (-) or Add (+)? Please enter the symbol of which process you would like to have completed:");
        x=input.next();

        if (y.equals("b"+"B")) {
            if (x.equals("*")) {
                System.out.println("\nThe product of these numbers is:" + (a*b));}
            else if (x.equals("/")) {
                System.out.println("\nThe quotient of these numbers is:" + (a/b));}
            else if (x.equals("+")) {
                System.out.println("\nThe sum of these numbers is:" + (a+b));}
            else if (x.equals("-")) {
                System.out.println("\nThe difference of these numbers is:" + (a-b));
            }
        }

        else 
        if (y.equals("a"+"A")){
            if (x.equals("*")) {
                System.out.println("\nThe product of these numbers is:" + (b*a));}
            else if (x.equals("/")) {
                System.out.println("\nThe quotient of these numbers is:" + (b/a));}
            else if (x.equals("+")) {
                System.out.println("\nThe sum of these numbers is:" + (b+a));}
            else if (x.equals("-")) {
                System.out.println("\nThe difference of these numbers is:" + ((b-a)));
            }
        }
    }
}

一个小程序,用于对一组两个数字进行计算。我想要的是用户输入他们想要的操作数量和类型(包括订单)。 计算结果不显示?有任何想法吗?所有人都非常感谢!

1 个答案:

答案 0 :(得分:1)

您将y和x与A + a和B + b进行比较 你应该做的是:

(y.equals("B") || y.equals("b"))

将管道||用作OR语句,而不是将其与+

连接

这是工作代码。

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

    public class SOScrap {
    public static void main (String [] args) throws IOException {
    int a;
    int b;
    String y;
    String x;
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter number A:");
    a = input.nextInt();

    System.out.println("\nPlease enter number B:");
    b = input.nextInt();

    System.out.println("\nLastly, enter A if you wish it to be the dividor and/or subtractor, or if you wish it to be B, please enter B :");
    y=input.next();

    System.out.println("\nWhat would you like to do? Multiply (*), Divide (/), Subtract (-) or Add (+)? Please enter the symbol of which process you would like to have completed:");
    x=input.next();


    if (y.equals("B") || y.equals("b")) {

    if (    x.equals("*")) {
    System.out.println("\nThe product of these numbers is:" + (a*b));}
    else
    if (x.equals("/")) {
    System.out.println("\nThe quotient of these numbers is:" + (a/b));}
    else
    if (x.equals("+")) {
    System.out.println("\nThe sum of these numbers is:" + (a+b));}
    else
    if (x.equals("-")) {
    System.out.println("\nThe difference of these numbers is:" + (a-b));}}
    else{
    if (y.equals("A") || y.equals("a")){

    if (x.equals("*")) {
    System.out.println("\nThe product of these numbers is:" + (b*a));}
    else
    if (x.equals("/")) {
    System.out.println("\nThe quotient of these numbers is:" + (b/a));}
    else
    if (x.equals("+")) {
    System.out.println("\nThe sum of these numbers is:" + (b+a));}
    else
    if (x.equals("-")) {
    System.out.println("\nThe difference of these numbers is:" + ((b-a)));}}}
           }}