HOWTO · Java

Java에서 프로그램 다시 시작

Java에서는 메소드를 재귀적으로 호출하여 프로그램을 다시 시작할 수 있습니다.

이 페이지의 내용

메뉴 기반 프로그램이나 게임에서는 프로그램을 다시 시작하거나 재설정하는 옵션이 필요합니다. 함수를 재귀적으로 호출하거나 조건부 루프 문을 사용하여 Java에서 프로그램을 다시 시작할 수 있습니다.

do-while 조건문 사용

import java.util.*;
import java.util.Scanner;

class Main {
  public static int addNumbers(int a, int b) {
    return a + b;
  }

  public static void main(String[] args) {
    boolean flag = false;

    Scanner sc = new Scanner(System.in);
    do {
      System.out.print("Enter the first number: ");
      int num1 = sc.nextInt();
      System.out.print("Enter the second number: ");
      int num2 = sc.nextInt();
      int result = addNumbers(num1, num2);
      System.out.println("The sum is: " + result);
      System.out.print("Do you want to add more numbers? Enter y for yes and n for no. ");
      char input = sc.next().charAt(0);
      if (input == 'y') {
        flag = true;
      } else {
        flag = false;
      }
    } while (flag);
  }
}

출력:

Enter the first number: 8
Enter the second number: 7
The sum is: 15
Do you want to add more numbers? Enter y for yes and n for no. y
Enter the first number: 9
Enter the second number: 4
The sum is: 13
Do you want to add more numbers? Enter y for yes and n for no. n

위의 코드는 함수를 다시 시작하기 위한 조건부 루프의 기능을 표시합니다. 사용자가 y를 입력하는 한 프로그램이 다시 시작됩니다.

중지 조건 없이 do-while 조건문 사용

import java.util.*;
import java.util.Scanner;

class Main {
  public static int addNumbers(int a, int b) {
    return a + b;
  }

  public static void main(String[] args) {
    boolean flag = true;

    Scanner sc = new Scanner(System.in);
    do {
      System.out.print("Enter the first number: ");
      int num1 = sc.nextInt();
      System.out.print("Enter the second number: ");
      int num2 = sc.nextInt();
      int result = addNumbers(num1, num2);
      System.out.println("The sum is: " + result);
    } while (flag);
  }
}

출력:

Enter the first number: 8
Enter the second number: 7
The sum is: 15
Enter the first number: 9
Enter the second number: 4
The sum is: 13
Enter the first number: .....

위의 코드는 함수를 무한대로 다시 시작하기 위한 조건부 루프의 기능을 표시합니다. 프로그램이 다시 시작되지 않도록 하는 유일한 방법은 프로그램을 닫는 것입니다.

재귀를 사용하여 프로그램 다시 시작

import java.util.*;
import java.util.Scanner;

class Main {
  public static void addNumbers(int a, int b, int c) {
    Scanner sc = new Scanner(System.in);
    if (c == 0) {
      int result = a + b;
      System.out.println("The sum is: " + result);
      return;
    }
    int result = a + b;
    System.out.println("The sum is: " + result);
    System.out.print("Enter the first number: ");
    int num1 = sc.nextInt();
    System.out.print("Enter the second number: ");
    int num2 = sc.nextInt();
    c--;
    addNumbers(num1, num2, c);
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the first number: ");
    int num1 = sc.nextInt();
    System.out.print("Enter the second number: ");
    int num2 = sc.nextInt();
    System.out.print("How many times do you want to restart this program? : ");
    int num3 = sc.nextInt();
    addNumbers(num1, num2, num3);
  }
}

출력:

Enter the first number: 5
Enter the second number: 6
How many times do you want to restart this program? : 2
The sum is: 11
Enter the first number: 4
Enter the second number: 5
The sum is: 9
Enter the first number: 7
Enter the second number: 8
The sum is: 15

위의 코드는 다시 시작하기 위한 재귀 함수의 기능을 표시합니다. 프로그램은 사용자가 요구하는 횟수만큼 다시 시작됩니다.

재귀를 사용하여 중지 조건 없이 프로그램 다시 시작

import java.util.*;
import java.util.Scanner;

class Main {
  public static void addNumbers() {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the first number: ");
    int num1 = sc.nextInt();
    System.out.print("Enter the second number: ");
    int num2 = sc.nextInt();
    int result = num1 + num2;
    System.out.println("The sum is: " + result);
    addNumbers();
  }

  public static void main(String[] args) {
    addNumbers();
  }
}

출력:

Enter the first number: 5
Enter the second number: 3
The sum is: 8
Enter the first number: 8
Enter the second number: 11
The sum is: 19
Enter the first number: ...

위의 코드는 다시 시작하기 위한 재귀 함수의 기능을 표시합니다. 재귀 호출 주기에는 중지 조건이 없으므로 프로그램이 무한정 다시 시작됩니다.