有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中,如何将用户输入的整数发送到链表?

我正在尝试编写代码,让用户输入正整数并将其发送到链接列表。用户输入负数后应结束输入。此外,我在编写isSorted boolean方法时遇到问题,如果链表按递增顺序排序,该方法将返回true,否则返回false

这是我到目前为止唯一的代码

import java.util.*;
public class List {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please type positive integers one by one separated by a space.");
    System.out.println("When you are done, please type a negative integer.");
    int num = input.nextInt();

    }


    public boolean isSorted(){


        if(){ 
        return true;
        }
        else{
        return false;
        }
    }
} 

共 (4) 个答案

  1. # 1 楼答案

    import java.util.LinkedList;
    import java.util.Scanner;
    
    
    public class NumberListing {
    
       public static void main(String[] args) {
           Scanner input = new Scanner(System.in);
           System.out.println("Please type positive integers one by one    separated by a space.");
           System.out.println("When you are done, please type a negative integer.");
           int num = input.nextInt();   
           LinkedList<Integer> list = new LinkedList<Integer>();
           while(num > 0){
    
               list.add(num);
               num = input.nextInt();
           }
    
           if(isSorted(list)){
               System.out.println("The list is sorted");
          } else{
               System.out.println("The list is not sorted");
           }
       }
    
    
           public static boolean isSorted(LinkedList<Integer> list){
               boolean done = false;
               boolean sorted = true;
               for(int i = 1; i < list.size() && !done  ; i++){
                    if(list.get(i) < list.get(i-1)){
                        done = true;
                       sorted = false;
                   }
               }
               return sorted;
           }
      }
    

    应该相当简单。当您输入的数字大于0时,将该数字存储在列表中,然后从控制台获取下一个数字

    然后,循环遍历从索引1开始的列表,检查每个当前数字是否大于前一个数字。我添加了一个提前退出,这样只要一个数字小于前一个数字,循环就会退出。这意味着它是无序的,我们不关心其余的

    然而,如果你想创建自己的链表,这样可以创建更多的代码,我建议你看看教科书或类似的东西来帮助你

  2. # 2 楼答案

    您可以使用int的包装类Integer将int放入容器(LinkedList)中。普通int是标准的基元类型,不能由容器使用,因为容器只能处理类的对象

    LinkedList<Integer> list = new LinkedList();
    

    至于isSorted方法,您可以遍历列表并检查当前值是否高于或等于前一个值

    int prevValue = 0;
    
    for(int i = 0; i < list.size(); i++){
        if(!list.get(i) >= prevValue){
            return false;
        }
        prevValue = list.get(i);
    }
    return true;
    
  3. # 3 楼答案

    您需要将下一行作为字符串读取,拆分该行,并遍历该行的元素列表,同时将正元素添加到LinkedList中。确保您存储了扫描仪。您需要使用nextLine,而不是nextInt

    import java.util.*;
    public class List {
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please type positive integers one by one separated by a space.");
        System.out.println("When you are done, please type a negative integer.");
        String line = input.nextLine();
        String[] integers = line.split(" ");
    
        LinkedList ll = new LinkedList<Integer>();
        for (int i = 0; i < integers.length; i++)
        {
            if (Integer.parseInt(integers[i]) > 0)
                ll.add(integers[i]);
        }
    
        System.out.println(isSorted(ll));
        input.close();
     }
    

    然后,只需为LinkedList使用一个简单的比较函数来检查它是否已排序:

    public static <T extends Comparable<T>> boolean isSorted(LinkedList<T> iterable)
        {
            Iterator<T> iter = iterable.iterator();
    
            if (!iter.hasNext()) {
                    return true;
                }
             T t = iter.next();
                while (iter.hasNext()) {
                    T t2 = iter.next();
                    if (t.compareTo(t2) > 0) {
                        return false;
                    }
                    t = t2;
                }
                return true;
        }
    
  4. # 4 楼答案

    public void input() {
        System.out.println("Please type positive integers one by one separated by a space.");
        System.out.println("When you are done, please type a negative integer.");
    
        LinkedList<Integer> ll = new LinkedList<>();
    
        //System.in.available()
    
        Scanner input = new Scanner(System.in);
    
        int num;
        while ( input.hasNextInt() ) {
            int i = input.nextInt();
            if (i >= 0) {
                ll.add(i);
            }
        }
    
        System.out.println(ll+" <-- ll"); //TODO remove debugging code
    
        System.out.println(isSortedAccending(ll)+" <-- isSortedAccending(ll)");//TODO
    }
    

    这是通过在发现某些东西出现故障时返回false来实现的

    public static boolean isSortedAccending(List<Integer> list){
        if (list.size() < 2) {
            return true;
        }
    
        Integer previous = list.get(0);
        for (Integer next : list) {
            if (previous > next) {
                return false;
            }
        }
        return true;
    }
    

    产出:

    Please type positive integers one by one separated by a space.
    When you are done, please type a negative integer.
    1
    2
    3
    -1
    [1, 2, 3] <-- ll
    true <-- isSortedAccending(ll)
    

    isSortedDecending()看起来完全一样,只是它使用了<