我是编码初学者,所以我想在我想创建的简单程序/游戏中寻求帮助
我正在尝试创建一个简单的争夺游戏,我几乎已经完成了...... 我想循环我的整个程序,但我遇到了问题.. 关于如何循环我的程序有什么建议吗?或用我正在使用的方法进行更正...任何帮助将不胜感激:)
public static void main(String[] args) throws InterruptedException {
String[] word = new String[22];
word[0] = "Package";
word[1] = "Import";
word[2] = "Public";
word[3] = "Private";
word[4] = "Static";
word[5] = "Void";
word[6] = "String";
word[7] = "Integer";
word[8] = "Character";
word[9] = "Boolean";
word[10] = "Public";
word[11] = "High-Level";
word[12] = "Low-Level";
word[13] = "Class";
word[14] = "Statements";
word[15] = "Constructor";
word[16] = "Default";
word[17] = "Method";
word[18] = "Declaration";
word[19] = "Object";
word[20] = "Variable";
word[21] = "Null";
String rword = word[(int) (Math.random() * word.length)];
Shuffle srword = new Shuffle();
srword.shuffle(rword,rword);
}
public void shuffle(String input,String rword) throws InterruptedException{
Scanner scanner = new Scanner(System.in);
List<Character> characters = new ArrayList<>();
for(char c:input.toCharArray()){
boolean scword = characters.add(c);
}
StringBuilder scrmbldword = new StringBuilder(input.length());
while(!characters.isEmpty()){
int randPicker = (int)(Math.random()*characters.size());
scrmbldword.append(characters.remove(randPicker));
}
// Game loads the game xD
System.out.print("\rLoading");
Thread.sleep(500);
System.out.print("\rLoading.");
Thread.sleep(500);
System.out.print("\rLoading..");
Thread.sleep(500);
System.out.print("\rLoading...");
Thread.sleep(500);
System.out.print("\rLoading....");
Thread.sleep(500);
System.out.print("\rLoading.....");
Thread.sleep(500);
System.out.print("\rLoading.....");
Thread.sleep(100);
System.out.print("\rCompleted");
// Game introduces itself to the user.
Thread.sleep(2000);
System.out.println("\rWelcome to Scramble PT!");
Thread.sleep(1000);
System.out.println("This is a game where you guess a word that is scrambled.");
// Game request name input from the user
Thread.sleep(2000);
System.out.println("First, What is your Name?");
String name = scanner.next();
// Game prints name input from the user
Thread.sleep(2000);
System.out.println("Hello " + name + ",Please get ready because the game is Starting!");
// Game ask the user for Y/N input
Thread.sleep(2000);
System.out.print("Are you ready?: ");
String yon = scanner.next();
switch (yon) {
case "Yes":
// Game prints the Scrambled Word
System.out.print("Scrambled Word: ");
System.out.print(scrmbldword.toString());
// Game let user enter it's guess
System.out.print("\nEnter your Answer: ");
String answer;
boolean win;
win = false;
while (win == false){
answer = scanner.next();
if (!answer.equalsIgnoreCase(rword)){
System.out.println("Wrong, please Try Again!");
Thread.sleep(500);
System.out.print("Enter your Answer: ");
continue;
}else{
System.out.println("Correct!");
Thread.sleep(500);
System.out.print(String.format("Thanks for playing %s!",name));
win = true;
Thread.sleep(500);
System.out.println("Would you like to try again?");
System.out.println("Enter Yes or No: ");
String retry = scanner.next();
Thread.sleep(500);
if (retry.equalsIgnoreCase("Yes")){
System.out.println("Alright! reloading the game");
return;
}else
if (retry.equalsIgnoreCase("No")){
System.out.println(String.format("Goodbye! %s !",name));
Thread.sleep(200);
System.out.println("\rGame Shutting down.");
Thread.sleep(200);
System.out.println("\rGame Shutting down..");
Thread.sleep(200);
System.out.println("\rGame Shutting down...");
Thread.sleep(200);
System.out.println("\rGame Shutting down....");
Thread.sleep(200);
System.out.println("\rGame Shutting down.....");
}else{
System.out.println("I can't understand you.... So.... Bye!");
Thread.sleep(200);
System.out.println("\rGame Shutting down.");
Thread.sleep(200);
System.out.println("\rGame Shutting down..");
Thread.sleep(200);
System.out.println("\rGame Shutting down...");
Thread.sleep(200);
System.out.println("\rGame Shutting down....");
Thread.sleep(200);
System.out.println("\rGame Shutting down.....");
}break;
}
}
case "No":
Thread.sleep(500);
System.out.print("Ok, Please come again!");
break;
default:
Thread.sleep(500);
System.out.print("Please answer exactly Yes or No!");
break;
}
}
}
最佳答案
我假设这就是您正在寻找的内容,但在您获取和使用此代码之前,请考虑以下几件事:您的代码编写方式并未最大限度地利用 Java 的面向对象特性。如果这段代码有多个类,那么它通常会更清晰、更好。 Main 类只能用于初始化另一个类或类组。好的,现在来说说代码。这应该可以解决问题:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Shuffle {
public static void main(String[] args) {
startGame();
}
public static void shuffle(String input,String rword) throws InterruptedException{
Scanner scanner = new Scanner(System.in);
List<Character> characters = new ArrayList<>();
for(char c:input.toCharArray()){
boolean scword = characters.add(c);
}
StringBuilder scrmbldword = new StringBuilder(input.length());
while(!characters.isEmpty()){
int randPicker = (int)(Math.random()*characters.size());
scrmbldword.append(characters.remove(randPicker));
}
// Game loads the game xD
System.out.print("\rLoading");
Thread.sleep(500);
System.out.print("\rLoading.");
Thread.sleep(500);
System.out.print("\rLoading..");
Thread.sleep(500);
System.out.print("\rLoading...");
Thread.sleep(500);
System.out.print("\rLoading....");
Thread.sleep(500);
System.out.print("\rLoading.....");
Thread.sleep(500);
System.out.print("\rLoading.....");
Thread.sleep(100);
System.out.print("\rCompleted");
// Game introduces itself to the user.
Thread.sleep(2000);
System.out.println("\rWelcome to Scramble PT!");
Thread.sleep(1000);
System.out.println("This is a game where you guess a word that is scrambled.");
// Game request name input from the user
Thread.sleep(2000);
System.out.println("First, What is your Name?");
String name = scanner.next();
// Game prints name input from the user
Thread.sleep(2000);
System.out.println("Hello " + name + ",Please get ready because the game is Starting!");
// Game ask the user for Y/N input
Thread.sleep(2000);
System.out.print("Are you ready?: ");
String yon = scanner.next();
switch (yon.toLowerCase()) {
case "yes":
// Game prints the Scrambled Word
System.out.print("Scrambled Word: ");
System.out.print(scrmbldword.toString());
// Game let user enter it's guess
System.out.print("\nEnter your Answer: ");
String answer;
boolean win;
win = false;
while (!win){
answer = scanner.next();
if (!answer.equalsIgnoreCase(rword)){
System.out.println("Wrong, please Try Again!");
Thread.sleep(500);
System.out.print("Enter your Answer: ");
}else{
System.out.println("Correct!");
Thread.sleep(500);
System.out.print(String.format("Thanks for playing %s!",name));
win = true;
Thread.sleep(500);
System.out.println(" Would you like to try again?");
System.out.println("Enter Yes or No: ");
String retry = scanner.next();
Thread.sleep(500);
if (retry.equalsIgnoreCase("Yes")){
System.out.println("Alright! reloading the game");
startGame();
return;
}else
if (retry.equalsIgnoreCase("No")){
System.out.println(String.format("Goodbye! %s !",name));
Thread.sleep(200);
System.out.println("\rGame Shutting down.");
Thread.sleep(200);
System.out.println("\rGame Shutting down..");
Thread.sleep(200);
System.out.println("\rGame Shutting down...");
Thread.sleep(200);
System.out.println("\rGame Shutting down....");
Thread.sleep(200);
System.out.println("\rGame Shutting down.....");
}else{
System.out.println("I can't understand you.... So.... Bye!");
Thread.sleep(200);
System.out.println("\rGame Shutting down.");
Thread.sleep(200);
System.out.println("\rGame Shutting down..");
Thread.sleep(200);
System.out.println("\rGame Shutting down...");
Thread.sleep(200);
System.out.println("\rGame Shutting down....");
Thread.sleep(200);
System.out.println("\rGame Shutting down.....");
}break;
}
}
case "no":
Thread.sleep(500);
System.out.print("Ok, Please come again!");
break;
default:
Thread.sleep(500);
System.out.print("Please answer exactly Yes or No!");
break;
}
}
public static void startGame() {
String[] word = new String[22];
word[0] = "Package";
word[1] = "Import";
word[2] = "Public";
word[3] = "Private";
word[4] = "Static";
word[5] = "Void";
word[6] = "String";
word[7] = "Integer";
word[8] = "Character";
word[9] = "Boolean";
word[10] = "Public";
word[11] = "High-Level";
word[12] = "Low-Level";
word[13] = "Class";
word[14] = "Statements";
word[15] = "Constructor";
word[16] = "Default";
word[17] = "Method";
word[18] = "Declaration";
word[19] = "Object";
word[20] = "Variable";
word[21] = "Null";
String rword = word[(int) (Math.random() * word.length)];
try {
shuffle(rword,rword);
} catch (InterruptedException ignored) {}
}
}
关于java - 如何循环整个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51675463/