![](/img/trans.png)
[英]java extending a arraylist i would like to know what this particular piece of code does
[英]I would like to know what's wrong with this code
我想將 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 有問題,要么沒有任何幫助......或者我只是沒有讓自己清楚......看,這是我的問題......
它運行,是的,但它只是跳到輸出“輸入錯誤”的其他部分......
請記住, 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.