迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

重新启动 Java 程序

作者:迹忆客 最近更新:2023/07/16 浏览次数:

在菜单驱动的程序或游戏中,我们需要一个选项来重新启动或重置我们的程序。 我们可以通过递归调用函数或使用条件循环语句来重新启动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: ...

上面的代码显示了递归函数的功能,用于再次重新启动自身。 由于递归调用周期中没有停止条件,因此程序将无限次重新启动。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便