有没有办法在遵循这些规范的同时优化代码?教授无法获得直接帮助。包括规格的屏幕截图。
这是我交上来的代码,按照说明:
该程序必须使用以下方式完成 仅 4 个整数变量用于存储用户定义的种子值、每个用户定义的猜测(仅需要一个变量来存储所有用户定义的猜测)、每次随机生成的硬币翻转(仅需要一个变量)需要存储所有抛硬币的次数)以及正确猜测的总数。 仅将 1 个扫描仪对象实例分配给用于读取种子和用户猜测的引用变量, 仅 1 个随机对象实例化,该实例化使用用户定义的输入作为种子,并分配给用于为抛硬币生成 0 或 1 的引用变量,以及 几个 if/else 语句,以及 几个打印语句。 代码应该高效地编写 消除冗余编码和 消除仅使用一次的变量。
import java.util.Scanner;
import java.util.Random;
public class CoinToss {
public static void main(String[] args) {
Toss();
}
//In this method, the computer simulates flipping a coin and the user guesses
//the result; the computer will report the status of each guess after each
//coin toss and the tabulate the cumulative results after 5 coin tosses.
//Note: The computer will use 0 to represent "heads" and 1 to represent
// "tails" for each coin toss.
public static void Toss() {
//Initiate new scanner.
Scanner scanner = new Scanner(System.in);
//User introduction to the program.
System.out.println("This program will repeatedly prompt the user for a guess to a coin toss.");
System.out.println("Note: 0 is heads and 1 is tails.\n");
//Prompting user for seed, then storing it in an int variable.
System.out.println("Enter the seed for the Random object.");
int seed = scanner.nextInt();
Random random = new Random(seed);
//Initializing 3 variables, they will be used later.
int userGuess, coinFlip, correctGuesses = 0;
/*The program will ask the user for a guess, either 1 or 2, and will store
the guess within the userGuess variable. It will then flip a coin, then
using an if/else statement, if the user guesses right, the number of
correct guesses goes up, and is stored in a variable. This process is
repeated 5 times.
*/
// Trial 1
System.out.println("\nWhat is your guess for trial #1?");
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
// Trial 2
System.out.println("\nWhat is your guess for trial #2?");
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
// Trial 3
System.out.println("\nWhat is your guess for trial #3?");
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
// Trial 4
System.out.println("\nWhat is your guess for trial #4?");
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
// Trial 5
System.out.println("\nWhat is your guess for trial #5?");
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
//Trials are over, final result is printed.
System.out.println("\nYou guessed correctly " + correctGuesses +
" out of 5 times (" + ((int)(correctGuesses / 5.0 * 100)) + "%).");
//Closing the scanner.
scanner.close();
}
}
首先,您只使用变量种子一次,因此您可以内联它。
int seed = scanner.nextInt();
Random random = new Random(seed);
// Replace with
Random random = new Random(scanner.nextInt());
接下来,您将获得以下区块五次:
System.out.println("\nWhat is your guess for trial #1?");
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
您可以将其替换为循环或方法。 或者,我最喜欢的,两者都使用! 所以类似:
public void simulateToss(int tossNr) {
System.out.println("\nWhat is your guess for trial #%d?".formatted(tossNr));
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
}
我也会将(至少在这种情况下)你的prov给出的变量放入属性中。 这会让你的代码看起来更好。
所以像这样:
for(int i = 1; i <= 5; i++) {
simulateTrial(i);
}
// simulateTrial
private void simulateTrial(int trialNr) {
System.out.printf("\nWhat is your guess for trial #%s?%n", trialNr);
userGuess = scanner.nextInt();
coinFlip = random.nextInt(2);
System.out.println("The coin toss was a " + coinFlip + ".");
if (userGuess == coinFlip) {
System.out.println("You guessed right!");
correctGuesses++;
} else {
System.out.println("Sorry, try again next time!");
}
}
编辑: 您还应该考虑使用 printf 和 println,而不是手动将
\n
添加到输出中并以这种方式将字符串连接在一起