ArrayList的Y / N在Java中输入值 [英] arraylist y/n to enter values in java

查看:117
本文介绍了ArrayList的Y / N在Java中输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建3个字段(每一个字符串)这个code心不是工作,需要帮​​助修复一个数组列表。任务似乎pretty直线前进,但我无法弄清楚什么是错。我运行的NetBeans

这是我迄今为止

 进口java.util.Scanner中;
进口的java.util.ArrayList;
公共类scientist1
{
    公共静态无效的主要(字串[] args)
    {
    扫描仪kbd1 =新的扫描仪(System.in);
    ArrayList的<文件> someStuff =新的ArrayList<文件>();
    焦炭退出='Y';
    串科学家,场,greatIdea;        而(退出=='Y')
        {
            System.out.print(\\ n科学家的名​​字:);
            科学家= scan.next();
            System.out.print(科学家场:);
            字段= scan.next();
            System.out.print(科学家伟大的想法:);
            greatIdea = scan.nextInt();
            someStuff.add(新文件(场,科学家,greatIdea));
            System.out.print(输入另一位科学家(Y / N)?);
            串词= scan.next();
            字= word.toUpperCase();
            退出= word.charAt(0);
        }
        为(文件的东西:someStuff)
        的System.out.println(东西);
}
}公共类文档
{
公共静态字符串科学家,场;
私人诠释greatIdea;
公开文件(最后字符串,字符串首先,字符串想法)
{
    科学家=第一;
    场=最后;
    greatIdea =理念;
}
公共字符串的toString()
{
    返回\\ n \\ n名称:+现场+,+科学家+\\ n文件code:+ greatIdea +\\ n;
}
公共布尔等于(对象除外)
{
    返回(field.equals(((文献)其他).getLast())及&放大器;
    scientist.equals(((文献)其他).getFirst()));
}
公众诠释的compareTo(对象除外)
{
    INT结果;
    串otherFirst =((文献)其他).getFirst();
    串otherLast =((文献)其他).getLast();
    如果(field.equals(otherLast))
        结果= scientist.compareTo(otherFirst);
    其他
        结果= field.compareTo(otherLast);
    返回结果;
}
公共字符串getFirst()
{
    返回的科学家;
}
公共字符串getLast()
{
    回场;
}
}


解决方案

好吧,向您展示如何做到这一点,并给你一个机会来解决问题,你的code我做出了表率。就像我nerdlyist嵌套在一类简单的主类里面。我还使用了内置的排序功能,通过hagmic建议,从比较的类。这意味着,因为你没有写自己比较节省时间和精力。

 进口java.util.Scanner中;
进口的java.util.List;
进口的java.util.ArrayList;
进口了java.util.Comparator;公共类的测试{
    私人扫描程序扫描=新的扫描仪(System.in);
    私人列表< LayoutOfScientist> scientistsNames =新的ArrayList< LayoutOfScientist>();
    私人字符串名称,域,理念;
    私人布尔continueLoop = TRUE;
    私人INT countTo3 = 0;    私人无效的run(){
        而(countTo3 3;&放大器;&放大器; continueLoop){
            如果(countTo3大于0){
                的System.out.println(你想添加另一位科学家(Y / N)?);
            }            如果(countTo3 == 0 || scan.nextLine()。equalsIgnoreCase(Y)){
                的System.out.println(请输入科学家的名​​字:);
                名称= scan.nextLine();
                的System.out.println(请输入科学家的领域:);
                字段= scan.nextLine();
                的System.out.println(请输入科学家的想法:);
                想法= scan.nextLine();
                scientistsNames.add(新LayoutOfScientist(名,字段,想法));
            }其他{
                continueLoop = FALSE;
            }
            countTo3 ++;
        }        scientistsNames.sort(Comparator.comparing(LayoutOfScientist :: getScientistName));
        对于(LOS LayoutOfScientist:scientistsNames){
            的System.out.println(lOS.getScientistName()+,+ lOS.getScientistField()+,+ lOS.getScientistIdea());
        }
    }    私有类LayoutOfScientist {
        私人字符串scientistName,scientistField,scientistIdea;        私人LayoutOfScientist(字符串scientistName,字符串scientistField,字符串scientistIdea){
            this.scientistName = scientistName;
            this.scientistField = scientistField;
            this.scientistIdea = scientistIdea;
        }        公共字符串getScientistName(){
            返回scientistName;
        }        公共字符串getScientistField(){
            返回scientistField;
        }        公共字符串getScientistIdea(){
            返回scientistIdea;
        }
    }    公共静态无效的主要(字串[] args){
        新的测试()的run()。
    }
}

输入示例

 
空间
2
ÿ
汤姆
地球
3
ÿ
短发
空间
6

输出

 鲍勃,空间,6
丹,空间,2
汤姆,地球,3

i need to create an array list with 3 fields (each a string) this code isnt working and need assistance fixing it. the task seems pretty straight forward but i am unable to figure out whats wrong. i am running netbeans

here is what i have so far

import java.util.Scanner;
import java.util.ArrayList;
public class scientist1
{
    public static void main (String[] args)
    {
    Scanner kbd1 = new Scanner(System.in);
    ArrayList<Document> someStuff = new ArrayList<Document>();
    char quit = 'Y';
    String scientist, field, greatIdea;

        while (quit == 'Y')
        {
            System.out.print("\n Scientists name: ");
            scientist = scan.next();
            System.out.print(" scientists field: ");
            field = scan.next();
            System.out.print(" Scientists great Ideas: ");
            greatIdea = scan.nextInt();
            someStuff.add (new Document(field, scientist, greatIdea));
            System.out.print(" Enter Another Scientist? (Y/N)");
            String word = scan.next();
            word = word.toUpperCase();
            quit= word.charAt(0);
        }
        for(Document stuff : someStuff)
        System.out.println(stuff);
}
}



public class Document
{
public static String scientist, field;
private int greatIdea;
public Document (String Last, String First,String Idea)
{
    scientist = First;
    field = Last;
    greatIdea = Idea;
}
public String toString ()
{
    return "\n\n Name: " + field + ", " + scientist + "\n Document Code: " + greatIdea + "\n";
}
public boolean equals (Object other)
{
    return (field.equals(((Document)other).getLast())&&
    scientist.equals(((Document)other).getFirst()));
}
public int compareTo (Object other)
{
    int result;
    String otherFirst = ((Document)other).getFirst();
    String otherLast = ((Document)other).getLast();
    if (field.equals(otherLast))
        result = scientist.compareTo(otherFirst);
    else
        result = field.compareTo(otherLast);
    return result;
}
public String getFirst ()
{
    return scientist;
}
public String getLast ()
{
    return field;
}
}

解决方案

Okay to show you how to do it and to give you a chance to solve the problems with your code I made an example. Like nerdlyist I nested the a class inside the main class for simplicity. I also used the inbuilt sort function, suggested by hagmic, from the comparator class. This means it saves time and effort because you don't have to write your own comparator.

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;

public class Test {
    private Scanner scan = new Scanner(System.in);
    private List<LayoutOfScientist> scientistsNames = new ArrayList<LayoutOfScientist>();
    private String name, field, idea;
    private boolean continueLoop = true;
    private int countTo3 = 0;

    private void run() {
        while(countTo3<3&&continueLoop) {
            if(countTo3>0) {
                System.out.println("Would you like to add another scientist? (Y/N)");
            }

            if(countTo3 == 0 || scan.nextLine().equalsIgnoreCase("y")) {
                System.out.println("Please enter the scientist's name:");
                name = scan.nextLine();
                System.out.println("Please enter the scientist's field:");
                field = scan.nextLine();
                System.out.println("Please enter the scientist's idea:");
                idea = scan.nextLine();
                scientistsNames.add(new LayoutOfScientist(name, field, idea));
            } else {
                continueLoop = false;
            }
            countTo3++;
        }

        scientistsNames.sort(Comparator.comparing(LayoutOfScientist::getScientistName));
        for(LayoutOfScientist lOS : scientistsNames) {
            System.out.println(lOS.getScientistName() + ", " + lOS.getScientistField() + ", " + lOS.getScientistIdea());
        }
    }

    private class LayoutOfScientist {
        private String scientistName, scientistField, scientistIdea;

        private LayoutOfScientist(String scientistName, String scientistField, String scientistIdea) {
            this.scientistName = scientistName;
            this.scientistField = scientistField;
            this.scientistIdea = scientistIdea;
        }

        public String getScientistName() {
            return scientistName;
        }

        public String getScientistField() {
            return scientistField;
        }

        public String getScientistIdea() {
            return scientistIdea;
        }
    }

    public static void main(String[] args) {
        new Test().run();
    }
}

Example input

Dan
Space
2
Y
Tom
Earth
3
Y
Bob
Space
6

Output

Bob, Space, 6
Dan, Space, 2
Tom, Earth, 3

这篇关于ArrayList的Y / N在Java中输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆