2016-07-17 2 views
1

루프를 사용하여 계승을 계산하는 프로그램의 일부를 작성하려고합니다. 오류 메시지가 없지만 출력이 없습니다.Java에서 루프를 사용하여 계승 계산하는 방법

내 접근 방식에 대한 제안 사항이 있습니까? 아니면 루프를 사용하는 것이 더 나은 방법입니까? 감사!

while (number >= 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

은이다 : 나는 당신이해야 할 의미가 생각

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

:
아래와 같이
또한, 두 번째 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 문을 사용했다? 그리고이 루프의 요점은 무엇입니까? 현재는 number 값이 변경되지 않으므로 무한 루프가 발생하므로 출력이 없습니다.

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); 
} 
} 
관련 문제