Java:找出百分比差异
Java: Finding Percent Difference
我想弄清楚如何找到原始(无 space)文本字符串和去元音(无 space)文本字符串之间的百分比差异。我试图通过使用等式 ((newAmount-reducedAmount)/reducedAmount) 来做到这一点,但我没有运气,最终得到的值为零,如下所示。
谢谢!
我的代码:
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 + "%");
}
}
我的输出:(测试字符串没有引号:"Testing please")
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%
您的公式给出了一个介于零和一之间的值。
整数不能包含小数,因此它始终显示为零。
乘以 100 得到常规百分比值。
int reductionRate = 100*(newAmount - reducedAmount) / reducedAmount; // Percentage of character reduction
您在执行整数除法计算百分比差异时使用了整数数据类型。您需要在等式右侧键入变量之一以执行双除法,然后将它们存储在双精度中。这样做的原因是 java 整数类型不能容纳实数。
另外,将它乘以 100 得到百分比。
double reductionRate = 100 * ((newAmount - reducedAmount) / (double)reducedAmount);
如果你想要一个介于 0 和 1 之间的小数,那么
double reductionRate = ((newAmount - reducedAmount) / (double)reducedAmount);
我想弄清楚如何找到原始(无 space)文本字符串和去元音(无 space)文本字符串之间的百分比差异。我试图通过使用等式 ((newAmount-reducedAmount)/reducedAmount) 来做到这一点,但我没有运气,最终得到的值为零,如下所示。
谢谢!
我的代码:
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 + "%");
}
}
我的输出:(测试字符串没有引号:"Testing please")
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%
您的公式给出了一个介于零和一之间的值。
整数不能包含小数,因此它始终显示为零。
乘以 100 得到常规百分比值。
int reductionRate = 100*(newAmount - reducedAmount) / reducedAmount; // Percentage of character reduction
您在执行整数除法计算百分比差异时使用了整数数据类型。您需要在等式右侧键入变量之一以执行双除法,然后将它们存储在双精度中。这样做的原因是 java 整数类型不能容纳实数。 另外,将它乘以 100 得到百分比。
double reductionRate = 100 * ((newAmount - reducedAmount) / (double)reducedAmount);
如果你想要一个介于 0 和 1 之间的小数,那么
double reductionRate = ((newAmount - reducedAmount) / (double)reducedAmount);