1.請設計一個Java程式,要求使用者輸入最多6個整數 - 若輸入值為字母Q或q,直接將已輸入資料排序並輸出結果 - 若輸入值為非數字、零或小於零的數字,則忽略要求再次輸入 請將使用者輸入的所有數字,由小到大排序並於畫面上顯示。
2.請設計一個Java程式,請使用者輸入一個數字(1~5)後,依據輸入的數字數量進行字元排列(A-E順序),並顯示所有排序結果,例如,若輸入數字3,則將A、B、C進行排列,顯示ABC、ACB、BAC、BCA、CAB、CBA共六種結果。
//第一題
public static void main(String[] args) {
        System.out.println("輸入數字");
        List<Integer> integerList = new ArrayList<>();
        Scanner inputScanner = new Scanner(System.in);
        while (integerList.size()<6){
            String input = inputScanner.next();
            if ("q".equalsIgnoreCase(input)){
                break;
            }
            try {
                Integer inputInt = Integer.parseInt(input);
                if (inputInt>0){
                    integerList.add(inputInt);
                    continue;
                }
            }catch (Exception e){}
            System.out.println("輸入無效");
        }
        integerList.stream().sorted().forEach(System.out::print);
    }
//第二題
public static void main(String[] args)  {
            List<String> array = Arrays.asList("A","B","C","D","E");
            System.out.println("輸入數字");
            Integer subInt = new Scanner(System.in).nextInt();
            array=array.subList(0,subInt);
            listAll(array, "",subInt);
        }
        public static void listAll(List candidate, String prefix, Integer subInt) {
            if(prefix.length()==subInt){
                System.out.println(prefix);
            }
            for(int i=0;i<candidate.size();i++) {
                List tmp = new LinkedList(candidate);
                listAll(tmp, prefix + tmp.remove(i), subInt);
            }
        }
同意作業自己做比較好+1
不然直接貼出來你也看不懂
經驗值也是給回答的人
//第一題
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		String str;
		boolean bool;
		int i;
		System.out.println("輸入最多6個整數");
		System.out.println("請輸入整數");
		ArrayList<Integer> integerList = new ArrayList<>();
		while (integerList.size() < 6) {
			Scanner sc = new Scanner(System.in);
			str = sc.next();
			if ("q".equalsIgnoreCase(str)) {
				integerList.stream().sorted().forEach(System.out::print);
				break;
			}
			bool = str.matches("[+-]?\\d*(\\.\\d+)?");
			if (!bool) {
				System.out.println("非數字、零或小於零的數字");
				System.out.println("再次輸入");
			}
			if (bool) {
				i = Integer.parseInt(str);
				integerList.add(i);
			}
		}
		integerList.stream().sorted().forEach(System.out::print);
	}}