Java 调用main的替换
我有这样一个问题,如果用户说是,我会尝试重新开始游戏,我总是通过调用main()来完成,但我的老师坚持认为有更好的方法。如何在不调用main的情况下启动程序。我假设您必须使用循环,但我不确定如何使用它来解决这个问题Java 调用main的替换,java,loops,Java,Loops,我有这样一个问题,如果用户说是,我会尝试重新开始游戏,我总是通过调用main()来完成,但我的老师坚持认为有更好的方法。如何在不调用main的情况下启动程序。我假设您必须使用循环,但我不确定如何使用它来解决这个问题 //import library import java.io.*; import java.util.*; //file name public class GuessingGame { //Main met
//import library
import java.io.*;
import java.util.*;
//file name
public class GuessingGame
{
//Main method, throws input and output error.
public static void main (String [] args) throws IOException
{
//inputs for random number and switch statements
Scanner inRn = new Scanner (System.in);
Scanner inSw = new Scanner (System.in);
//variables for the loop, random number, character, counter and input
int guess=0;
int rnd;
int num;
char decision;
//random number generator
Random random = new Random();
rnd = random.nextInt(100) + 1;
//loops the user input for the guess
while (true){
try{
//prompt the user
System.out.println(" Please guess a number between 1-100. Press 0 to give up.");
num = inRn.nextInt();
}
//catches input errors
catch (Exception e){
System.out.println("You can only enter numbers!");
inRn.next();
continue;
}
//if statements
if (num==0)
{
//when user types '0' it ends the program
System.out.println("You gave up after " + guess + " try(s) .... Closing program ...");
System.exit(0);
}
else if (num>rnd)
{
System.out.println("The number is too big!");
guess++;
}
else if (num<rnd)
{
//prints 'too small', adds to counter 'guess'
System.out.println("The number is too small!");
guess++;
}
else
{
//prints correct, adds to counter, dsiplays # of guesses and ends loop
System.out.println("You guessed the right number!!: " + rnd);
guess++;
System.out.print(" # of guesses: " + guess + " -");
//loops the case untill correct input is chosen either 'Y' or 'N'
while(true){
//prompt the user if they want to play again
System.out.println(" Would you like to play again? <Y/N>");
decision = inSw.nextLine().charAt(0);
//switch statements
switch (decision) {
case 'Y':
case 'y':
//calls main, basically restarts the game
GuessingGame.main(args);
break;
case 'N':
case 'n':
System.out.println("Bye!");
//exits the program completely
System.exit(0);
break;
default:
//if incorrect input, this prints
System.out.println("Please enter a Yes or No <Y/N>");
}
}
}
}
}
}
//导入库
导入java.io.*;
导入java.util.*;
//文件名
公开课猜谜游戏
{
//主方法,抛出输入和输出错误。
公共静态void main(字符串[]args)引发IOException
{
//随机数和开关语句的输入
扫描仪inRn=新扫描仪(System.in);
扫描仪inSw=新扫描仪(系统in);
//循环变量、随机数、字符、计数器和输入
int guess=0;
int rnd;
int-num;
字符决策;
//随机数发生器
随机=新随机();
rnd=random.nextInt(100)+1;
//循环用户输入以进行猜测
while(true){
试一试{
//提示用户
System.out.println(“请猜一个介于1-100之间的数字,按0放弃”);
num=inRn.nextInt();
}
//捕获输入错误
捕获(例外e){
System.out.println(“您只能输入数字!”);
inRn.next();
继续;
}
//如果语句
如果(num==0)
{
//当用户键入“0”时,它将结束程序
System.out.println(“您在“+guess+”尝试后放弃了…”关闭程序…”);
系统出口(0);
}
否则如果(num>rnd)
{
System.out.println(“数字太大了!”);
猜测++;
}
else if(num这里我将游戏逻辑移到了一个新的play()
方法。此方法返回一个布尔值
,以指示用户是否希望在游戏结束时继续玩。我在main
方法中使用了一个do…while
循环来启动游戏至少一次,然后检查play()
方法的返回值以决定是否继续玩
这样,程序将优雅地结束,而不必使用System.exit
方法强制退出。它也不会使用while(true)
循环中调用的递归方法,而该循环最终会导致堆栈溢出错误
异常
public static void main(String[] args) throws IOException {
boolean keepPlaying = false;
do {
keepPlaying = play();
} while (keepPlaying);
}
private static boolean play() {
//inputs for random number and switch statements
Scanner inRn = new Scanner(System.in);
Scanner inSw = new Scanner(System.in);
//variables for the loop, random number, character, counter and input
int guess = 0;
int rnd;
int num;
char decision;
//random number generator
Random random = new Random();
rnd = random.nextInt(100) + 1;
//loops the user input for the guess
while (true) {
try {
//prompt the user
System.out.println(" Please guess a number between 1-100. Press 0 to give up.");
num = inRn.nextInt();
} //catches input errors
catch (Exception e) {
System.out.println("You can only enter numbers!");
inRn.next();
continue;
}
//if statements
if (num == 0) {
//when user types '0' it ends the program
System.out.println("You gave up after " + guess + " try(s) .... Closing program ...");
return false;
} else if (num > rnd) {
System.out.println("The number is too big!");
guess++;
} else if (num < rnd) {
//prints 'too small', adds to counter 'guess'
System.out.println("The number is too small!");
guess++;
} else {
//prints correct, adds to counter, dsiplays # of guesses and ends loop
System.out.println("You guessed the right number!!: " + rnd);
guess++;
System.out.print(" # of guesses: " + guess + " -");
//loops the case untill correct input is chosen either 'Y' or 'N'
while (true) {
//prompt the user if they want to play again
System.out.println(" Would you like to play again? <Y/N>");
decision = inSw.nextLine().charAt(0);
//switch statements
switch (decision) {
case 'Y':
case 'y':
// User wants to keep playing
return true;
case 'N':
case 'n':
System.out.println("Bye!");
//exits the program completely
return false;
default:
//if incorrect input, this prints
System.out.println("Please enter a Yes or No <Y/N>");
}
}
}
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
布尔keepPlaying=false;
做{
保持播放=播放();
}同时(继续播放);
}
私有静态布尔播放(){
//随机数和开关语句的输入
扫描仪inRn=新扫描仪(System.in);
扫描仪inSw=新扫描仪(系统in);
//循环变量、随机数、字符、计数器和输入
int guess=0;
int rnd;
int-num;
字符决策;
//随机数发生器
随机=新随机();
rnd=random.nextInt(100)+1;
//循环用户输入以进行猜测
while(true){
试一试{
//提示用户
System.out.println(“请猜一个介于1-100之间的数字,按0放弃”);
num=inRn.nextInt();
}//捕获输入错误
捕获(例外e){
System.out.println(“您只能输入数字!”);
inRn.next();
继续;
}
//如果语句
如果(num==0){
//当用户键入“0”时,它将结束程序
System.out.println(“您在“+guess+”尝试后放弃了…”关闭程序…”);
返回false;
}否则如果(num>rnd){
System.out.println(“数字太大了!”);
猜测++;
}否则如果(num
你的老师说得对,你不必再调用main,只要改进你的算法,你就会得到一个更好的代码。也许像这样:
//import library
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
//file name
public class GuessingGame {
// Main method, throws input and output error.
public static void main(String[] args) throws IOException {
// inputs for random number and switch statements
Scanner inRn = new Scanner(System.in);
Scanner inSw = new Scanner(System.in);
// variables for the loop, random number, character, counter and input
int guess = 0;
int rnd;
int num = -1;
char decision = 'Y';
// random number generator
Random random = new Random();
rnd = random.nextInt(100) + 1;
// loops the user input for the guess
do {
if (num != rnd) {
try {
// prompt the user
System.out
.println(" Please guess a number between 1-100. Press 0 to give up.");
num = inRn.nextInt();
}
// catches input errors
catch (Exception e) {
System.out.println("You can only enter numbers!");
inRn.next();
continue;
}
// if statements
if (num == 0) {
// when user types '0' it ends the program
System.out.println("You gave up after " + guess
+ " try(s) .... Closing program ...");
System.exit(0);
} else if (num > rnd) {
System.out.println("The number is too big!");
guess++;
} else if (num < rnd) {
// prints 'too small', adds to counter 'guess'
System.out.println("The number is too small!");
guess++;
} else {
// prints correct, adds to counter, dsiplays # of guesses and
// ends loop
System.out.println("You guessed the right number!!: " + rnd);
guess++;
System.out.print(" # of guesses: " + guess + " -");
}
} else {
// prompt the user if they want to play again
System.out.println(" Would you like to play again? <Y/N>");
decision = inSw.nextLine().charAt(0);
// switch statements
switch (decision) {
case 'N':
case 'n':
System.out.println("Bye!");
// exits the program completely
System.exit(0);
break;
case 'Y':
case 'y':
rnd = random.nextInt(100) + 1;
break;
default:
// if incorrect input, this prints
System.out.println("Please enter a Yes or No <Y/N>");
}
}
} while (decision != 'N' || decision != 'n');
inRn.close();
inSw.close();
}
}
//导入库
导入java.io.IOException;
导入java.util.Random;
导入java.util.Scanner;
//文件名
公开课猜谜游戏{
//主方法,抛出输入和输出错误。
公共静态void main(字符串[]args)引发IOException{
//随机数和开关语句的输入
扫描仪inRn=新扫描仪(System.in);
扫描仪inSw=新扫描仪(系统in);
//循环变量、随机数、字符、计数器和输入
int guess=0;
public static void main(String args[])
{
main(args);
}
Scanner in = new Scanner(System.in);
char decision;
do{
//game code ...
do{
System.out.print("Do you wish to continue? (y,n): ");
decision = in.nextLine().charAt(0);
}while(!(decision == 'y' || decision == 'n'));
}while(decision == 'y');