java - 我的 Java System.out.print 似乎略有重叠

标签 java java.util.scanner

我今天一直在做这件事;它是 Java 代码,因此您可以:输入学生数量,输入他们的名字、姓氏、百分比,然后打印出该百分比是多少(通过/优秀)等。

但由于某种原因,仅对于第一个条目,名字和姓氏提示同时出现,因此名字会被姓氏覆盖,甚至无法输入第一个学生的名字。

你能找出问题所在吗?谢谢。

//imports scanner class
import java.util.Scanner;

public class Grades { 

    public static void main(String[] args) { 

        // new scanner called sc
        Scanner sc = new Scanner(System.in); 

        // start off by asking:
        System.out.print("How many students? ");
        // typed in number = number of students
        int count = sc.nextInt();
        // Line break 
        System.out.print("\n");

        // new strings for name/grade and int for percentage
        String fname[] = new String[count]; 
        String lname[] = new String[count]; 
        int percent[]  = new int[count]; 

        System.out.println("A-Z for Name & 0-9 for Percentages Only\n"); 

        // if there's input - add the info in 
        for (int i=0; i<count;i++){ 

            // enter first name
            System.out.print("Enter first name: "); 
            fname[i] = sc.nextLine(); 

            // enter surname
            System.out.print("Enter last name: "); 
            lname[i] = sc.nextLine(); 

            // enter percent
            System.out.print("Enter percentage: "); 
            percent[i] = Integer.parseInt(sc.nextLine());
        }

        // print out some text for information
        System.out.println("\nThe following information displays the students full name, percentage & their grade.");   

        // and print them on screen
        for (int i=0; i<count;i++) { 
            System.out.print("\nStudent - " + fname[i] + " " + lname[i] + " received " + percent[i] + "%."); 

            if (percent[i] >= 00 && percent[i] <= 39)   {System.out.print(" That's a Fail.");}
            if (percent[i] >= 40 && percent[i] <= 64)   {System.out.print(" That's a Pass.");}
            if (percent[i] >= 65 && percent[i] <= 84)   {System.out.print(" That's a Merit.");}
            if (percent[i] >= 85 && percent[i] <= 100)  {System.out.print(" That's a Distinction.");}
            if (percent[i] > 100)                       {System.out.print(" This percentage is too high? please try again.");}
            if (percent[i] < 0)                         {System.out.print(" This percentage is negative? please try again.");}

        }
    }    
}

最佳答案

问题是第一个扫描器的 nextInt() 条目。您可以通过输入数字并按 Enter 键来输入此数据。但 nextInt 只是读取整数,回车符仍然保留在输入中。当代码继续执行时,下一个扫描器 nextLine() 方法将立即读取该回车,因为它仍在输入中。在此过程中将名字留空。为了避免这种情况,您可以在循环之前调用 nextLine()。

您还可以通过始终使用 nextLine() 并将结果字符串解析为第一个问题中的整数来避免这种情况。

关于java - 我的 Java System.out.print 似乎略有重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26168393/

相关文章:

java - 出现 "No services have been found."错误

java - 列出 ConcurrentModificationException

java - 在 Java 中将 XPS 转换为 Word

java - inData 无法解析

java.lang.NoClassDefFoundError : Could not initialize class biz. 图盖.dao.HibernateUtil

java - 如何使用 Selenium 和 Java 自动访问附加用户提供的语言的 url?

java - 扫描仪方法打开和关闭两次

java - 尝试编写一个方法来检查 JAVA 中的用户输入,得到 NoSuchElementException

java - 如何在 while 循环中使用扫描仪

java - 无法在应用于 AJAX 的多个类上设置 CSS