我想弄清楚如何找到原始(無空格)字符串文本和disemvoweled(無空格)字符串文本之間的百分比差異。我試圖通過使用公式((newAmount-reducedAmount)/ reducedAmount)來做到這一點,但我沒有運氣,並以零結束,如下所示。Java:查找百分比差
謝謝!
我的代碼:
import java.util.Scanner;
public class Prog5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner console = new Scanner(System.in);
System.out.println("Welcome to the disemvoweling utility!"); // Initially typed "disemboweling" xD
System.out.print("Enter text to be disemvoweled: ");
String inLine = console.nextLine();
String vowels= inLine.replaceAll("[AEIOUaeiou]", ""); // RegEx for vowel control
System.out.println("Your disemvoweled text is: " + vowels); // Prints disemvoweled text
// Used to count all characters without counting white space(s)
int reducedAmount = 0;
for (int i = 0, length = inLine.length(); i < length; i++) {
if (inLine.charAt(i) != ' ') {
reducedAmount++;
}
}
// newAmount is the number of characters on the disemvoweled text without counting white space(s)
int newAmount = 0;
for (int i = 0, length = vowels.length(); i < length; i++) {
if (vowels.charAt(i) != ' ') {
newAmount++;
}
}
int reductionRate = ((newAmount - reducedAmount)/reducedAmount); // Percentage of character reduction
System.out.print("Reduced from " + reducedAmount + " to " + newAmount + ". Reduction rate is " + reductionRate + "%");
}
}
我的輸出:(測試字符串是不帶引號: 「測試請」)
Welcome to the disemvoweling utility!
Enter text to be disemvoweled: Testing please
Your disemvoweled text is: Tstng pls
Reduced from 13 to 8. Reduction rate is 0%
因爲你使用'int',請改變'INT reductionRate =((newAmount - reducedAmount)/ reducedAmount); 'to'dobule reductionRate =((double)(newAmount - reducedAmount)/ reducedAmount); ' – BlackMamba