【问题标题】:How do I make so that when I input a value into a scanner if it is not an integer it won't give me an error?如何使当我将一个值输入到扫描仪中时,如果它不是整数,它不会给我一个错误?
【发布时间】:2012-12-04 07:05:18
【问题描述】:

我正在编写一个程序来解决简单的数学问题。我想要做的是使它即使我在扫描仪级别输入一个字符串也不会给我一个错误。该级别是选择数学问题的难度。我已经尝试过 parseInt,但现在不知道该怎么做。

import java.util.Random;
import java.util.Scanner;
public class Test { 
    static Scanner keyboard = new Scanner(System.in);
    static Random generator = new Random();
    public static void main(String[] args) {
        String level = intro();//This method intorduces the program,
        questions(level);//This does the actual computation.
    }
    public static String intro() {

        System.out.println("HI - I am your friendly arithmetic tutor.");
        System.out.print("What is your name? ");
        String name = keyboard.nextLine();
        System.out.print("What level do you choose? ");
        String level = keyboard.nextLine();
        System.out.println("OK " + name + ", here are ten exercises for you at the level " + level + ".");
        System.out.println("Good luck.");
        return level;
    }
    public static void questions(String level) {
        int value = 0, random1 = 0, random2 = 0;
        int r = 0, score = 0;
        int x = Integer.parseInt("level");
        if (x==1) {
            r = 4;          
        }       
        else if(x==2) {
            r = 9; 
        }
        else if(x==3) {
            r = 50; 
        }
        for (int i = 0; i<10; i++) {
            random1 = generator.nextInt(r);//first random number.
            random2 = generator.nextInt(r);//second random number.
            System.out.print(random1 + " + " + random2 + " = ");
            int ans = keyboard.nextInt();
            if((random1 + random2)== ans) {
                System.out.println("Your answer is correct!");
                score+=1;
            }
            else if ((random1 + random2)!= ans) {
            System.out.println("Your answer is wrong!");
            }
        }
        if (score==10 || score==9) {
            if (score==10 && x == 3) {
                System.out.println("This system is of no further use.");
            }
            else {
                System.out.println("Choose a higher difficulty");
            }
            System.out.println("You got " + score + " out or 10");
        }
        else if (score<=8 && score>=6) {
            System.out.println("You got " + score + " out or 10");
            System.out.println("Do the test again");
        }
        else if (score>6) {
            System.out.println("You got " + score + " out or 10");
            System.out.println("Come back for extra lessons");
        }
    }
}

【问题讨论】:

    标签: java.util.scanner parseint


    【解决方案1】:

    我看到的第一个错误是你试图Integer.parseInt()一个String“级别”而不是字符串变量命名级别

        int x = Integer.parseInt("level");
    

    应该是

        int x = Integer.parseInt(level);
    

    此外,在定义级别时,您可以使用keyboard.nextInt 而不是keyboard.nextLine

    String level = keyboard.nextInt();
    

    那么,您以后就不必再执行Integer.parseInt() 操作了

    【讨论】:

      猜你喜欢
      • 2016-03-10
      • 2022-01-16
      • 2018-03-15
      • 2022-11-29
      • 2022-11-22
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 2011-01-06
      相关资源
      最近更新 更多