2012-09-05 38 views
1

我寫了一個程序來模擬邏輯門操作。我的代碼如下,嘗試運行我的代碼時出現NoSuchElementFound異常 - 出現了什麼問題?

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) 

什麼是我的代碼的問題。

+0

什麼是我的代碼中的實際問題之前使用hasNext()? – rajesh

+0

你沒有抓到它 –

+0

是不是這個while循環,'while(contd)'只是要永遠運行?也許'nextInt()'在'while(true)'循環中用完「next int」。 –

回答

5

你需要調用nextInt()

if(scanner.hasNextInt()) 
     scanner.nextInt(); 
相關問題