线程" main"中的例外情况java.util.NoSuchElementException(如何修复错误)

时间:2017-10-10 19:11:38

标签: java exception java.util.scanner

这个错误让我感到紧张。

import java.util.Scanner;
import java.lang.Math;

class Main {
  public static void main(String[] args){


      Scanner scan = new Scanner(System.in);

      System.out.println("Welcome. What is your name?");
      String x;
      x = scan.nextLine();

      System.out.println("Hello " + x + ". Try your best to crack the code!");
      System.out.println(" ");

//Phase 1
      System.out.println("PHASE 1");

      System.out.println("Enter a number:");
      int y = scan.nextInt();
      if (y == 3){
        System.out.println("Correct!");
        System.out.println(" ");
//Phase 2
        System.out.println("PHASE 2");
        System.out.println("Enter a number:");
      }
      int z = scan.nextInt();
      if (z == 1 || (z >= 33 && z <= 100)){
        System.out.println("Correct!");
        System.out.println(" ");
//Phase 3
        System.out.println("PHASE 3");
        System.out.println("Enter a number:");}
      int c = scan.nextInt();
      if (c % 3 == 0 || c % 7 == 0){
        System.out.println("Correct!");
        System.out.println("You have cracked the code!");}
      else{
        System.out.println("Sorry, that was incorrect!");
        System.out.println("Better luck next time!");}


    }
}

我的代码:

<string name="song_number_and_title">"%1$d ~ %2$s"</string>

如果输入不正确,想要结束整个程序。 不确定究竟是什么错。 现在已经坚持了一段时间。 你怎么知道扫描仪和输入有什么问题?

1 个答案:

答案 0 :(得分:1)

您的错误纯属逻辑错误。你不应该到处都是。 你也没有消耗/ n&#34;新线&#34;按下enter会在nextInt()上生成。

为避免混淆您在嵌套if上的自我开放和结束评论 在创建if时创建框架是一个很好的做法,并使用空格将所有内容隔开。

if( conditon) 
{
      if_statement(s);
}
else
{
      else_statement(s);
}

这是您更正后的代码

import java.util.Scanner;
import java.lang.Math;

class BrockTaylor
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Welcome. What is your name?");
        String x;
        x = scan.nextLine();
        System.out.println("Hello " + x + ". Try your best to crack the code!");
        System.out.println(" ");
        System.out.println("PHASE 1");
        System.out.println("Enter a number:");
        int y = scan.nextInt();
        // consume the /n
        scan.nextLine();
        //Phase 1
        if (y == 3)
        {
            System.out.println("Correct!");
            System.out.println(" ");
            System.out.println("PHASE 2");
            System.out.println("Enter a number:");
            int z = scan.nextInt();
            // consume the /n
            scan.nextLine();
            //Phase 2
            if (z == 1 || z >= 33 && z <= 100)
            {
                System.out.println("Correct!");
                System.out.println(" ");
                System.out.println("PHASE 3");
                System.out.println("Enter a number:");
                int c = scan.nextInt();
                // consume the /n
                scan.nextLine();
                //Phase 3
                if (c % 3 == 0 || c % 7 == 0)
                {
                    System.out.println("Correct!");
                    System.out.println("You have cracked the code!");
                }
                else // phase 3
                {
                    System.out.println("Sorry, that was incorrect!");
                    System.out.println("Better luck next time!");
                }
            }
            else // phase 2
            {
                System.out.println("Sorry, that was incorrect!");
                System.out.println("Better luck next time!");
            }
        }
        else// phase 1
        {
        System.out.println("Sorry, that was incorrect!");
        System.out.println("Better luck next time!");
        }
    }
}