Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6561245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:33:48+00:00 2026-05-25T13:33:48+00:00

When I run the code below, why does it throw this error? Exception in

  • 0

When I run the code below, why does it throw this error?

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at GuessNumber.main(GuessNumber.java:35)

This is my code, thank you:

public class GuessNumber {
    public static void main(String[] args) {
        int[][] num = new int[5][16];
        int[] len = new int[5];
        char[] bit;
        for (int i = 1; i <= 32; i++) {
            bit = ToBinary(i);
            //bit的大小为5:把二进制数存储到数组中num
            for (int j = 0; j < bit.length; j++) {
                if (bit[j] == '1') {
                    //11000
                    num[j][len[j]++] = i;
                }
            }
        }

        Random r = new Random((new Date()).getTime());
        int numRoad = r.nextInt(31);
        bit = ToBinary(numRoad);
        String cardRand = "";
        for (int i = 0; i < bit.length; i++) {
            if (bit[i] == '1') {
                cardRand = cardRand + (i + 1) + ",";
            }
        }
        System.out.println("在卡片" + cardRand + "上的数字是:");
        System.out.println("请玩家输入猜测数字:");
        Scanner c = new Scanner(System.in);
        int number = c.nextInt();
        if (number == numRoad) {
            System.out.println("恭喜您,猜对了.");
        } else {
            System.out.println("对不起!猜错了,该数应该为:" + numRoad);
        }

    }

    /**
     * 将十进制数转成二进制数
     * 
     * @param i
     * @return
     */
    public static char[] ToBinary(int c) {

        char[] bit = new char[5];
        String a = Integer.toBinaryString(c);
        bit = a.toCharArray();
        char temp;
        for (int i = 0; i < bit.length / 2; i++) {
            temp = bit[i];
            bit[i] = bit[bit.length - 1 - i];
            bit[bit.length - 1 - i] = bit[i];
        }
        return bit;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:33:49+00:00Added an answer on May 25, 2026 at 1:33 pm

    Most likely because the Scanner expected an integer value and found something else. The exception is a result of your actual input at the console.


    Looks like you see a binary number (10011) and have to enter the decimal value (19)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the code below, why does it throw this error? Exception in
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
When I try to run the code below the browser does not display an
I am trying to run the code below but it keeps locking up my
the code below works fine but it takes an absolute age to run. How
Any ideas given the code below why the highlight is being triggered to run
When I run a Test Project on Visual Studio I use the code below
I'm trying to run the below-mentioned code in VB(Excel Macro) but I'm stuck with
Does anyone know how best to determine the specific underlying cause of this exception?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.