在程序终止后,返回到主代码的开头或其他地方。

huangapple go评论77阅读模式
英文:

After program termination, return to the start of the main code or somewhere else

问题

我对编程完全是崭新的,目前正在学习游戏编程,游戏编程中主要涉及的一个要素是循环,更具体地说,是在完成特定目标后,回到游戏的另一部分,就好像重新开始一样。

问题是,我应该使用什么类型的循环来实现这个功能?看看这个基本的代码,你就能理解我在说什么了。

import java.util.Scanner;
Scanner scan = new Scanner();
String x;
System.out.println("Hello, how's it going?");
x = scan.nextLine();
System.out.println("Nice");

我想在输入到扫描器变量后,它会回到"Hello, how's it going",然后重复这个过程,直到我不想再这样做为止,这样我就能更详细地了解该做什么。

因为我正在尝试制作的测试游戏只是一个简单代码的基本循环,只是为了开始我的编程之旅。

英文:

I am brand new to programming in general, I'm currently studying programming in games, which one of the main things that have in games are loops, more specifically about those who after completing a certain objective, it returns to another part of the game, like if it was restarting all over again.
The question is, what type of looping should I use to do it? Take a look at this basic code so you can understand what I'm talking about.

import java.util.Scanner;
Scanner scan = new Scanner();
String x;
System.out.println("Hello, how's it going?");
x = scan.nextLine();
System.out.println("Nice");

I want to do so after writing on the scanner variable, it returns to the "Hello, how's it going", and repeats this process until I don't want to do it anymore, so I will know what to do at a more detailed situation.
Because the testing game I'm trying to make is just a basic loop of a simple code, just to start my programming journey.

答案1

得分: 0

你可以使用 do-while 循环。

以下是一个示例循环,只有在用户输入字符串 "exit" 时才会退出:

do{
    System.out.println("你好,进展如何?");
    x = scan.nextLine();
} while(!x.equals("exit"));
英文:

You can use a do-while loop.

Here is an example of a loop that will only exit once the user inputs the string "exit":

do{
    System.out.println("Hello, how's it going?");
    x = scan.nextLine();
} while(!x.equals("exit"));

huangapple
  • 本文由 发表于 2020年7月22日 10:24:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63025846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定