1

这是问题:

编写一个 Java 程序,读入一系列正整数并写出所有小于 25 的整数与所有大于或等于 25 的整数之和的乘积。使用 0 作为标记值。

我能够理解如何让用户将输入添加到数组中。但是我找不到将代码与 if 语句集成以制作完整的工作程序来完成任务的方法。这是我目前的发现;

import java.util.*;
public class UserInput {
    public static void main(String[] args) {

        List<String> list = new ArrayList<String>();
        Scanner stdin = new Scanner(System.in);

        do {
            System.out.println("Current list is " + list);
            System.out.println("Add more? (y/n)");
            if (stdin.next().startsWith("y")) {
                System.out.println("Enter : ");
                list.add(stdin.next());
            } else {
                break;
            }
        } while (true);

        System.out.println("List is " + list);
        String[] arr = list.toArray(new String[0]);
        System.out.println("Array is " + Arrays.toString(arr));
    }
}

如何让数组读取输入并找到所有小于 25 的数字的乘积和大于 25 的所有数字的总和。在这之间,什么是标记值,知道它有多大用处?

解决方案: 经过数小时调整下面志愿者发布的答案后,我找到了适合我的解决方案。我特此发布它以防有人发现它有用;

import java.util.*;
  public class UserInput {
  public static void main(String[] args) {

List<Integer> list = new ArrayList<Integer>();
Scanner stdin = new Scanner(System.in);
do{
    System.out.println("Current list is : " + list);
    System.out.println("Enter number : ");
    int  entered = stdin.nextInt();
    if(entered == 0){
        int sum = 0,product = 1;
        for(Integer number : list){
            if(number<25){
                sum += number; 
            }else if(number >= 25){
                product *= number;
            }
        }
        System.out.println("sum : " + sum);
        System.out.println("product : " + product);
    }else{
        list.add(entered);
        int sum = 0,product = 1;
        for(Integer number : list){
            if(number<25){
                sum += number; 
            }else if(number >= 25){
                product *= number;
            }
        }
        System.out.println("sum : " + sum);
        System.out.println("product : " + product);
    }           
    System.out.println("Add more numbers to the list ? (Y/N) : ");

    } while(stdin.next().equalsIgnoreCase("y"));
}
}

感谢大家的帖子和评论!这真的很有帮助!:)

4

2 回答 2

0
import java.util.*;
public class UserInput {
 public static void main(String[] args) {

    List<Integer> list = new ArrayList<Integer>();
    Scanner stdin = new Scanner(System.in);
    do{
        System.out.println("Current list is : " + list);
        System.out.println("Enter number : ");
        int  entered = stdin.nextInt();
        if(entered != 0){
            list.add(entered);
        }else{
            int sum = 0,product = 1;
            for(Integer number : list){
                if(number<25){
                    sum += number; 
                }else if(number >= 25){
                    product *= number;
                }
            }
            System.out.println("sum : " + sum);
            System.out.println("product : " + product);
            break;
        }           
        System.out.println("Add more numbers to the list ? (Y/N) : ");
    }while(stdin.next().equalsIgnoreCase("y"));
 }
}
于 2013-05-05T18:25:09.630 回答
0
Class Main {

    public static void main(String[] args) {

        List<Integer> numsGe25 = new ArrayList<>();  // Store numbers greather than or equal to 25 

        List<Integer> numsLt25 = new ArrayList<>();  // Store numbers less than 25


        Scanner in = new Scanner(System.in);
        int num = -1;
        // Reads a number from the standard input till the 0 value is inserted. 0 is the sentinel
        while ((num = in.nextInt()) != 0) {  
            if (num >= 25) {
                numsGe25.add(num)   
            } else {
                numsLt25.add(num);
            }
        }
        System.out.println("sum of numbers >= 25: " + sum(numsGe25));
        System.out.println("product of numbers < 25: " + product(numsLt25));
    }

    private static int sum(List<Integer> numbers) {
        int result = 0;
        for (Integer num : numbers) {
            result += num;
        }
        return result; 
    }

    private static int product(List<Integer> numbers) {
        int result = 1;
        for (Integer num : numbers) {
            result *= num;
        }
        return result;
    }
}
于 2013-05-05T16:53:36.457 回答