【发布时间】:2016-03-30 14:25:21
【问题描述】:
我想使用 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 有问题,要么没有任何帮助......或者我只是没有让自己清楚......看,这是我的问题......
它运行,是的,但它只是跳到输出“输入错误”的 else 部分......
【问题讨论】:
-
什么是 letterChoice?
-
这是一个字符串变量...字符串...
-
你上面肯定有
nextInt() -
是的,我的意思是,它们是不同的......
-
letterChoice 和其他带有 .nextInt() 的不同...
标签: java string if-statement linked-list