- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做 Walter Savitch 所著的《Java 简介》一书第 4 章中的练习 3。指示:为猜测 secret 五位数代码的简单游戏开发一种算法。当用户输入对代码的猜测时,程序会返回两个值:猜测中正确位置的数字位数以及这些数字的总和。例如,如果密码为 53840,而用户猜测为 83241,则数字 3 和 4 位于正确的位置。因此,程序应该响应 2 和 7。允许用户猜测固定次数
当前问题:
我花了两个小时。本章还有另外 25 个练习等着我,所以我希望其他练习能更有效率。
public static void main(String[] args) {
int SecretCode = 12140;
int Win = 0;
//just checking the incrementor
int sum = 0;
System.out.println("Ceci est un jeu de lotterie. \nPouvez-vous deviner le nombre à 5 chiffres caché ?\n");
Scanner fromPlayer = new Scanner(System.in);
do {
System.out.println("Votre nombre porte-bonheur: ");
int guess = fromPlayer.nextInt();
//Interprets Guess int to string
String stringGuess = String.valueOf(guess);
int length = stringGuess.length();
boolean CorrectLength = (length == 5);
if (CorrectLength) {
String Firstdigit = stringGuess.substring(0, 1);
String Seconddigit = stringGuess.substring(1, 2);
String Thirddigit = stringGuess.substring(2, 3);
String Fourthdigit = stringGuess.substring(3, 4);
String Fifthdigit = stringGuess.substring(4);
//Interprets Secret Code int to string
String stringSecretCode = String.valueOf(SecretCode);
String FirstdigitCode = stringSecretCode.substring(0, 1);
String SeconddigitCode = stringSecretCode.substring(1, 2);
String ThirddigitCode = stringSecretCode.substring(2, 3);
String FourthdigitCode = stringSecretCode.substring(3, 4);
String FifthdigitCode = stringSecretCode.substring(4);
//Just checking the values that the program will compare to secret code
System.out.println("Vous avez entré \n" + Firstdigit + "\n" + Seconddigit + "\n" + Thirddigit + "\n" + Fourthdigit + "\n" + Fifthdigit);
if (Firstdigit.equals(FirstdigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Firstdigit);
System.out.println("Premier numéro est : correct. Score: " + Win);
} else {
System.out.println("Premier numéro est : incorrect. Score:" + Win);
}
if (Seconddigit.equals(SeconddigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Seconddigit);
System.out.println("Deuxième numéro est : correct. Score: " + Win);
} else {
System.out.println("Deuxième numéro est : incorrect. Score: " + Win);
}
if (Thirddigit.equals(ThirddigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Thirddigit);
System.out.println("Troisième numéro est : correct. Score: " + Win);
} else {
System.out.println("Troisième numéro est : incorrect. Score: " + Win);
}
if (Fourthdigit.equals(FourthdigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Fourthdigit);
System.out.println("Quatrième numéro est : correct. Score: " + Win);
} else {
System.out.println("Quatrième numéro est : incorrect. Score: " + Win);
}
if (Fifthdigit.equals(FifthdigitCode)) {
Win = Win + 1;
sum = sum + Integer.parseInt(Fifthdigit);
System.out.println("Cinquième numéro est : correct. Score: " + Win);
} else {
System.out.println("Cinquième numéro est : incorrect. Score: " + Win);
}
System.out.println("Vous avez deviné " + Win + " numéros. Leur total vaut " + sum);
} else {
System.out.println("ERREUR. Il faut entrer 5 chiffres.");
}
我预计 02140 的输出是“总理号码:不正确。得分:0Deuxième numéro est:正确。得分:1Troisième numéro est:正确。得分:2Quatrième numéro est:正确。得分:3Cinquième numéro est:正确。得分:4您有 4 个数字。 Leur 总高度 7"
但实际输出是:ERREUR。 Il faut entrer 5 chiffres。就好像程序没有将 0 识别为数字。
最佳答案
而不是使用
String stringGuess = String.valueOf(guess);
你应该使用
String stringGuess = String.format("%05d", guess);
始终将您读取的数字转换为五位数长的字符串
。在当前的解决方案中,您应该会看到,如果打印 stringGuess
前导零将从输入中删除。
相反,您可以使用以下代码,该代码使用 Scanner
类的 nextLine()
方法来解决您的问题:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[] expected = "53849".toCharArray();
// enter CTRL-D to stop
while (scanner.hasNext()) {
String input = scanner.nextLine();
if (input.length() != 5) {
System.err.println("wrong length");
continue;
}
char[] actual = input.toCharArray();
int digitSum = 0;
int correctDigits = 0;
for (int i = 0; i < expected.length; i++) {
if (actual[i] == expected[i]) {
correctDigits++;
digitSum += actual[i] - '0';
}
}
String msg = String.format(
"Number of correct digits: %d, their sum: %d",
correctDigits, digitSum
);
System.out.println(msg);
}
关于java - "Why does my lottery game ignores zeros?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58477216/
根据我的理解,INSERT IGNORE 插入一个新条目,如果它不存在,如果存在,则忽略它。所以我已经尝试这样做了一段时间,但似乎没有用。这是我的尝试: insert insert ignore in
出于某种奇怪的原因,StyleCop 不尊重我的文档规则设置。考虑以下代码: internal class SomeClass { public SomeClass() { }
我有一个带有字符串参数和 bool 返回值的方法。我想确保它总是返回 true 我试过了 myClass.Foo(A.Ignored) .WithReturnType() .Returns(tru
这个问题在这里已经有了答案: Filter invoke twice when register as Spring bean (2 个答案) 关闭 1 年前。 我们正面临 SpringSecuri
将 iconv 与 //TRANSLIT//IGNORE 一起使用从 utf8 转换为 ascii 工作正常;它根据当前语言环境(在我的情况下为 de_DE)将不可转换的字符替换为正确的音译: > e
我有这样的查询: $query="INSERT IGNORE INTO mytable (key, word1, word2) VALUES (1, 'abc', 'def')"; 如果因为主键已经存
我有一个 MySQL 表,如下所示: `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `company_id` int(8) unsigned NOT N
git blame --ignore-revs-file显然是现代 Git 中存在的一个选项。 只有一个问题。它不起作用。 或者至少,它对我不起作用: 您可以将其添加到 shell 脚本中: mkdi
使用docker stack deploy,我可以看到如下信息: Ignoring unsupported options: restart 是否意味着重启政策没有到位? 是否必须在撰写文件之外指定它
我正在尝试检查被 git 忽略的文件,我发现 2 个命令显示相同的结果。 哪个是 git check-ignore * git ls-files --others --ignored --exclud
我想为我的 bash 设置和插件等创建一个 git 仓库。我忽略了所有内容(第 0 行),然后在 repo 中手动添加了我想要的文件/文件夹。 (我必须这样做,因为 repo 在我的 ~ 文件夹中。)
我们有一个集中式开发服务器,每个人都在本地结账处工作。我们如何仅忽略本地存储库中的特定目录,而不忽略集中存储库中的特定目录? 一些背景资料: 该项目是一个Drupal站点,该站点的文件目录一直在变化。
当我向 svn:ignore 条目添加一些东西时,它是存储在存储库中还是存储在我的本地副本中? (在 Tortoise 中,为什么添加到 svn:ignore 的项目会出现在我的更改列表中?) 最佳答
我想确保当我从 svn 结帐时,某些 ide 项目元数据文件没有更新。不幸的是,在创建项目时它已被 checkin 项目,但我有我自己的这些文件版本。 svn:ignore 是否也会在更新期间忽略文件
我有一个目录,其中包含具有各种扩展名的文件。我想在 svn:ignore 中执行与以下 .gitignore 代码等效的操作: *.* # ignore everything !.htaccess #
我在实时服务器上有一个大型项目,我想将其提交给 svn 存储库。我已使用 svn add 将所有文件和目录添加到存储库中. 问题是我想提交一个文件夹结构,但没有它的内容。文件夹名称是: /home/m
当我使用 svn:ignore 从 SVN 中排除文件时然后另一个人尝试提交排除的文件, svn 阻止他?或者他必须在他的电脑上执行相同的命令? 最佳答案 想象一下,如果您有一堆 Java 文件( *
如果已经有一个带有_id的文档,是否可以不重新索引文档?也许像MySQL中的INSERT IGNORE查询一样? 我使用批量API,并且重新索引需要很长时间,因此我只想索引丢失的文档。 可能吗? 最佳
我的房间有问题。 我正在使用带有 Gson 转换器的改造作为其余 api,我想与房间分享 pojos。一般来说它可以工作,但在某些情况下我需要忽略一些字段,因为我有对象列表。我尝试使用 @Ignore
我正在使用 vba 代码从数组中查找值。有些结果是“#N/A”,并且单元格的左上角会出现一个绿色三角形。我想自动删除绿色三角形(忽略错误),但“#N/A”结果应该保留,只需删除绿色三角形即可。 有人知
我是一名优秀的程序员,十分优秀!