明解JAVA 第三章答案
练习3-1
package candle1220; import java.util.Scanner; public class Nightwatch { public static void main(String[] args){ Scanner stdIn=new Scanner(System.in); System.out.print("整数值"); int n=stdIn.nextInt(); if(n>0) System.out.println("其绝对值是"+(n)); else System.out.println("其绝对值是"+(-n)); } }
练习3-2
package candle1220; import java.util.Scanner; public class Nightwatch { public static void main(String[] args){ Scanner stdIn=new Scanner(System.in); System.out.print("整数A:"); int a=stdIn.nextInt(); System.out.print("整数B:"); int b=stdIn.nextInt(); if(a%b==0) System.out.println(b+"是"+a+"的约数"); else System.out.println(b+"不是"+a+"的约数"); } }
练习3-3
同样可以运行
练习3-4
————恢复内容开始————
练习3-1
package candle1220; import java.util.Scanner; public class Nightwatch { public static void main(String[] args){ Scanner stdIn=new Scanner(System.in); System.out.print("整数值"); int n=stdIn.nextInt(); if(n>0) System.out.println("其绝对值是"+(n)); else System.out.println("其绝对值是"+(-n)); } }
练习3-2
package candle1220; import java.util.Scanner; public class Nightwatch { public static void main(String[] args){ Scanner stdIn=new Scanner(System.in); System.out.print("整数A:"); int a=stdIn.nextInt(); System.out.print("整数B:"); int b=stdIn.nextInt(); if(a%b==0) System.out.println(b+"是"+a+"的约数"); else System.out.println(b+"不是"+a+"的约数"); } }
练习3-3
同样可以运行
练习3-4
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner (System.in); System.out.print("a:"); int a=input.nextInt(); System.out.print("b:"); int b=input.nextInt(); if(a>b) System.out.print("a更大"); else if(a==b) System.out.print("一样大"); else System.out.print("b更大"); } }
练习3-5
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner (System.in); System.out.print("请输入一个正整数:"); int a=input.nextInt(); if(a<=0) System.out.print("请输入正整数"); else if(a%5==0) System.out.print("该值可以被5整除"); else System.out.print("该值不可以被5整除"); } }
练习3-6
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner (System.in); System.out.print("请输入一个正整数:"); int a=input.nextInt(); if(a<=0) System.out.print("请输入正整数"); else if(a%10==0) System.out.print("该值是10的倍数"); else System.out.print("该值不是10的倍数"); } }
练习3-7
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner (System.in); System.out.print("请输入一个正整数:"); int a=input.nextInt(); if(a<=0) System.out.print("请输入正整数"); else if(a%3==0) System.out.print("该值可以被3整除"); else if(a%3==1) System.out.print("该值除以3余1"); else System.out.print("该值除以3余2"); } }
练习3-8
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner (System.in); System.out.print("请输入分数:"); int a=input.nextInt(); if(a>=0&&a<60) {System.out.println("不及格");} else if(a>=60&&a<70) {System.out.println("及格");} else if(a>=70&&a<80) {System.out.println("良");} else if(a>=80&&a<=100) {System.out.println("优");} else {System.out.println("请输入合理的分数");} } }
练习3-9
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner (System.in); System.out.print("a=:"); int a=input.nextInt(); System.out.print("b=:"); int b=input.nextInt(); System.out.print(a>b?a:b); } }
练习3-10
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input =new Scanner (System.in); System.out.print("a="); int a=input.nextInt(); System.out.print("b="); int b=input.nextInt(); if(a>=b) System.out.print(a-b); else System.out.print(b-a); } }
练习3-11
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input =new Scanner (System.in); System.out.print("a="); int a=input.nextInt(); System.out.print("b="); int b=input.nextInt(); int c=a-b; if(c>10||c<-10) System.out.print("它们的差值大于等于11"); else System.out.print("它们的差值小于等于10"); } }
练习3-12
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.print("a="); int a =input.nextInt(); System.out.print("b="); int b =input.nextInt(); System.out.print("c="); int c =input.nextInt(); int min1=a<b?a:b; int min=min1<c?min1:c; System.out.print(min); } }
练习3-13
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner (System.in); System.out.print("a="); int a =input.nextInt(); System.out.print("b="); int b =input.nextInt(); System.out.print("c="); int c =input.nextInt(); int mid1=a<b?b:a; int mid=mid1<c?mid1:c; System.out.print(mid); } }
练习3-14
// 计算两个整数值中较小的值和较大的值 import java.util.Scanner; class New { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.print("整数a:"); int a = stdIn.nextInt(); System.out.print("整数b:"); int b = stdIn.nextInt(); if(a==b){ System.out.print("两个整数相等"); } else{ int min, max; // 较小的值/较大的值 if (a < b) { // 如果a小于b min = a; max = b; } else { // 否则 min = b; max = a; } System.out.println("较小的值是" + min + "。"); System.out.println("较大的值是" + max + "。"); } } }
练习3-15
// 计算两个整数值中较小的值和较大的值 import java.util.Scanner; class New { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.print("整数a:"); int a = stdIn.nextInt(); System.out.print("整数b:"); int b = stdIn.nextInt(); if(a==b){ System.out.print("两个整数相等"); } else{ int min, max; // 较小的值/较大的值 if (a < b) { // 如果a小于b min = a; max = b; } else { // 否则 min = b; max = a; } System.out.print(max+" "); System.out.println(min); } } }
练习3-16
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.print("a="); int a =input.nextInt(); System.out.print("b="); int b =input.nextInt(); System.out.print("c="); int c =input.nextInt(); int min1=a<b?a:b; int min=min1<c?min1:c; int mid1=a<b?a:b; int mid=mid1>c?mid1:c; int max1=a>b?a:b; int max=max1>c?max1:c; System.out.print(min+" "); System.out.print(mid+" "); System.out.print(max); } }
练习3-17
import java.util.Scanner; import java.util.Random; public class New{ public static void main(String[] args){ Scanner input=new Scanner(System.in); Random rand=new Random(); int a=rand.nextInt(3); switch(a){ case 0:System.out.println("石头");break; case 1:System.out.println("剪刀");break; case 2:System.out.println("布"); break; } } }
练习3-18
import java.util.Scanner; public class New{ public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.print("输入月份:"); int mouth=input.nextInt(); switch(mouth){ case 1: case 2: case 3:System.out.println("春"); case 4: case 5: case 6:System.out.println("夏"); case 7: case 8: case 9:System.out.println("秋"); case 10: case 11: case 12:System.out.println("冬"); } } }