这个问题在这里已经有答案了:
如果用户为scanner的next()输入两个单词,则第二个单词将被传输到nextint()方法,该方法随后会导致inputmismatchexception(4个答案)
扫描仪在使用next()或nextfoo()后跳过nextline()(21个答案)
23天前关门了。
当我试图输入一个类别名称时,如果我没有在要添加到变量category的字符串中添加空格,它就会工作。我试着扫描nextline(),它只是跳过了所有的类别输入。让我知道你的想法。谢谢
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
//*********************************************************************
// Enter the amount of grading categories used in grading
// and create an array to store grading percentages
//*********************************************************************
System.out.println("Enter how many grade percentage "
+ "caterogies there are:");
int numCategories = scan.nextInt();
double [] gradePercentages = new double [numCategories];
double [] finalGrades = new double [numCategories];
for (int i=0;i<numCategories;i++)
{
//**********************************************************************
// Enter the data for category 1
//**********************************************************************
System.out.println("Enter the category name:");
**String category = scan.next();**
System.out.println("Enter category percentage:");
gradePercentages[i] = scan.nextInt();
//**
// - Compiler:
//**
//Enter how many grade percentage caterogies there are:
//5
//Enter the category name:
//Section Quizzes
//Enter category percentage:
//Exception in thread "main" java.util.InputMismatchException
//at java.base/java.util.Scanner.throwFor(Scanner.java:939)
//at java.base/java.util.Scanner.next(Scanner.java:1594)
//at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
//at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
//at GradeCalculator.main(GradeCalculator.java:30)
暂无答案!
目前还没有任何答案,快来回答吧!