如何提示用户输入号码?我想计算百分比,因此我希望用户继续输入整数。如果用户按输入而无需输入任何内容,那就是计算启动的位置
这是示例输出
Input Integer:
2
17
1
3
The numbers and percentage:
2 8.7%
17 73.9%
1 4.3%
3 13.0%
23 100.0%
用户完成输入数字后,我想对它们进行总结并计算每个数字的比例。
这就是我所做的
package basic.functions;
import java.util.*;
import java.text.DecimalFormat;
public class Percent {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
reader.useDelimiter(System.getProperty("line.separator"));
List<Integer> list = new ArrayList<>();
System.out.println("Enter Integer: ");
do {
try {
int n = reader.nextInt();
list.add(Integer.valueOf(n));
} catch (InputMismatchException exception) {
System.out.println("Not an integer, please try again");
}
}
//When user press enter empty
while (reader.hasNextInt());
reader.close();
//SUM all the integer elements in the list
int sum = 0;
for (int x : list)
sum += x;
//calculate the percentage of each integer that was entered as proportion of the sum
//for all elements in the list, divide by the SUM then time 100 to get the percentage
System.out.println("The numbers and percentage:");
if (sum == 0) {
System.out.println("Division by 0 not possible");
}
else {
for (int x : list) {
System.out.println(x + " " + ((x * 100 / sum) + "%"));
}
System.out.println(sum + " " + ((sum * 100) / sum) + "%");
}
}
}
假设您已经有统计部分的方法:
您需要一个扫描仪,整数列表,并将扫描仪的定界符设置为平台独立 line.separator
List<Integer> list = new ArrayList<>();
Scanner myScanner = new Scanner(System.in);
myScanner.useDelimiter(System.getProperty("line.separator"));
System.out.println("give some int...");
while (myScanner.hasNextInt()) {
list.add(myScanner.nextInt());
}
myScanner.close();
System.out.println("now calculating");
doMath(list);
System.out.println("done");
您也需要将reader.useDelimiter(System.getProperty("line.separator"));
包含在Scanner
对象中,如其他答案中所建议的。
另外,您必须使用reader.close();
Scanner
您还需要List<Integer> list = new ArrayList<Integer>();
来存储整数以进行以后处理。
最后,DecimalFormat df = new DecimalFormat("#.0");
是将输出舍入到一个小数位。
编辑:
现在您也需要捕获不正确的输入,您不再需要reader.useDelimiter(System.getProperty("line.separator"));
,所以我已删除了此行。
基本上,您需要将input
读为String
,然后检查用户是否按Enter(给出isEmpty()
String
(或输入无效输入(由NumberFormatException
捕获(。
这是执行此操作的代码:
package basic.functions;
import java.util.*;
import java.io.IOException;
import java.text.DecimalFormat;
public class Percent {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
System.out.println("Enter Integer: ");
while(reader.hasNextLine())
{
String input = reader.nextLine();
if(input.isEmpty())
{
//When user press enter empty
break;
}
else
{
try
{
list.add(Integer.valueOf(input));
}
catch (NumberFormatException exception)
{
System.out.println("Not an integer, please try again");
}
}
}
reader.close();
//SUM all the integer elements in the list
int sum = 0;
for (int x : list)
sum += x;
//calculate the percentage of each integer that was entered as proportion of the sum
//for all elements in the list, divide by the SUM then time 100 to get the percentage
System.out.println("The numbers and percentage:");
if (sum == 0) {
System.out.println("Division by 0 not possible");
}
else {
for (int x : list) {
System.out.println(x + " " + ((x * 100 / sum) + "%"));
}
System.out.println(sum + " " + ((sum * 100) / sum) + "%");
}
}
}