java - 使用 sc.nextLine 输入字符串时出错

标签 java

import java.util.Scanner;
public class GpaConverterTester
{
public static void main(String[] args)
{
    Scanner sc = new Scanner(System.in);
    GpaConverter g = new GpaConverter();

    System.out.println("How many classes are you taking? ");
    int classAmount = sc.nextInt();
    while(classAmount > 0)
    {
        System.out.println("Enter Grade: ");
        String grade = sc.nextLine();
        g.setGpaValue(grade);
        classAmount--;
    }
    System.out.println("Average: " + g.getAverage());

}
}

我的基本问题是它不允许我输入成绩字符串。这就是发生的事情...

输出: “你上几节课? 2 输入年级: 输入年级: ” 它不允许我输入成绩字符串。 谢谢您的帮助!

最佳答案

我明白了。

使用 sc.next() 而不是 line。

关于java - 使用 sc.nextLine 输入字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26540145/

相关文章:

java - 从 twitter4j 获取翻译数据

java - 如何为不同的环境读取不同的属性文件?

java - 保持 Jersey Client API 函数和 REST (Jersey API) Server 函数链接的 "proper"和正确方法是什么?

java - 编译版本高于Build tool

java - Tensorflow Java API 设置分类列的占位符

java - Android PorterDuff.Mode 错误 : PorterDuff cannot be resolved to a variable

java - Java中的平台库是什么

java - Node.js 和 WebSphere Portal 之间的单点登录

java - 当从键盘输入 10 个数字时,从 while 循环中打印最大的数字

java.lang.IllegalArgumentException : width and height must be > 0 in android 异常