2016-07-17 11 views
1

ループを使用して階乗を計算するプログラムの一部を記述しようとしています。エラーメッセージは表示されませんが、出力が得られません。ループで階乗を計算する方法

私のアプローチについての提案はありますか、ループを使用するより良いアプローチがありますか?ありがとう!あなたのコードで

import java.util.Scanner; 

public class Factorial { 
public static void main(String[] args) { 

    System.out.print("Enter a non-negative number that you wish to perform a factorial function on: "); 

    //Create scanner object for reading user input 
    Scanner input = new Scanner(System.in); 

    //Declare variables 
    int number = input.nextInt(); 
    int factTotal = 1; 

    //Execute factorial 
    do{ 
     factTotal = factTotal * number; 
     number--; 
     while (number >= 1); 
    } 
    while (number <= 0);{ 
     System.out.println("That's not a positive integer!"); 
    } 

    System.out.print(factTotal); 
} 

}

+0

*「エラーメッセージは表示されませんが、出力が得られません」* - デバッガを使用してみてください! –

+2

'while(number> = 1);' while(number> = 1){/ * do nothing * /} 'と同じで、永遠に実行されます。 – Andreas

+0

階乗の計算は正の数を掛けることを意味するので、while(number <= 0)は何をすべきでしょうか? 'number'が負である間にループすることは、このコードの中には何もないようです。 – Andreas

答えて

3

これは、コードの一部は、と等価であること
以下のように
また、2番目のwhileループはむしろifの文でなければなりません私はあなたの問題の階乗部分に近づくだろう。 do/whileループを削除するのは、出力が得られない場合に無限ループに陥っているように見えるからです。

//Call this method when you want to calculate the factorial 
public int factorial(int num){ 
    for(int i = num-1; i > 1; i--){ 
     num *= i; 
    } 
    return num; 
} 

これはあなたのコードのようです。

import java.util.Scanner; 

public class Factorial { 
public static void main(String[] args) { 

    System.out.print("Enter a non-negative number that you wish to perform a factorial function on: "); 

    //Create scanner object for reading user input 
    Scanner input = new Scanner(System.in); 

    //Declare variables 
    int number = input.nextInt(); 
    int factTotal = 1; 

    if(number > 0){ 

     factTotal = factorial(number); 

     System.out.print(factTotal); 
    } 
    else 
     System.out.println("This is a negative number"); 
} 
-1

do...whileループで、あなたはこのwhileステートメントを使用しています

while (number >= 1); 

あなたはセミコロンに気づきませんでしたか?そして、このループのポイントは何ですか?現在はnumberの値が決して変更されず、結果として出力が得られないため、無限ループに陥っています。私はあなたがする意図と信じ

while (number >= 1) 
{ 
    //doNothing 
} 

import java.util.Scanner; 

public class Factorial { 
public static void main(String[] args) { 

    System.out.print("Enter a non-negative number that you wish to perform a factorial function on: "); 

    //Create scanner object for reading user input 
    Scanner input = new Scanner(System.in); 

    //Declare variables 
    int number = input.nextInt(); 
    int factTotal = 1; 

    //Execute factorial 
    do{ 
     factTotal = factTotal * number; 
     number--; 
    } 
    while (number <= 1); 
    if(number < 0) 
    { 
     System.out.println("That is not a positive integer"); 
    } 
    else 
    System.out.print(factTotal); 
} 
+0

チャットでこのディスカッションを続行しましょう(http://chat.stackoverflow.com/rooms/117502/discussion-between-skorrloregaming-productions-and-vaibhav-bajaj)。 – Speentie8081

0

@Pernicious、若干の変更と私のコメントが追加されたプログラムをご覧ください。私はこれがあなたがしようとしていたものだと思います。

import java.util.Scanner; 

public class Factorial { 

public static void main(String[] args) { 

    System.out.print("Enter a non-negative number that you wish to perform a  factorial function on: "); 

    //Create scanner object for reading user input 
    Scanner input = new Scanner(System.in); 

    //Declare variables 
    int number = input.nextInt(); 
    int factTotal = 1; 

// The input number check should be before factorial calculation 
    if(number <= 0){ 
     System.out.println("That's not a positive integer!"); 
     System.exit(0); 
    } 

    //Execute factorial 
    do { 
     factTotal = factTotal * number; 
     number--; 
// while (number >= 1); This while should be after do{}, not within do{} 
    } while (number >= 1); 
//  This check should be done immeduately after user input, not after calculation of factorial. 
//  while (number <= 0); 
//  { 
//   System.out.println("That's not a positive integer!"); 
//  } 

    System.out.println(factTotal); 
} 
} 
関連する問題