2014-04-26 2 views
0

숫자를 인쇄 할 때와 같은 방식으로 숫자를 함께 추가하는 방법은 무엇입니까?함께 인쇄하는 방법으로 숫자를 함께 추가하는 방법 (System.out.println (numbers)); 숫자 ++;

System.out.println(numbers); 
    numbers++; 

이렇게 인쇄됩니다.

1 
    2 
    3 
    4 

등 전 1 + 2 + 3 + 4

로 함께 추가 얼마나

여기이 질문에 내 현재 코드입니다. 이것은 제가 헬싱키 대학교에서 MOOC를 위해 연습하고있는 운동입니다. 저는 미국에 살기 때문에 8 시간의 차이 때문에 도움을 요청하기가 어렵습니다.

public static void main(String[] args) { 
    Scanner reader = new Scanner(System.in); 

    System.out.print("Until What?:"); 
    // the user inputs a number here 
    int blockExe = 1; 
    // the blockExe variable is supposed to store a count of how many times the 
    // block has been executed which i belive should be limited to the user input 
    int userIn = Integer.parseInt(reader.nextLine()); 
    int sum = userIn + blockExe; 
    // i am supposed to add the number of block executions the user input 
    // each time adding 1 to the execution so 1+2+3 
    // then printing the sum of those numbers 

    while (blockExe <= userIn) { 
     blockExe += 1; 
     if (blockExe + userIn == sum) { 
      break; 
     } 
    } 
    System.out.println("Sum is:" +sum); 

} 

은}

+0

샘플 입력은 무엇이며 어떤 출력을 얻었으며 어떤 출력이 필요합니까? – anirudh

+0

과정에 대한 테스트 러너 두 부정적인 양수를 테스트합니다, 그래서 첫 번째 샘플 입력 6 출력 4 그리고 6이 필요하므로 1 + 2 + 3 = 6으로 계산해야합니다 –

답변

1

이 코드는 모호 :

while (blockExe <= userIn) { 
    blockExe += 1; 
    if (blockExe + userIn == sum) { 
     break; 
    } 
} 

은 아마 당신이 원하는 :

int sum=0; 
for(blockExe = 1;blockExe <= userIn; blockExe ++) { 
    sum+=blockExe; 
} 
+0

나는 이해합니다 이것은 효과가 있지만, 우리는 아직 우리 코스를 커버하지 못했다. 지금까지 (if, else, if-else, while, sout 및 대부분의 수학적 opper, 변수 및 구문 중 일부). –

0

에서 System.out.println ("합계는 :"+ 합계) ;

그 라인은 의미가 없습니다. 나는 당신이 출력하려고하는 숫자가 blockExe가되어야한다고 생각하지만 당신이 설명에서 무엇을하려고하는지 명확하게 알지 못합니다. 해당 줄을 다음과 같이 변경하십시오.

System.out.println ("Sum is :"blockExe);

당신의 답에 더 가까워 지는지 확인하십시오.

관련 문제