我编写了一个程序来模拟逻辑门操作。我的代码如下,

import java.io.*;
import java.util.Scanner;

// This program simulates the logic gates operations for the given input
class GatesSimulator
{
    int input1, input2, output;
    boolean boolInput1, boolInput2, boolOutput, validate = true;

    void simulateAndGate()
    {
        getInput();
        boolOutput = boolInput1 & boolInput2;
        showOutput("AND");
    }

    void simulateOrGate()
    {
        getInput();
        boolOutput = boolInput1 | boolInput2;
        showOutput("OR");
    }

    void simulateNotGate()
    {
        System.out.println("Under Development");
    }

    void simulateXorGate()
    {
        getInput();
        boolOutput = boolInput1 ^ boolInput2;
        showOutput("XOR");
    }

    void simulateNandGate()
    {
        getInput();
        boolOutput = !(boolInput1 & boolInput2);
        showOutput("NAND");
    }

    void simulateNorGate()
    {
        getInput();
        boolOutput = !(boolInput1 | boolInput2);
        showOutput("NOR");
    }

    void getInput()
    {

        Scanner simInput = new Scanner(System.in);

        while (validate)
        {

            // Scanner simInput = new Scanner(System.in);
            System.out.println("First Input:  ");
            input1 = simInput.nextInt();

            System.out.println("Second Input:  ");
            input2 = simInput.nextInt();
            // simInput.close();
            if ((input1 == 1 || input1 == 0) && (input2 == 1 || input2 == 0))
            {
                boolInput1 = (input1 == 1) ? true : false;
                boolInput2 = (input2 == 1) ? true : false;
                validate = false;
            }
            else
            {
                System.out.println("Enter a Valid Input(1/0)");
            }
        }
        simInput.close();
    }

    void showOutput(String gate)
    {

        output = (boolOutput == true) ? 1 : 0;
        System.out.println(input1 + " " + gate + " " + input2 + " = " + output);

    }
}

class Operations
{
    int choice;
    GatesSimulator simulator = new GatesSimulator();
    boolean contd = true;

    void chooseOperation()
    {

        Scanner input = new Scanner(System.in);

        while (contd)
        {
            System.out
                .println("Enter Your Choice of Simulation\n\t1 -> AND         Gate\n\t2 -> OR Gate\n\t3 -> NOT Gate\n\t4 -> XOR Gate\n\t5 -> NAND Gate\n\t6 -> NOR Gate\n\t7 -> Exit");
            // Scanner input = new Scanner(System.in);
            choice = input.nextInt();
            // input.close();
            switch (choice)
            {
            case 1:
                simulator.simulateAndGate();
                break;
            case 2:
                simulator.simulateOrGate();
                break;
            case 3:
                simulator.simulateNotGate();
                break;
            case 4:
                simulator.simulateXorGate();
                break;
            case 5:
                simulator.simulateNandGate();
                break;
            case 6:
                simulator.simulateNorGate();
                break;
            case 7:
                contd = false;
                break;
            default:
                System.out.println("\tEnter A valid Choice(1 to 7)\t");
            }
        }
        input.close();
    }
}

public class LogicGatesSimulator
{
    public static void main(String args[])
    {
        Operations gates = new Operations();
        System.out.println("\t\t\tLogic gate Simulator\t\t\t");
        gates.chooseOperation();
    }
}


我得到类似的输出:

Exception in thread "main" java.util.NoSuchElementException
     at java.util.Scanner.throwFor(Scanner.java:855)
     at java.util.Scanner.next(Scanner.java:1478)
     at java.util.Scanner.next(Scanner.java:1478)
     at java.util.Scanner.nextInt(Scanner.java:2067)
     at Operations.chooseOperation(LogicGatesSimulator.java:103)
     at LogicGatesSimulator.main(LogicGatesSimulator.java:132)


我的代码有什么问题。

最佳答案

您需要在调用hasNext()之前使用nextInt()

    if(scanner.hasNextInt())
        scanner.nextInt();

关于java - 尝试运行我的代码时出现NoSuchElementFound异常-怎么了?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12279313/

10-10 15:57