簡體   English   中英

Java Integer.parseInt()中斷程序

[英]Java Integer.parseInt() breaks program

我在Java類中有一個問題要解決,它要求我編寫一個程序,該程序接收用戶的n個數字並輸出它們的平均值。 我知道我可以用一種更簡單的方法來完成此工作,只需要求用戶輸入開始時需要輸入的值的數量即可,但是我想創建該程序,因此用戶不必一定要知道開頭的值數。 因此,為此,我在for循環內創建了一個100長度的數組(希望可以覆蓋用戶需要輸入的數量)(在循環后將100長度的數組呈現為空,這樣程序就不會變得太占用內存)每次迭代都運行一個計數器槽。 一旦用戶進入停止位置,循環就結束了,並且輸入到100長度數組中的值將被轉移到一個計數大小的數組中。 這是代碼:

import java.util.*;
import java.lang.*;

public class main
{
   public static void main(String args[])
   {
      Scanner input = new Scanner(System.in);
      //Question 1
      System.out.println("Enter your numbers. (Enter 'Stop' when you're done)");
      int temp = 0;
      String uInput = "";
      char stopper;
      int count = 0;
      double total = 0;
      int a = 0;
      boolean inStop = true;
      for (boolean stop = false; stop != true;)
      {
         int array [] = new int [100];
         if (inStop == true)
         {
            System.out.println("point 5");
            System.out.print("Input: ");
            uInput = input.nextLine(); //reads user input
         }
         try //empty input repeater
         {
            System.out.println("point 1");
            try   //dealing with letters in string instead of numbers
            {
               System.out.println("point 2");

               temp = Integer.parseInt(uInput); //converts string to int
               array[count] = temp;
               count++;
               System.out.println(inStop);
               if (inStop == false) //executes when stop has been reached
               {
                  System.out.println("point 3");
                  int numberArray [] = new int [count]; //fills final array
                  for (int i = 0; i < count; i++)
                  {
                     numberArray[i] = array[i];
                  }
                  for (a = 0; a < numberArray.length; a++)
                  {
                     total = total + numberArray[a];
                  }
                  total = total / a;
                  stop = true; //ends parent loop
               }
            }
            catch (NumberFormatException e) //catches letters in string and checks for stop
            {
               System.out.println("point 4");
               stopper = uInput.charAt(0);
               stopper = Character.toUpperCase(stopper);
               if (stopper == 'S')
               {
                  inStop = false;

                  System.out.println("point 6");
               }
            }
         }
         catch (StringIndexOutOfBoundsException e)
         {
         }
      }
      System.out.println("The average of the values entered is: " + total + ".");
   }
}

問題是,正如您所看到的那樣,有許多帶編號的打印輸出(對我而言)指示該程序當前所在的位置。 除第3點外,其他所有程序運行正常。第3點由於某種原因未執行。 不管我做什么。 現在,問題出在第34行, temp = Integer.valueOf(uInput); //converts string to int temp = Integer.valueOf(uInput); //converts string to int如果我在該行之后直接插入打印函數,則該位置不會打印到屏幕上。 我相信該部分沒有語法或邏輯錯誤,我的講師也沒有,但是代碼仍然無法執行,並且程序隨后無限循環。 某一行中斷了temp或uInput,我們無法弄清楚是什么。 我已經使用與最初使用的編譯器不同的編譯器編譯並運行了代碼,甚至在命令提示符中嘗試了相同的結果(因此不是由IDE引起問題)。

我們可能錯過的任何見解將不勝感激。 謝謝。

ps:請不要敲我的講師,他沒有寫代碼,而且它也不容易閱讀。 如果我的解釋或他對我的程序的運行方式的解釋沒有錯誤,他可以很容易地知道問題出在哪里。

我認為您在確定問題時遇到問題的原因是由於您的代碼結構。

您已經將告知使用的邏輯與讀取輸入並進行計算的邏輯混合在一起。

如果您的主要方法僅處理通知用戶的問題,並依靠另一種方法來計算平均值,而另一種方法則是讀取用戶的輸入,則所有內容都將更易於閱讀,理解並看到您將“ stop”解析為一個int。

public static void main(String[] args) {
    System.out.println("instructions");
    int[] all = readUserInputs();
    double ave = calculateAverage(all);
    System.out.println("message " + ave);
}

private static double calculateAverage(int[] numbers) {
    // I will leave it to you to fill this out
    return yourValue;
}

private static String readUserInputs() {
    Scanner input;// as above
    int[] values; // is an array best? What about a List?
    for (int i = 0; ; i++) {
        String line = input.nextLine();
        if ("stop".equals(line) {
            break;
        }
        //try to parse and put into array/list
    }
    return values;
}

希望您會發現它更易於閱讀和使用,我為您留出了一些空白。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM