简单的Java错误,初学者CS学生

时间:2015-10-16 20:58:41

标签: java

我按z时的错误是:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at InputSum.main(InputSum.java:60)

这是我的计划:

import java.util.Scanner;

public class InputSum
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);

   //declarations
      int sum;
      int number;
      int letter;
      boolean cont = true;
      boolean mop = true;

   //outputs

      System.out.println("Input ONLY Positive Integers including zero to Add (end with -1): ");


   //variables
      sum = 0;
      String numbers = "";

   //logic

      while(cont)  // loop
      {    
         number = input.nextInt();
         if(number == -1)
            break;
         // create a break for -1

         else if(number > -1)
         {sum = sum + number; numbers = numbers + number + ", "; } // summation of the numbers and string 

         else 
            System.out.println("Invalid input. Enter a valid input");
            // for non positive integers the output is invalid
      }



    //outputs and loops

      System.out.println("Entered numbers: " + numbers);
      System.out.println("The sum: " + sum);
      System.out.println("Would you like to continue with new inputs?(enter z for yes and any number other than 50 for no)");
      letter = input.nextInt();
      while(cont)
      {
         if(letter == 50){
            System.out.println("Input ONLY Positive Integers including zero to Add (end with -1): ");

            while(cont)  // loop
            {    
               number = input.nextInt();
               if(number == -1)
                  break;
               // create a break for -1

               else if(number > -1)
               {sum = sum + number; numbers = numbers + number + ", "; } // summation of the numbers and string 

               else 
                  System.out.println("Invalid input. Enter a valid input");
            // for non positive integers the output is invalid
            }

         }  
         else 

            System.exit(0); 
      }   // output and print statement for the sum

   }     
}

我不知道如何修复它,所以当我点击z时,它表示是,并且不会给我这些错误。我该如何解决?

以下是该计划的说明:

  

练习#1

     

编写一个名为InputSum的程序,提示用户输入   正整数。程序应该接受整数直到   用户输入值-1(负一)。用户输入-1后,   程序应显示输入的数字,后跟其总和   如下所示。

     

您必须设计程序,以便询问用户是否需要   在每组条目之后继续新输入,结束程序   只有当他们没有用"是"。

回答问题时      

请注意,在以下示例运行中,-1不是输出的一部分   显示"输入的数字":

Input numbers to add (end with -1): 10  2  13  50  100  -1
Entered numbers: 10, 2, 13, 50, 100
The Sum: 175

Would you like to continue with new inputs? yes

Input numbers to add (end with -1): 1  2  3  4  5  6  7  8  9  10  -1
Entered numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
The Sum: 55

Would you like to continue with new inputs? yes

Input numbers to add (end with -1): 1  1  1  1  100  -1
Entered numbers: 1, 1, 1, 1, 100
The Sum: 104

Would you like to continue with new inputs? yes

Input numbers to add (end with -1): 0  0  0  0  0  100  -1
Entered numbers: 0, 0, 0, 0, 0, 100
The Sum: 100 

Would you like to continue with new inputs? no    Make sure the program validates each entered number before processing it as      
     

用户可以输入句子值以外的负数   -1。如果是负数不是-1,则打印输出"无效输入"并继续接受数字。腾出你的输出   如上所示。""

1 个答案:

答案 0 :(得分:0)

当您要求用户输入" z"时,您使用letter = input.nextInt();,而您想要使用letter = input.next()letter = input.nextLine() " Z"是一个字符串而不是整数。当然letter是一个整数,所以你不能在其中存储一个字符串,因此需要一个新的变量

相关问题