首先,我想说我对这一切真的很陌生......我已经尽可能多地学习了,所以如果我的任何代码看起来很荒谬或到处都是很抱歉,但我需要从某个地方开始。(顺便说一句,这段代码的基础归功于 CrossCoastGaming:http ://tinyurl.com/kktyq4e )。
现在谈谈手头的事情。通过添加几个不同的短语、利用变量和添加一个尝试计数器,我已经改进了视频中那个人显示的编码(因为没有更好的词)。这是我的代码:
import java.util.Random;
import java.util.Scanner;
public class Main {
public static int number, guess, tryCount, replay;
public static int maxValue =1;
public static Scanner scan;
public static Random rand;
public static void main(String args[]) {
scan = new Scanner(System.in);
rand = new Random();
System.out.print("Enter a maximum number: ");
while(maxValue < 2)
maxValue = scan.nextInt();
number = rand.nextInt(maxValue);
System.out.print("Guess a number from 1 to " + maxValue + ": ");
while (guess != number) {
guess = scan.nextInt();
tryCount++;
if (guess < 1) {
System.out.print("Guess is not positive. Try again: ");
}else if (guess < number) {
System.out.print("Too low! Try again: ");
}
if (guess > maxValue) {
System.out.println("Guess is higher than " + maxValue + ". Try again: ");
}else if (guess > number) {
System.out.print("Too high! Try again: ");
}
}
if (tryCount == 1) {
System.out.println("Nailed it! It only took you 1 try!");
}
else {
System.out.println("Nailed it! It took you " + tryCount + " tries.");
}
System.out.println("Type 0 to play again. Type 1 to quit.");
if (replay == 1) {
replay = scan.nextInt();
}
}
}
好的,希望这能让任何知道他们在做什么的人对我的目标有所了解。现在,正如您在这一行中看到的那样:
if (replay == 1) {
replay = scan.nextInt();
}
我想写一种方法,这样人们就可以重玩游戏而无需重新启动文件。我已经知道我想做什么,但是我到处搜索,似乎无法找到在此之后继续做什么。我确定我错过了一些东西。任何帮助将不胜感激。