簡體   English   中英

Java中的算術異常,如何處理?

[英]Arithmetic Exception in Java, how to deal with it?

我正在編寫一個程序,該程序計算用戶輸入的字符串中字母的出現頻率,並且最近遇到了“算術異常”錯誤。

即使我知道這是因為某物被零除,我也無法弄清楚是什么原因。

這是我的代碼:

package day1.examples;

import java.util.Scanner;

public class rl_frequency_count {

    public static int input;

    public static void main(String[] args) {

        System.out
                .println("Please enter some text that you would like to work out the occurence for.");
        System.out
                .println("However, do remember that any other characters outside of the alphabet will NOT be counted.");

        Scanner stringUser = new Scanner(System.in);
        String input = stringUser.nextLine();
        input = input.replaceAll("\\s+", "");
        input = input.toLowerCase();

        // counting occurrence of character with loop
        int i;
        int charCountA = 0;
        int charCountB = 0;
        int charCountC = 0;
        int charCountD = 0;
        int charCountE = 0;
        int charCountF = 0;
        int charCountG = 0;
        int charCountH = 0;
        int charCountI = 0;
        int charCountJ = 0;
        int charCountK = 0;
        int charCountL = 0;
        int charCountM = 0;
        int charCountN = 0;
        int charCountO = 0;

        for (i = 0; i < input.length(); i++) {
            if (input.charAt(i) == 'a') {
                charCountA++;
                getOccurence(charCountA, "A");
            }
        }
        for (i = 0; i < input.length(); i++) {
            if (input.charAt(i) == 'b') {
                charCountB++;
                getOccurence(charCountB, "B");
            }
        }
        for (i = 0; i < input.length(); i++) {
            if (input.charAt(i) == 'c') {
                charCountC++;
                getOccurence(charCountC, "C");
            }
        }
        for (i = 0; i < input.length(); i++) {
            if (input.charAt(i) == 'm') {
                charCountM++;
                getOccurence(charCountM, "M");
            }
        }
    }

    // method for the occurrence
    public static void getOccurence(int number, String letter) {
        double occ = number / input * 10; //
        System.out.println();
        System.out.println("Number of " + letter + "'s - " + number);
        System.out.println("Occurence of " + letter + " - " + occ + "%");
    }
}

我知道目前我只有ABC和M,但稍后會繼續工作。

這是我第一次在這里發布文章,但我仍然對Java還是陌生的,因此,任何幫助都將不勝感激!

我運行它,並說第67行。這是總數:

public static void getOccurence(int number,String letter){
    double occ = number / input *10; //
    System.out.println();
    System.out.println("Number of "+ letter +"'s - "+ number);
    System.out.println("Occurence of "+ letter +" - "+ occ + "%");
}

修理:

  double occ = (number > 0) ? number/input * 10 : 0;

如果數字設置為0,則將occ設置為0。祝您好運。 希望這可以幫助。

導致錯誤的代碼行在您的方法中:

public static void getOccurence(int number,String letter){ 
    double occ = number / input *10; // <------ERROR FROM HERE (input is always 0)
    System.out.println();
    System.out.println("Number of "+ letter +"'s - "+ number);
    System.out.println("Occurence of "+ letter +" - "+ occ + "%");       
  }

input變量在您的類中在這里聲明:

Line 6: public static int input;

由於您沒有初始化它,也沒有在代碼中更改值,因此在整個程序中, input的值始終為0。 (未初始化的int變量的默認值為0)

由於它始終為0,因此您總是將數字除以0。

double occ = number / 0*10;

暫無
暫無

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

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