【问题标题】:How can I get this to run?我怎样才能让它运行?
【发布时间】:2021-05-27 11:43:04
【问题描述】:

似乎我已经尝试了所有方法,但没有任何效果。我怎样才能得到这个,以便用户可以决定是否添加另一个名字?我可以在没有用户决定的情况下很好地运行 for 循环。

import java.util.Scanner;
import java.text.DecimalFormat;

public class Part2 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        final int STUDENT_SIZE = 50;

        char choice1 = 'n';
        int i = 0;
        int stdntLength = 0;
        boolean choice = true;

        String[] stdntName = new String[STUDENT_SIZE];
        String[] WIDNUM = new String[STUDENT_SIZE];
        int[] EXM1 = new int[STUDENT_SIZE];
        int[] EXM2 = new int[STUDENT_SIZE];
        int[] EXM3 = new int[STUDENT_SIZE];
        int[] finalExm = new int[STUDENT_SIZE];

        do {
            for (i = 0; i < stdntName.length; i++) {
                System.out.println("Please enter the name of Student "
                        + (i + 1) + ": ");
                stdntName[i] = s.nextLine();
                String fullName = stdntName[i];
                String str[] = fullName.split(" ");
                StringBuilder sb = new StringBuilder();
                sb.append(str[1]);
                sb.append(", ");
                sb.append(str[0]);
                String fullname = sb.toString();

                stdntName[i] = fullname;
                System.out.println(stdntName[i]);

                System.out.print("Do you wish to enter another? (y/n): ");
                choice1 = s.next().charAt(0);
            }
        } while (choice1 == 'y');
    }
}

【问题讨论】:

  • 提示:在for 循环结束时询问用户是要停止还是继续,对for 循环没有丝毫影响:无论如何它都会继续运行.而do/while 循环围绕for 循环,因此在for 循环完成之前它没有任何控制权。

标签: java arrays loops while-loop stringbuilder


【解决方案1】:

do-while 循环似乎是多余的,可以删除。

在输入第i个学生的数据时,最好检查y的输入,如果输入了除y之外的任何字符,则中断。

更新
其他需要解决的问题:

  1. 在拆分全名时检查str的长度;将姓氏移到开头(不仅仅是第二个名)。
  2. 在读取choice1 时使用nextLine() 而不是next() - 因为\n 未被使用,并且使用nextLine 读取的下一个名称将是一个空行。
for (i=0; i < stdntName.length; i++) {
        System.out.println("Please enter the name of Student " + (i+1) + ": ");
        stdntName[i] = s.nextLine();
        String fullName = stdntName[i];
        String str []  = fullName.split(" ");
        if (str.length > 1) {
            StringBuilder sb = new StringBuilder();
            sb.append(str[str.length - 1]); // move the last name to beginning
            sb.append(", ");
            for (int j = 0; j < str.length - 1; j++) { // join remaining names
                if (j > 0) {
                    sb.append(' ');
                }
                sb.append(str[j]);
            }
            
            stdntName[i] = sb.toString();
        }
        System.out.println(stdntName[i]);
        
        System.out.print("Do you wish to enter another? (y/n): ");
        choice1 = s.nextLine().toLowerCase().charAt(0); // read entire line
        if (choice1 != 'y') {
            break;
        }
    }

【讨论】:

  • 由于某种原因它仍然给我同样的错误信息
  • 错误信息和堆栈跟踪是什么?请编辑您的问题以包含此内容。
  • 错误信息是 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Part2.main(Part2.java:35)
猜你喜欢
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-01
  • 1970-01-01
相关资源
最近更新 更多