Java
1//降順にソートするプログラム 2import java.util.Arrays; 3import java.util.Comparator; 4import java.io.*; 5 6class ToDo{ 7 int number; 8 int month; 9 int day; 10 String item; 11 int priority; 12} 13 14class sort8 15{ 16 public static void main(String[] args) throws IOException 17 { 18 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 19 20 ToDo[] ToDoList = new ToDo[100]; //配列の宣言 21 22 for(int i = 0; i < 100; i++){ 23 ToDoList[i] = new ToDo(); 24 } 25 26 int num_items = 0; 27 28 while(true){ 29 System.out.println("予定を追加するなら1を入力。予定を月順で表示するなら2、優先度順で表示するなら3を入力。"); 30 31 String str = br.readLine(); 32 int num = Integer.parseInt(str); 33 34 if(num == 1){ 35 do{ 36 System.out.println("月を入力して下さい(1〜12)"); 37 38 String month = br.readLine(); 39 ToDoList[num_items].month = Integer.parseInt(month); 40 41 }while(13 <= ToDoList[num_items].month && ToDoList[num_items].month <= 0); 42 //↑1〜12以外が入力されたら再入力を求める 43 44 do{ 45 System.out.println("日を入力して下さい(1〜31)"); 46 47 String day = br.readLine(); 48 ToDoList[num_items].day = Integer.parseInt(day); 49 50 }while(32 <= ToDoList[num_items].day); 51 52 System.out.println("予定"); 53 54 String item = br.readLine(); 55 ToDoList[num_items].item = item; 56 57 58 do{ 59 System.out.println("優先度を入力して下さい(1〜5)"); 60 61 String priority = br.readLine(); 62 ToDoList[num_items].priority = Integer.parseInt(priority); 63 64 }while(6 <= ToDoList[num_items].priority); 65 66 num_items++; 67 } 68 else if(num == 2){ 69 System.out.println("予定です。(月昇順)"); 70 71 Arrays.sort(ToDoList , 0, num_items, new Comparator<ToDo>(){ 72 public int compare(ToDo todo1, ToDo todo2){ 73 if(todo1.month < todo2.month){ 74 return -1; 75 } 76 else if(todo1.month == todo2.month){ 77 if(todo1.day <= todo2.day){ 78 return -1; 79 } 80 else{ 81 return 1; 82 } 83 } 84 else{ 85 return 1; 86 } 87 }}); 88 89 for(int i = 0; i < num_items; i++){ 90 System.out.println(ToDoList[i].month + "月" + ToDoList[i].day + "日 " + ToDoList[i].item + "優先度:" + ToDoList[i].priority); // 昇順になる 91 } 92 } 93 else if(num == 3){ 94 System.out.println("予定です。(優先度昇順)"); 95 Arrays.sort(ToDoList , 0, num_items, (a, b)->a.priority - b.priority); 96 97 for(int i = 0; i < num_items; i++){ 98 System.out.println(ToDoList[i].month + "月" + ToDoList[i].day + "日 " + ToDoList[i].item + "優先度:" + ToDoList[i].priority); // 昇順になる 99 } 100 } 101 } 102 } 103}
上記のプログラムで月を入力させる時に1〜12以外の数字を入力した時に再入力を求める様にしたいのですが、うまくいきません。
改善策を教えて下さい。
よろしくお願い致します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/21 13:21
2017/12/21 13:23
2017/12/21 13:24