gpt4 book ai didi

java - LinkedList 永远不会终止 Java 中的输入

转载 作者:太空宇宙 更新时间:2023-11-04 14:40:57 25 4
gpt4 key购买 nike

我正在用Java制作一个链表程序。但是,当我调用方法 get_values(); 时它不断要求输入。我似乎无法阻止它。由于我无法找出代码中的问题,因此我将整个程序代码粘贴到此处。

import java.io.*; //Importing Java Input-Output Library
class Node
{
int x;
Node next; //Initializing Node data type named next
Node() //method to set all values to 0
{
x=0;
next=null;
}

void get_data(int a) //get data from user
{
x=a;
}

int put_data() // print returned values
{
return x;
}
}

class List
{
Node start; //Another node (user created variable)
List()
{
start=null;
}

void Add_Node(int a) // Add or Creating new nodes and linking them
{
Node n=new Node();
n.get_data(a);
if(start==null)
{
start=n;
}
else if(start!=null)
{
for(Node temp=start, prev=start; temp!=null; prev=temp, temp=temp.next) //sorting
{
if(temp.x>n.x && temp==start)
{
n.next=temp;
start=n;
}
else if(temp.x<n.x && temp.next==null)
{
temp.next=n;
}
else if(temp.x>n.x && prev.x<n.x)
{
n.next=temp;
prev.next=n;
}
}
}
}
void traverse() //printing traversed values
{
for(Node temp=start; temp!=null; temp=temp.next)
{
System.out.print(temp.put_data()+" ");
}
}
void extraction(int b) throws IOException
{
InputStreamReader ist=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ist);
int a;
for(int i=0; i<5; i++)
{
System.out.println("Enter the value for node "+i+":");
a=Integer.parseInt(br.readLine());
Add_Node(a);
}
traverse();
Node store_new=null;
boolean match_check=false;
for(Node temp=start, prev=start; temp!=null; prev=temp, temp=temp.next)
{
if(temp.x==b && temp!=null)
{
match_check=true;
store_new=temp;
break;
}
else if(temp.x!=b)
{
//match_check=false;
}
}
if(match_check==true)
{
System.out.println("Value found. The value address is: "+store_new+" and the value stored in it is: "+store_new.x);
}
else if(match_check==false)
{
System.out.println("Value not found in the list, please enter a value which exists in the list.");
System.exit(0);
}
}
}

public class LinkedList //main class
{
static void get_values() throws IOException
{
InputStreamReader ist=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ist);
List l=new List();
int a;
for(int i=0; i<5; i++)
{
System.out.println("Enter the value for node "+i+":");
a=Integer.parseInt(br.readLine());
l.Add_Node(a);
}
l.traverse();
}
static void extract() throws IOException
{
InputStreamReader ist=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ist);
List l=new List();
int b;
System.out.println("Enter the value for extraction: ");
b=Integer.parseInt(br.readLine());
l.extraction(b);
}
static void sublist()
{

}
public static void main(String[] args) throws IOException
{
InputStreamReader ist=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ist);
List l = new List();
int input;
boolean Menu_check=false;
System.out.println("Enter a the corresponding number to select the menu and then press enter");
System.out.println("1. Enter values and traverse them");
System.out.println("2. Enter a value and find it in the list");
System.out.println("3. Enter a range to be extracted from the list");
System.out.println("4. Exit?");
input=Integer.parseInt(br.readLine());
if(input<5 && input>0)
{
Menu_check=true;
}
while(Menu_check=true)
{
switch(input)
{
case 1: get_values();
break;
case 2: extract();
break;
case 3: sublist();
break;
case 4: System.out.println("Goodbye");
System.exit(0);
break;
}
}
}
}

有人可以检查一下我的代码并帮助我吗?

最佳答案

  1. 在 java 中比较基元与 ==

  2. 存在逻辑错误。我认为您想再次询问用户输入,但输入仍然是 4,并且只有在输入小于 5 且大于 0 时才执行某些操作。

将你的 main 更改为:

    InputStreamReader ist=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ist);
List l = new List();
int input;

while(true)
{
System.out.println("Enter a the corresponding number to select the menu and then press enter");
System.out.println("1. Enter values and traverse them");
System.out.println("2. Enter a value and find it in the list");
System.out.println("3. Enter a range to be extracted from the list");
System.out.println("4. Exit?");
input=Integer.parseInt(br.readLine());

if(input<5 && input>0)
{
switch(input)
{
case 1: get_values();
break;
case 2: extract();
break;
case 3: sublist();
break;
case 4: System.out.println("Goodbye");
System.exit(0);
break;
}
}
}

您还可以删除情况 4 并执行 while(input != 4) 而不是 while(true)。如果您这样做,则必须使用不等于 4 的值来初始化 input

关于java - LinkedList 永远不会终止 Java 中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24935235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com