iT邦幫忙

0

請問這兩題java程式要怎麼寫 謝謝

1.請設計一個Java程式,要求使用者輸入最多6個整數 - 若輸入值為字母Q或q,直接將已輸入資料排序並輸出結果 - 若輸入值為非數字、零或小於零的數字,則忽略要求再次輸入 請將使用者輸入的所有數字,由小到大排序並於畫面上顯示。

2.請設計一個Java程式,請使用者輸入一個數字(1~5)後,依據輸入的數字數量進行字元排列(A-E順序),並顯示所有排序結果,例如,若輸入數字3,則將A、B、C進行排列,顯示ABC、ACB、BAC、BCA、CAB、CBA共六種結果。

小魚 iT邦大師 1 級 ‧ 2021-06-06 17:31:21 檢舉
需要摳摳
skyksl066 iT邦新手 4 級 ‧ 2021-06-06 20:39:40 檢舉
感覺像作業啊,去作業網站看看懸賞要多少吧?
haward79 iT邦研究生 3 級 ‧ 2021-06-07 02:06:35 檢舉
像作業+1
程式自己動手寫才會進步啊!
寫到卡關再丟上來,相信大家人都很好。
這些都基本題啊!
該不會是遠距的期末功課吧?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
theRookie
iT邦新手 1 級 ‧ 2021-06-08 10:17:12
最佳解答
//第一題
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

不然直接貼出來你也看不懂
經驗值也是給回答的人

0
lihaoming1215
iT邦新手 5 級 ‧ 2021-06-13 16:24:10
//第一題
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);
	}}

我要發表回答

立即登入回答