Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户将Java输入到列表并理解_Java_User Input - Fatal编程技术网

用户将Java输入到列表并理解

用户将Java输入到列表并理解,java,user-input,Java,User Input,如何提示用户输入数字?我想计算百分比,所以我希望用户继续输入整数。如果用户在没有输入任何内容的情况下按enter键,则计算开始 这是示例输出 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

如何提示用户输入数字?我想计算百分比,所以我希望用户继续输入整数。如果用户在没有输入任何内容的情况下按enter键,则计算开始

这是示例输出

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) + "%");


        }
    }
}
package basic.functions;
导入java.util.*;
导入java.text.DecimalFormat;
公共类百分比{
公共静态void main(字符串[]args){
扫描仪阅读器=新扫描仪(System.in);
reader.useDelimiter(System.getProperty(“line.separator”);
列表=新的ArrayList();
System.out.println(“输入整数:”);
做{
试一试{
int n=reader.nextInt();
list.add(Integer.valueOf(n));
}捕获(输入不匹配异常){
System.out.println(“不是整数,请重试”);
}
}
//当用户按enter键时为空
while(reader.hasnetint());
reader.close();
//对列表中的所有整数元素求和
整数和=0;
for(int x:list)
总和+=x;
//计算作为总和比例输入的每个整数的百分比
//对于列表中的所有元素,除以总和,然后乘以时间100,得到百分比
System.out.println(“数字和百分比:”);
如果(总和=0){
System.out.println(“不可能除以0”);
}
否则{
for(int x:list){
System.out.println(x++(x*100/和)+“%”);
}
System.out.println(总和+“”+((总和*100)/总和+“%”);
}
}
}

假设您已经有了统计部分的方法:

您需要一个扫描器、一个整数列表,并将扫描器的设置为独立于平台的

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");
List List=new ArrayList();
Scanner myScanner=新扫描仪(System.in);
useDelimiter(System.getProperty(“line.separator”);
System.out.println(“给出一些int…”);
while(myScanner.hasNextInt()){
add(myScanner.nextInt());
}
myScanner.close();
System.out.println(“正在计算”);
多马斯(名单);
系统输出打印项次(“完成”);

您需要包括
reader.useDelimiter(System.getProperty(“line.separator”)到您的
扫描仪
对象,如另一个答案中所建议的

另外请注意,您必须使用
reader.close()关闭
扫描仪

您还需要
List List=new ArrayList()
以存储整数以供以后处理

最后,
DecimalFormat df=newdecimalformat(#.0”)将输出四舍五入到小数点后一位

编辑:

现在您也需要捕获不正确的输入,您不再需要
reader.useDelimiter(System.getProperty(“line.separator”)所以我删除了这一行

基本上,您需要将
输入
作为
字符串
读取,然后检查用户是否按了enter键(给出
isEmpty()
字符串
)或输入了无效输入(被
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) + "%");
        }
    }
}
package basic.functions;
导入java.util.*;
导入java.io.IOException;
导入java.text.DecimalFormat;
公共类百分比{
公共静态void main(字符串[]args)引发IOException{
扫描仪阅读器=新扫描仪(System.in);
列表=新的ArrayList();
System.out.println(“输入整数:”);
while(reader.hasNextLine())
{
字符串输入=reader.nextLine();
if(input.isEmpty())
{
//当用户按enter键时为空
打破
}
其他的
{
尝试
{        
list.add(Integer.valueOf(input));
}
捕获(NumberFormatException异常)
{
System.out.println(“不是整数,请重试”);
}
}
}
reader.close();
//对列表中的所有整数元素求和
整数和=0;
for(int x:list)
总和+=x;
//计算作为总和比例输入的每个整数的百分比
//对于列表中的所有元素,除以总和,然后乘以时间100,得到百分比
System.out.println(“数字和百分比:”);
如果(总和=0){
System.out.println(“不可能除以0”);
}
否则{
for(int x:list){
System.out.println(x++(x*100/和)+“%”);
}
System.out.println(总和+“”+((总和*100)/总和+“%”);
}
}
}

恭喜,您已经向用户询问了1个整数。现在你只需要再次询问它,使用循环,并找到一种方法,使用特定的条件或输入退出循环。好主意,我没有使用足够的扫描仪,但我从来没有想过这种循环!我会毫不怀疑地记得第一次成功了,但现在我被困在如何理解清单上的内容。我试着遍历它,但是有很多错误。我更新了代码,你能检查一下吗?请看:)退出while loopoh后,你需要处理列表,这个读卡器。hasNextInt()因为它是空的,带有“()”,这意味着关闭读卡器并停止扫描循环,对吗?@Kutam它检查流中的下一项是否为
int
,并保持循环运行。一旦它找不到
int
它就会返回
false
,循环就会中断。是的,这就是我的意思。哦,还有一件事