2011-10-23 26 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

相關問題