2016-10-17 97 views
-3

計算器詢問用戶兩個數字,但如果用戶選擇階乘運算符(!),我需要它僅向用戶請求一個數字。有人可以幫我弄這個嗎?Java計算器程序階乘

public static void main(String[] args) throws IOException { 

    Scanner in; 
    in = new Scanner(System.in); 

    char nextChar; 
    char oper; 
    int num1 = 0; 
    int num2 = 0; 
    int base = 2; 
    int exp = 10; 

    System.out.println("Please enter operator"); 
    oper = in.nextLine().charAt(0); 
    System.out.println("The operator is " + oper); 

    System.out.println("Please enter the first number"); 
    num1 = in.nextInt(); 
    System.out.println("The first number is " + num1); 

    if (oper =='!') 
    { 
     System.out.println("Please enter the first number"); 
     num1 = in.nextInt(); 
     System.out.println("The first number is " + num1); 
    } else { 
     System.out.println("Please enter the second number"); 
     num2 = in.nextInt(); 
     System.out.println("The second number is " + num2); 
    } 

    switch (oper) { 

     case '+': 
      System.out.println("The answer is " + (num1 + num2));//addition 
      break; 
     case '-': 
      System.out.println("The answer is " + (num1 - num2));//subtraction 
      break; 
     case '*': 
      System.out.println("The answer is " + (num1 * num2));//multiplication 
      break; 
     case '/': 
      System.out.println("The answer is " + (num1/num2));//division 
      break; 
     case '%': 
      System.out.println("The answer is " + (num1 % num2));//remainder 
      break; 
     case 'p': 
      System.out.println("The answer is " + exponent(num1,num2));//exponent 
      break; 
     case '!': 
      System.out.println("The answer is "); //factorial 
      break; 
     case 'n': 
      System.out.println("The answer is "); //previous answer 
      break; 
    } 
} 
+0

您是否自己嘗試過任何方法,或者您只是要求我們爲您編碼? – nhouser9

+0

是的,上面的代碼是我嘗試過的,而不是隻要求用戶輸入一個數字,它仍然要求兩個 –

+0

在上面的代碼中,你要求輸入,然後如果運算符是!你要求另一個輸入。 – nhouser9

回答

1

您的解決方案肯定會要求第一次輸入兩次。 更改如下您if條件:

 if(oper != '!'){ 
    System.out.println("Please enter the second number"); 
num2 = in.nextInt(); 
System.out.println("The second number is " + num2); 
      } 

沒有別的這裏需要。你的代碼的

+0

該代碼對我無效 –

+0

這不可能,這一定會奏效。你一定犯了一些錯誤。 –

+0

它不是要求第一次輸入兩次,但它仍然要求第一個數字和第二個數字,我只需要第一個數字 –

0

正確的片段:

System.out.println("Please enter operator"); 
oper = in.nextLine().charAt(0); 
System.out.println("The operator is " + oper); 
if (oper =='!'){ 

    System.out.println("Please enter the number"); 
     num1 = in.nextInt(); 
    System.out.println("The first number is " + num1); 
} 
else { 
    System.out.println("Please enter the first number"); 
    num1 = in.nextInt(); 
    System.out.println("The first number is " + num1); 
    System.out.println("Please enter the second number"); 
    num2 = in.nextInt(); 
    System.out.println("The second number is " + num2); 
} 
0

此代碼是一個問題:

System.out.println("Please enter the first number");  //you ask for 1st num 
num1 = in.nextInt(); 
System.out.println("The first number is " + num1); 

if (oper == '!') 
{ 
    System.out.println("Please enter the first number"); //you ask for 1st num again 
    num1 = in.nextInt(); 
    System.out.println("The first number is " + num1); 
} else { 
    System.out.println("Please enter the second number"); 
    num2 = in.nextInt(); 
    System.out.println("The second number is " + num2); 
} 

只要看看它做什麼。它說「請輸入第一個數字」,如果操作員是「!」,則說「請輸入第一個數字」。這顯然是不正確的。如果操作員是「!」,則不應再向用戶詢問第一個號碼。更改爲:

System.out.println("Please enter the first number"); 
num1 = in.nextInt(); 
System.out.println("The first number is " + num1); 

if (oper != '!') { 
    System.out.println("Please enter the second number"); 
    num2 = in.nextInt(); 
    System.out.println("The second number is " + num2); 
} 

當運算符爲「!」時,將刪除第一個數字的第二個請求。

+0

是的,謝謝你這刪除了第一個號碼的第二個請求' –

+0

@KatieKeenan不客氣。請記住,upvote和接受,因爲這解決了問題=] – nhouser9

+0

它不是要求第一次輸入兩次了,但它仍然要求第一個號碼和第二個號碼,我只需要第一個號碼 –

0

這是你的程序的修正版本,應該工作:

package com.example; 

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

class Main { 

    public static void main(String[] args) throws IOException { 

     Scanner in; 
     in = new Scanner(System.in); 

     char oper; 
     int num1 = 0; 
     int num2 = 0; 

     System.out.println("Please enter operator"); 
     oper = in.nextLine().charAt(0); 
     System.out.println("The operator is " + oper); 

     System.out.println("Please enter the first number"); 
     num1 = in.nextInt(); 
     System.out.println("The first number is " + num1); 

     if (oper !='!') { 
      System.out.println("Please enter the second number"); 
      num2 = in.nextInt(); 
      System.out.println("The second number is " + num2); 
     } 

     switch (oper) { 

      case '+': 
       System.out.println("The answer is " + (num1 + num2));//addition 
       break; 
      case '-': 
       System.out.println("The answer is " + (num1 - num2));//subtraction 
       break; 
      case '*': 
       System.out.println("The answer is " + (num1 * num2));//multiplication 
       break; 
      case '/': 
       System.out.println("The answer is " + (num1/num2));//division 
       break; 
      case '%': 
       System.out.println("The answer is " + (num1 % num2));//remainder 
       break; 
      case 'p': 
       System.out.println("The answer is " + Math.pow(num1,num2));//exponent 
       break; 
      case '!': 
       System.out.println("The answer is " + factorial(num1)); //factorial 
       break; 
      case 'n': 
       System.out.println("The answer is "); //previous answer 
       break; 
     } 
    } 

    // from http://stackoverflow.com/a/7879559/1549950 
    private static int factorial(int n) { 
     int fact = 1; 
     for (int i = 1; i <= n; i++) { 
      fact *= i; 
     } 
     return fact; 
    } 

} 

我還增加了階乘函數的實現,因爲它好像有在Math對此沒有現成的使用功能類。

由於您的問題似乎來源於某種作業,我強烈建議您開始使用其他「解決方案技巧」,而不是要求此論壇中的人員完成您的工作。這可能會幫助你一段時間,但不會讓你通過考試......有很多(免費)在線書籍來學習Java - 花時間學習Java - 從長遠來看,其他一切都是浪費時間!