2016-03-30 2 views
-1

文字列変数を使用した複数選択のif文を使用したい... しかし、私の選択した答えや文字を入力することはできず、すぐに "入力エラーが発生しました... このコードの何が問題なのか尋ねることはできますか?このコードで何が問題なのか知りたいのですが

System.out.println("Pick your choice: \na. Add Node\nb. Delete Node\nc. Insert Node"); 
    String letterChoice = scan.nextLine(); 

    if(letterChoice.equalsIgnoreCase("A")){ 
     System.out.println("Enter node value: "); 
     int nodeValue = scan.nextInt(); 
     list.addNodes(nodeValue); 
    } 
    else if(letterChoice.equalsIgnoreCase("B")){ 
     System.out.println("Which node to delete? Node #: "); 
     int thisNode = scan.nextInt(); 
     list.deleteNode(thisNode); 
    } 
    else if(letterChoice.equalsIgnoreCase("C")){ 
     System.out.println("Input node number: "); 
     int thisNode = scan.nextInt(); 
     int prevNode = thisNode - 1; 

     System.out.println("Input node value: "); 
     int nodeValue = scan.nextInt(); 

     list.insertNode(nodeValue, prevNode); 
    } 
    else{ 
     System.out.println("Input Error"); 
    } 

私は自分のコードを変更しない限り、私はここに、以前の質問からの助けを見つけることができませんでした...私はしたくないので、私はお願いしていた...おかげで...

完全なコードすべての助けを...

import java.util.*; 

    public class DoublyLinkedList { 


     static DNode root; 
     static DNode temp; 
     static DNode current; 


     public void addNodes(int data){ 

      DNode dNode = new DNode(data); 

      if(root==null){ 

       root = dNode; 
       root.previousNode = null; 
       root.nextNode = null; 

      }else{ 

       current = root; 

       while(current.nextNode!=null){ 

        current = current.nextNode; 

       } 

       current.nextNode = dNode; 
       dNode.previousNode = current; 
       dNode.nextNode = null; 

      } 

     } 

     public void insertNode(int data, int after){ 

      DNode dNode = new DNode(data); 

      int ithNode = 1; 

      current = root; 

      while(ithNode != after){ 

       current = current.nextNode; 

       ithNode++; 

      } 

      temp = current.nextNode; 

      current.nextNode = dNode; 
      dNode.nextNode = temp; 
      temp.previousNode = dNode; 
      dNode.previousNode = current; 

     } 

     public void deleteNode(int nodeNumber){ 

      int ithNode = 1; 

      current = root; 

      if(nodeNumber==1){ 

       root = current.nextNode; 
       current.nextNode = null; 
       current.previousNode = null; 

      }else{ 

       while(ithNode != nodeNumber){ 

        current = current.nextNode; 

        ithNode++; 

       } 

       if(current.nextNode == null){ 

        current.previousNode.nextNode = null; 
        current.previousNode = null; 

       }else{ 

        current.previousNode.nextNode = current.nextNode; 
        current.nextNode.previousNode = current.previousNode; 

       } 

      } 

     } 

     public void print(){ 

      current = root; 

      System.out.print("The Linked List: "); 

      do{ 

       System.out.print(" " + current.data + " "); 


       current = current.nextNode; 

      }while(current!=null); 

     } 

     public void printBackwards(){ 

      current = root; 

      while(current.nextNode!=null){ 

       current = current.nextNode; 

      } 

      System.out.print("Inverse: "); 

      do{ 

       System.out.print(" " + current.data + " "); 

       current = current.previousNode; 

      }while(current.previousNode!=null); 

      System.out.print(" " + current.data + " "); 

     } 

     public static void main(String[] args){ 

      DoublyLinkedList list = new DoublyLinkedList(); 
      Scanner scan = new Scanner(System.in); 

      String letterChoice = ""; 
      int nodesNum; 

      System.out.print("Input number of nodes: "); 
      nodesNum = scan.nextInt(); 

      for(int x = 1; x <= nodesNum; x++){ 
       System.out.print("Input value of node #" + x + ": "); 
       int value = scan.nextInt(); 
       list.addNodes(value); 
      } 

      list.print(); 


      System.out.println("Pick your choice: \na. Add Node\nb. Delete Node\nc. Insert Node"); 
      letterChoice = scan.nextLine(); 

      if(letterChoice.equalsIgnoreCase("A.")){ 
       System.out.println("Enter node value: "); 
       int nodeValue = scan.nextInt(); 
       list.addNodes(nodeValue); 
      } 
      else if(letterChoice.equalsIgnoreCase("B.")){ 
       System.out.println("Which node to delete? Node #: "); 
       int thisNode = scan.nextInt(); 
       list.deleteNode(thisNode); 
      } 
      else if(letterChoice.equalsIgnoreCase("C.")){ 
       System.out.println("Input node number: "); 
       int thisNode = scan.nextInt(); 
       int prevNode = thisNode - 1; 

       System.out.println("Input node value: "); 
       int nodeValue = scan.nextInt(); 

       list.insertNode(nodeValue, prevNode); 
      } 
      else{ 
       System.out.println("Input Error"); 
      } 

      list.print(); 
      System.out.println(); 
      list.printBackwards(); 

      System.out.println(); 
      System.out.println("The number of DNodes in the Linked List is " + DNode.noOfLinkedList); 


     } 

    } 


    class DNode { 

     static int noOfLinkedList = 0; 

     int data; 

     DNode previousNode; 
     DNode nextNode; 

     DNode(int data){ 

      this.data = data; 
      noOfLinkedList++; 

     } 

    } 

おかげで、何かのいずれかは、多分私はちょうどこれは、見る...自分自身が明らかにしていないよ...私のJDKまたはCMD、またはその何も貢献していないだと間違っているのですか私の問題... Image of the problem

それははい、動作しますが、それだけで

+0

letterChoiceは何ですか? –

+0

それは文字列変数です...文字列... –

+0

あなたは確かに上記の 'nextInt()'を持っています。 –

答えて

0

Scanner#nextInt方法は、あなたの入力の最後に改行文字を消費しないので、その改行が消費されていることを忘れないでください...それは「入力エラー」を出力し、他の部分にスキップ次のScanner#nextLineの呼び出しで呼び出されます。

これを見てみましょう:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine%28%29

クイックフィックス

System.out.print("Input number of nodes: "); 
nodesNum = scan.nextInt(); 
scan.nextLine(); //this force the reading of the last newline after reading the integer... 

... 
System.out.println("Pick your choice: \na. Add Node\nb. Delete Node\nc. Insert Node"); 
final String letterChoice = scan.nextLine(); 
+0

ありがとう...しかし、実際には...助けてくれませんでした... –

+0

あなたはこのscan.nextLine()を追加していますか? forループの内側にも??? –

+0

私は...いけませんでしたか? –

関連する問題