Java黑皮书课后题第4章:*4.10(猜测生日)改写程序清单4-3,提示用户输入字符Y代表“是”N代表“否”,代替之前输入1表示“是”,0表示“否
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第4章:*4.10(猜测生日)改写程序清单4-3,提示用户输入字符Y代表“是”N代表“否”,代替之前输入1表示“是”,0表示“否
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*4.10(猜測生日)改寫程序清單4-3,提示用戶輸入字符Y代表“是”N代表“否”,代替之前輸入1表示“是”,0表示“否
- 題目
- 題目描述
- 程序清單4-3
- 本題代碼
題目
題目描述
*4.10(猜測生日)改寫程序清單4-3,提示用戶輸入字符Y代表“是”N代表“否”,代替之前輸入1表示“是”,0表示“否
程序清單4-3
import java.util.Scanner;public class qingdan {public static void main(String[] args) {String set1 =" 1 3 5 7\n" +" 9 11 13 15\n" +"17 19 21 23\n" +"25 27 29 31";String set2 =" 2 3 6 7\n" +"10 11 14 15\n" +"18 19 22 23\n" +"26 27 30 31";String set3 =" 4 5 6 7\n" +"12 13 14 15\n" +"20 21 22 23\n" +"28 29 30 31";String set4 =" 8 9 10 11\n" +"12 13 14 15\n" +"24 25 26 27\n" +"28 29 30 31";String set5 ="16 17 18 19\n" +"20 21 22 23\n" +"24 25 26 27\n" +"28 29 30 31";int day = 0;// Create a ScannerScanner input = new Scanner(System.in);;// Prompt the user to answer questionsSystem.out.println("Is your birthday in Set1?\n");System.out.println(set1);System.out.println("\nEnter 0 for No and 1 for Yes:");int answer = input.nextInt();if(answer == 1)day += 1;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set2?\n");System.out.println(set2);System.out.println("\nEnter 0 for No and 1 for Yes:");answer = input.nextInt();if(answer == 1)day += 2;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set3?\n");System.out.println(set3);System.out.println("\nEnter 0 for No and 1 for Yes: ");answer = input.nextInt();if(answer == 1)day += 4;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set3?\n");System.out.println(set4);System.out.println("\nEnter 0 for No and 1 for Yes: ");answer = input.nextInt();if(answer == 1)day += 8;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set3?\n");System.out.println(set5);System.out.println("\nEnter 0 for No and 1 for Yes: ");answer = input.nextInt();if(answer == 1)day += 16;System.out.println("\nYour birthday is " + day + "!");} }本題代碼
import java.util.Scanner;public class Test4_10 {public static void main(String[] args) {String set1 =" 1 3 5 7\n" +" 9 11 13 15\n" +"17 19 21 23\n" +"25 27 29 31";String set2 =" 2 3 6 7\n" +"10 11 14 15\n" +"18 19 22 23\n" +"26 27 30 31";String set3 =" 4 5 6 7\n" +"12 13 14 15\n" +"20 21 22 23\n" +"28 29 30 31";String set4 =" 8 9 10 11\n" +"12 13 14 15\n" +"24 25 26 27\n" +"28 29 30 31";String set5 ="16 17 18 19\n" +"20 21 22 23\n" +"24 25 26 27\n" +"28 29 30 31";int day = 0;// Create a ScannerScanner input = new Scanner(System.in);;// Prompt the user to answer questionsSystem.out.println("Is your birthday in Set1?\n");System.out.println(set1);System.out.println("\nEnter N for No and Y for Yes:");String str = input.nextLine();char answer = str.charAt(0);if(answer == 'Y')day += 1;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set2?\n");System.out.println(set2);System.out.println("\nEnter 0 for No and 1 for Yes:");str = input.nextLine();answer = str.charAt(0);if(answer == 'Y')day += 2;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set3?\n");System.out.println(set3);System.out.println("\nEnter 0 for No and 1 for Yes: ");str = input.nextLine();answer = str.charAt(0);if(answer == 'Y')day += 4;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set3?\n");System.out.println(set4);System.out.println("\nEnter 0 for No and 1 for Yes: ");str = input.nextLine();answer = str.charAt(0);if(answer == 'Y')day += 8;// Prompt the user to answer questionsSystem.out.println("\nIs your birthday in Set3?\n");System.out.println(set5);System.out.println("\nEnter 0 for No and 1 for Yes: ");str = input.nextLine();answer = str.charAt(0);if(answer == 'Y')day += 16;System.out.println("\nYour birthday is " + day + "!");} }總結
以上是生活随笔為你收集整理的Java黑皮书课后题第4章:*4.10(猜测生日)改写程序清单4-3,提示用户输入字符Y代表“是”N代表“否”,代替之前输入1表示“是”,0表示“否的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第4章:*4.9(给
- 下一篇: Java黑皮书课后题第4章:*4.11(