2011-10-23 25 views
0

可能重复:
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?为什么我的Java程序的最后输出行不显示?

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))); 
      } 
     } 
    } 
} 

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

+0

请更新您的最后一个问题。 – zengr

+0

您需要:'y.equals(“b”)|| y.equals(“B”)代替'y.equals(“b”+“B”)'。 '“b”+“B”'结果在'“bB”' – zengr

+0

提示:如果你正在学习java。不要经常发布你的问题。花一些时间调试和理解正在发生的事情。其他方面,你永远不会明白*它。 – zengr

回答

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)));}}} 
      }} 
+0

好的答案伙计。 +1小心你的缩进。另外,考虑只显示固定线代码而不是所有线。 – Gray

相关问题