Java 如何跟踪用户尝试输入数据的总次数
我做了一个非常简单的猜谜游戏。但是我想计算用户在找到原始号码之前所做的尝试总数。这意味着,一旦隐藏号码与用户输入匹配,它就应该知道在用户获胜之前已经输入了多少号码 这是我的代码:Java 如何跟踪用户尝试输入数据的总次数,java,Java,我做了一个非常简单的猜谜游戏。但是我想计算用户在找到原始号码之前所做的尝试总数。这意味着,一旦隐藏号码与用户输入匹配,它就应该知道在用户获胜之前已经输入了多少号码 这是我的代码: package guessinggame; import java.util.Scanner; public class GuessingGame { public static void main(String[] args) { int hiddenNum = 10;
package guessinggame;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int hiddenNum = 10;
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Enter a numebr by guessing: ");
int guessedNum = input.nextInt();
if (guessedNum == hiddenNum) {
System.out.println("Congratulation! Your number is matched");
System.exit(0);
} else if (guessedNum < hiddenNum) {
System.out.println("Not matched! Try a bigger number");
} else if (guessedNum > hiddenNum) {
System.out.println("Not matched! Try a smaller number");
}
}
}
}
包猜游戏;
导入java.util.Scanner;
公开课猜谜游戏{
公共静态void main(字符串[]args){
int-hiddenNum=10;
扫描仪输入=新扫描仪(System.in);
while(true){
System.out.println(“通过猜测输入numebr:”;
int guessedNum=input.nextInt();
if(猜测数==hiddenNum){
System.out.println(“恭喜!您的号码匹配”);
系统出口(0);
}else if(猜测数隐藏数){
System.out.println(“不匹配!请尝试较小的数字”);
}
}
}
}
试试这个:
public static void main(String[] args) {
//storing the hidden number in a variable
int hiddenNum = 10;
//introducing the scanner class (for user input)
Scanner input = new Scanner(System.in);
//asking user to enter a random number
int attempts = 0; // use as a counter
while (true) {
System.out.println("Enter a numebr by guessing: ");
//Storing that into a variable
int guessedNum = input.nextInt();
attempts += 1;
//Checking wheather the number is matched
if (guessedNum == hiddenNum) {
System.out.println("Congratulation! Your number is matched");
//System.out.println("You have made total of " + attempts +" attempts to find the number!");
//If matched number found, terminate the program
System.exit(0);
//checking another condition
} else if (guessedNum < hiddenNum) {
System.out.println("Not matched! Try a bigger number");
//checking another condition
} else if (guessedNum > hiddenNum) {
System.out.println("Not matched! Try a smaller number");
}
}
}
publicstaticvoidmain(字符串[]args){
//将隐藏的数字存储在变量中
int-hiddenNum=10;
//介绍scanner类(用于用户输入)
扫描仪输入=新扫描仪(System.in);
//要求用户输入一个随机数
int尝试次数=0;//用作计数器
while(true){
System.out.println(“通过猜测输入numebr:”;
//将其存储到变量中
int guessedNum=input.nextInt();
尝试次数+=1;
//检查号码是否匹配
if(猜测数==hiddenNum){
System.out.println(“恭喜!您的号码匹配”);
//System.out.println(“您总共进行了“+次尝试+”次尝试来查找号码!”);
//如果找到匹配的号码,终止程序
系统出口(0);
//检查另一个条件
}else if(猜测数隐藏数){
System.out.println(“不匹配!请尝试较小的数字”);
}
}
}
}else if(猜测数这个回答更适合你,但它是这样的
package guessinggame;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int hiddenNum = 10;
int guessedNum = 0;
Scanner input = new Scanner(System.in);
boolean running = true;
for (int i = 1; running; i++) {
System.out.println("Enter a number to guess: ");
guessedNum = input.nextInt();
if (guessedNum == hiddenNum) {
System.out.println("Congratulation! Your number is matched");
System.out.println("You have made " + i + " to find the number!");
running = false;
} else if (guessedNum < hiddenNum) {
System.out.println("Not matched! Try a bigger number");
} else {
System.out.println("Not matched! Try a smaller number");
}
}
}
}
包猜游戏;
导入java.util.Scanner;
公开课猜谜游戏{
公共静态void main(字符串[]args){
int-hiddenNum=10;
int guessedNum=0;
扫描仪输入=新扫描仪(System.in);
布尔运行=真;
for(int i=1;正在运行;i++){
System.out.println(“输入一个要猜测的数字:”);
guessedNum=input.nextInt();
if(猜测数==hiddenNum){
System.out.println(“恭喜!您的号码匹配”);
System.out.println(“您已经用“+i+”来查找号码!”);
运行=错误;
}else if(猜测数
对代码的一些更改:
- 您应该避免让
和“随机”终止程序。相反,我引入了while(true)
变量。它可以保存boolean
或true
的值。我在开始时将其设置为false
,如果用户猜对了,随后将其设置为true
false
- 我现在删除了注释,一般来说,当代码没有解释自己时,您应该使用注释。例如,在这里,在for循环中使用注释是一个很好的理由。(a
循环在这里是更好的练习,但我只想显示一个虽然
循环。)for
- 对于上一个条件检查,我删除了比较,要么是相等的,要么是更大的,要么是更小的。如果我们测试了相等的,要么是更大的,剩下的都是更小的,因此只有
在那里else
//检查另一个条件
是一个糟糕的注释,它比代码本身告诉的要少。八月,是的,但它应该是什么类型的计数器我很困惑。我在while循环中有if-else条件,所以我不确定如何解决它。不确定!=不能做
,请先尝试,你已经有了int-hiddenNum=10;
。因此你知道如何初始化和声明变量的基本知识。从那里开始,每次猜测都增加变量。这一刻是我的啊时刻。迫不及待地想接受你的答案。是的,谢谢,这是一个非常好的解决方案。是的,不客气,这是一个非常好的解决方案我先发布的。xox
package guessinggame;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int hiddenNum = 10;
int guessedNum = 0;
Scanner input = new Scanner(System.in);
boolean running = true;
for (int i = 1; running; i++) {
System.out.println("Enter a number to guess: ");
guessedNum = input.nextInt();
if (guessedNum == hiddenNum) {
System.out.println("Congratulation! Your number is matched");
System.out.println("You have made " + i + " to find the number!");
running = false;
} else if (guessedNum < hiddenNum) {
System.out.println("Not matched! Try a bigger number");
} else {
System.out.println("Not matched! Try a smaller number");
}
}
}
}