【问题标题】:Scanner and ArrayList with letters带有字母的扫描仪和 ArrayList
【发布时间】:2018-02-20 21:09:26
【问题描述】:

我想声明一个名为“课程”的数组列表,但我的代码有问题。我还想打印笔记和课程。

ArrayList<Integer> listNotes = new ArrayList();
ArrayList<String> listCourses = new ArrayList();

int notes;
String[] courses = {"Science", "History", "English"};

 for(int i=0; i<5;i++){
   //System.out.print((i+1) + " note(s) : ");
   System.out.print((courses[i]) + " Note : ");
   notes = INPUT.nextInt();
   listNotes.add(notes);
 }

 System.out.print("Display : " );

感谢您的帮助。

【问题讨论】:

  • 问题是……?
  • @DaveNewton 我还想打印一下笔记和课程。
  • 您已经描述了您想要的,但我们需要知道您遇到了什么问题。显然你的代码没有做你想要的?它应该做什么,而现在它又做了什么?
  • @julie 好的,有什么问题?
  • @JJJ: 我想显示我的 2 arrayList 的结果:-(

标签: java arraylist


【解决方案1】:

youassassin 是正确的,你的问题是你的 for 循环迭代次数太多,否则你的代码正在做它应该做的事情。

当你迭代一个数组时,最安全的方法是在终止条件中使用它的长度。

for(int i=0; i < courses.length; i++) {
  //System.out.print((i+1) + " note(s) : ");
  System.out.print((courses[i]) + " Note : ");
  notes = INPUT.nextInt();
  listNotes.add(notes);
}

如果您处理不同大小的数组或处理变化的数组,这种方法会给您带来优势。例如,如果您想扩展课程数组,则不必调整循环必须执行的迭代次数,因为循环本身将查找数组的大小并为每个数组元素进行一次迭代。

【讨论】:

  • 谢谢。现在我想打印,但它不起作用.... :-( System.out.print(courses[i] + notes[i] + "notes");
  • 您的意思是要打印ArrayList ListNotes 的所有元素吗?变量 notes 只是一个 int,而不是 int[],因此 notes[i] 应该给你一个语法错误
  • @Ivo Vidovic:我的问题已经解决了。谢谢你的帮助我的朋友。
【解决方案2】:
    ArrayList<Integer> listNotes = new ArrayList(); ArrayList<String> listCourses = new ArrayList(); 
int notes; 
String[] courses = {"Science", "History", "English"}; 
    Scanner input = new Scanner(System.in);
    for(int i=0; i<courses.length;i++){
 //System.out.print((i+1) + " note(s) : "); System.out.print((courses[i]) + " Note :"); 
notes = input.nextInt(); listNotes.add(notes); } System.out.print("Display : " );

【讨论】:

    【解决方案3】:

    这是解决方案。

    public class Main {
    
        public static void main(String[] args) {
            Scanner INPUT = new Scanner(System.in);
    
            ArrayList<Integer> listNote = new ArrayList();
    
            int note = 0;
            String[] cours = {"Math", "Geo", "Os"};
            double somme = 0;
    
            for(int i = 0; i<cours.length;i++){
                System.out.print(cours[i] + " : " );
                note = INPUT.nextInt();
                listNote.add(note);
                somme = somme + note;
            }
    
            //System.out.print("La note est de " + somme);
            double moyenne = somme / 3;
            System.out.println("Résumé");
    
            for(int i = 0; i<cours.length;i++){
                System.out.println(cours[i] + " " +  listNote.get(i));
            }
    
            System.out.print("moyenne = " + moyenne);
        }
    }
    

    【讨论】:

      【解决方案4】:

      我想为您提供一个使用 HashMap 的替代解决方案:

      public class Main {
          public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
              String input;
              Map<String, String> courses = new HashMap<>();
              //courses.put("add your course here","add your note here");
              courses.put("science", "my notes my notes on Science");
              courses.put("history", "my notes on History");
              courses.put("english", "my notes on English");
      
              while (true) {
                  System.out.println("Which note would you like to check out?");
                  input = scanner.nextLine().toLowerCase();
      
                  if (courses.keySet().contains(input))
                      System.out.println("Found notes on " + input + ": " + courses.get(input));
                  else
                      System.out.println("Couldn't find notes on " + input);
                  System.out.println("Another search? (Y/N)");
                  input = scanner.nextLine().toLowerCase();
                  if (input.equals("n"))
                      break;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 2020-12-14
        相关资源
        最近更新 更多