아하
생활

생활꿀팁

단아한망둥어94
단아한망둥어94

while문을 이용해서 풀라는데 어캐해야되나요?

안녕하세요. 요즘 취미로 코딩을 배우고 있는 대학생입니다.

문제는 밑에 사진 입니다. while 문을 이용해서 이문제를 풀어야 됩니다.

지금 이 문제만 2시간째 하는데도 잘 모르겠어요ㅠㅠㅠ

55글자 더 채워주세요.
2개의 답변이 있어요!
  • 고급스런비쿠냐84
    고급스런비쿠냐84

    안녕하세요

    자바공부중이시군요! 너무 어렵게 생각하시는것 같은데.. while 문을 무한루프로 사용하고 수를 입력받아서 계속 더해주고 no 일때 while 문을 빠져나가면서 더해진 값을 출력하면 되지 않을까요

    int sum = 0; 합계변수선언

    while(1=1) //무한루프

    {

    숫자입력받기

    sum += 입력받은숫자;

    again 입력받기

    if ( again == "no") // again 값이 no 일때

    {

    break; //while 문 빠져나오기

    }

    }

    sum 변수 출력

  • 탈퇴한 사용자
    탈퇴한 사용자

    참고하세요.

    import java.util.Scanner; public class CalculateTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int total = 0; while (true) { System.out.print("Input any number : "); if (scanner.hasNextInt()) { int number = scanner.nextInt(); total += number; } else { System.out.println("Incorrect input: an integer is required"); scanner.next(); continue; } System.out.print("Input again? (yes/no) : "); String line = scanner.next(); if ("yes".equals(line)) { continue; // yes 문자열이 아닌 경우에는 모두 no로 처리 } else { break; } } System.out.println("The total from the input is : " + total); } }