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

时间:2014-07-24 13:33:47

标签: java input

我正在用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;
            }
        }
    }  
}

有人可以查看我的代码并帮助我吗?

3 个答案:

答案 0 :(得分:0)

您正在使用Menu_check=true表示休息状况。在Java(以及大多数(如果不是所有其他语言)中,一个=设置变量,==用于比较。

您每次都将Menu_check设置为true,因此您的循环将无限期地继续。你想要的是Menu_check==true

但是,如果你真的想要一个交互式控制台,你应该摆脱你的Menu_check布尔值,然后像这样做一个while循环:

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)
    {
        break;
    }

    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 :(得分:0)

更正

1.Keep true == Menu_check。

2.在切换

之前,将此片段保持在内部
input=Integer.parseInt(br.readLine());
        if(input<5 && input>0)
        {
            Menu_check=true;
        }else
        {
            Menu_check=false;
        }

使其成为交互式菜单

答案 2 :(得分:0)

  1. 将原语与java中的==进行比较

  2. 存在逻辑失败。我想你想要再次询问用户输入仍然输入是4并且只在输入小于5且大于0时才做某事。

  3. 将您的主要内容更改为:

        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