Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试运行我的代码时,NoTouchElementFind异常-what';怎么了?_Java_Exception Handling - Fatal编程技术网

Java 尝试运行我的代码时,NoTouchElementFind异常-what';怎么了?

Java 尝试运行我的代码时,NoTouchElementFind异常-what';怎么了?,java,exception-handling,Java,Exception Handling,我写了一个程序来模拟逻辑门的操作。我的代码如下: 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, vali

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

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)

我的代码有什么问题。

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


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


我的代码的实际问题是什么?这个while循环,
while(contd)
不会永远运行吗?可能
nextInt()
在for
while(true)
循环中用完了“next int”。我的代码的实际问题是什么?这个while循环,
while(contd)
不就是永远运行吗?可能
nextInt()
在for
while(true)
循环中用完了“next int”。
    if(scanner.hasNextInt())
        scanner.nextInt();