J'ai un programme qui invite un utilisateur à entrer des nombres entiers jusqu'à ce qu'il tape une valeur pour continuer le programme à l'étape suivante. Comment incrémenter une variable appelée count chaque fois que l'utilisateur saisit un entier? Au fait, j'utilise count ++ pour incrémenter le montant du comptage, je ne sais tout simplement pas comment le faire augmenter lorsque l'utilisateur insère des données.
Code jusqu'à présent
//variables
int num, count = 0, high, low;
Scanner userInput = new Scanner(System.in);
//loop
do {
System.out.print("Enter an integer, or -99 to quit: --> ");
num = userInput.nextInt();
high = num;
low = num;
//higher or lower
if(count > 0 && num > high)
{
high = num;
}
else if(count > 0 && num < low)
{
low = num;
}
else
{
System.out.println("You did not enter any numbers.");
}
} while (num != -99);
System.out.println("Largest integer entered: " + high);
System.out.println("Smallest integer entered: " + low);
3 Réponses :
Vous pouvez mettre count ++; n'importe où dans votre boucle do.
C'est simple. Il suffit de mettre count ++ dans la boucle while comme suit,
// variables
int num, count = 0, high, low;
Scanner userInput = new Scanner(System.in);
// loop
do {
System.out.print("Enter an integer, or -99 to quit: --> ");
num = userInput.nextInt();
count++; // here it goes
high = num;
low = num;
// higher or lower
if(count > 0 && num > high)
{
high = num;
}
else if(count > 0 && num < low)
{
low = num;
}
else
{
System.out.println("You did not enter any numbers.");
}
} while (num != -99);
System.out.println("Largest integer entered: " + high);
System.out.println("Smallest integer entered: " + low);
Merci! Étonnant à quel point une chose aussi simple me déroutait Existe-t-il un moyen pour un certain entier de ne pas être inclus dans le décompte? Par exemple, l'utilisateur peut taper -99 pour continuer au-delà de l'invite, mais comment aurais-je -99 non inclus en tant que num ou avoir un incrément de comptage?
En fait, je n'ai pas compris ce que vous avez demandé. Mais si vous entrez -99, cela mettra fin à la boucle for , n'est-ce pas? Mais pour votre question Y a-t-il un moyen pour qu'un certain entier ne soit pas inclus dans le décompte? Oui, vous pouvez. Vous pouvez essayer avec une simple instruction if-else . Faites-moi savoir si vous avez besoin de plus d'aide.
Je comprends pourquoi vous l'utilisez, mais pourquoi utiliser le compteur?
Le code ci-dessous utilise une technique différente pour acquérir des entiers de l'utilisateur. Cela garantit également que le nombre fourni est bien un entier compris dans la plage Integer.MIN_VALUE et Integer.MAX_VALUE:
Scanner userInput = new Scanner(System.in);
String ls = System.lineSeparator();
int high = 0;
int low = Integer.MAX_VALUE;
int num;
String input = "";
while(input.equals("")) {
System.out.print("Enter an integer, (q to quit): --> ");
input = userInput.nextLine().toLowerCase();
if (input.equals("q")) {
if (low == Integer.MAX_VALUE) {
low = 0;
}
break;
}
if (!input.matches("^-?\\d+$")) {
System.err.println("Invalid Entry (" + input + ")! "
+ "You must supply an Integer (int) value!" + ls);
input = "";
continue;
}
boolean invalidInteger = false;
long tmpVal=0;
try {
tmpVal = Long.parseLong(input);
} catch(NumberFormatException ex) {
invalidInteger = true;
}
if (invalidInteger || tmpVal < Integer.MIN_VALUE || tmpVal > Integer.MAX_VALUE) {
System.err.println("Invalid Entry (" + input + ")! " + ls
+ "Number too large (Minimum Allowable: " + Integer.MIN_VALUE
+ " Maximum Allowable: " + Integer.MAX_VALUE + ")!" + ls
+ "You must supply an Integer (int) value!" + ls);
input = "";
continue;
}
num = Integer.parseInt(input);
if (num > high) {
high = num;
}
if (num < low) {
low = num;
}
input = "";
}
System.out.println("Largest integer entered: " + high);
System.out.println("Smallest integer entered: " + low);
Si vous voulez garder une trace du nombre d'entrées le L'utilisateur a fait alors vous pouvez toujours appliquer un compteur si vous le souhaitez.
Que diriez-vous de count ++ ou count = count + 1?