尝试次数不准确(for循环)



是否可以显示猜测/尝试的次数,而不计算抛出异常时的次数?因为执行程序时的尝试次数包含了异常。当抛出异常时,我使用减量,但这不起作用。

下面是我的代码:

import java.util.*;
public class Main {
public static void main(String[] args) {
int min = 1;
int max = 50;
int rndm = (int) (Math.random() * (max - min) + 1) + min;
System.out.println("Guess a number from 1 to 50!");
for (int x = 1; x > 0; x++) {
Scanner sc = new Scanner(System.in);
System.out.print("Guess the randomized number: ");
int jv = sc.nextInt();
try {
if (jv < min || jv > max) {
throw new IllegalArgumentException();
}
else if (jv >= min && jv <= max) {
if (jv == rndm) {
System.out.println("The randomized number is " + rndm + ". "
+ "Your guess is correct!");
int atmpts = x;
System.out.println("You got it in " + atmpts + " attempts!");
System.exit(0);
}
else if (jv > rndm) {
System.out.println("Too high! Try again.");
}
else if (jv < rndm) {
System.out.println("Too low! Try again.");
}
}
}
catch (IllegalArgumentException ex) {
System.out.println("Invalid input! Input must be a number from 1 to 50. Try again.");
x = x--;
}
catch (InputMismatchException ex) {
System.out.println("Invalid input! Input must be a number. Try again.");
x = x--;
}
}
}
}

无需在每次需要从用户处获取输入时创建新的Scanner。在进入循环之前,创建一次Scanner

这行代码是不正确的:

x = x--;

应该是:

--x;

参考Oracle的Java教程


(注意,我使用Random类来获得一个随机数。)
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int min = 1;
int max = 50;
Random rand = new Random();
int rndm = rand.nextInt(1, 51); // (int) (Math.random() * (max - min) + 1) + min;
System.out.println("Guess a number from 1 to 50!");
Scanner sc = new Scanner(System.in);
for (int x = 1; x > 0; x++) {
System.out.print("Guess the randomized number: ");
int jv = sc.nextInt();
try {
if (jv < min || jv > max) {
throw new IllegalArgumentException();
}
else if (jv >= min && jv <= max) {
if (jv == rndm) {
System.out.println("The randomized number is " + rndm + ". "
+ "Your guess is correct!");
int atmpts = x;
System.out.println("You got it in " + atmpts + " attempts!");
System.exit(0);
}
else if (jv > rndm) {
System.out.println("Too high! Try again.");
}
else if (jv < rndm) {
System.out.println("Too low! Try again.");
}
}
}
catch (IllegalArgumentException ex) {
System.out.println("Invalid input! Input must be a number from 1 to 50. Try again.");
--x;
}
catch (InputMismatchException ex) {
System.out.println("Invalid input! Input must be a number. Try again.");
--x;
}
}
}
}

try this,

public static void main(String[] args) {
int min = 1;
int max = 50;
int rndm = (int) (Math.random() * (max - min) + 1) + min;
int x = 0;
System.out.println("Guess a number from 1 to 50!");
Scanner sc = new Scanner(System.in);
while(true) {    
System.out.print("Guess the randomized number: ");
int jv = sc.nextInt();
try {
if (jv < min || jv > max) {
throw new IllegalArgumentException();
}
else if (jv >= min && jv <= max) {
x++;
if (jv == rndm) {
System.out.println("The randomized number is " + rndm + ". "
+ "Your guess is correct!");
int atmpts = x;
System.out.println("You got it in " + atmpts + " attempts!");
System.exit(0);
}
else if (jv > rndm) {
System.out.println("Too high! Try again.");
}
else if (jv < rndm) {
System.out.println("Too low! Try again.");
}
}
}
catch (IllegalArgumentException ex) {
System.out.println("Invalid input! Input must be a number from 1 to 50. Try again.");
}
catch (InputMismatchException ex) {
System.out.println("Invalid input! Input must be a number. Try again.");
}
}
}

最新更新