Java零基础学习04(有C语言基础)
本章主要熟练使用Scanner输入数据,练习java常见数学函数、字母数字判别比较方法的使用。
文章目录
/*课后习题p135 4.2 几何:大圆距离*/
/*此题目的在熟练使用Math中三角函数和度与角度的转换*/
import java.util.Scanner;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter point 1 (latitude and longitude) in degrees:");
double x1=Math.toRadians(input.nextDouble());
double y1=Math.toRadians(input.nextDouble());
System.out.print("Enter point 2 (latitude and longitude) in degrees:");
double x2=Math.toRadians(input.nextDouble());
double y2=Math.toRadians(input.nextDouble());
double d;
d = 6371.01*Math.acos(Math.sin(x1)*Math.sin(x2)+Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2));
System.out.print("The distance between the two pionts is "+d+" km");
}
}
/*课后习题p136 4.8 给出ASCII码对应的字符*/
/*此题目的在ASCII码转换转换成对应字符:之前为此很头疼,一直找不到相应的转换公式,原理从C语言开始就提供了将ASCII数值转换成对应字符的方法
* 方法非常简单,只需以int型输入ASCII码,然后强制转换成char就行*/
/*不用担心需要字符型数字怎么办,如果需要字符型数字则在输入时就确定字符类型输入,如果是以int型输入数字,后再强制转换成char字符型,就默认转换成对应的字符,如果输入的数字不在ASCII码范围内,强制转换后会为空,而不是被强制转换成字符型*/
import java.util.Scanner;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an ASCII code: ");
int asc=input.nextInt();
if(asc<0||asc>128){
System.out.print("该数字无效,请输入正确的ASCII码! ");
}
else {
System.out.println("The character for ASCII code "+asc+" is "+(char)asc);/*将ASCII转换成对应字符的方式是强制转换成char*/
}
}
}
/*课后习题p136 4.9 给出字符Unicode码*/
/*先用String字符串类型接收
再调用charAt(0)方法获取String对象第一个字符,赋值给char类型对象
最后强转为int类型*/
/*在java中char不能直接接受输入的单字符(因为在输入结束后,还有一个回车符,虽然我们看不见,但是它确实存在,所以只能用string字符串,然后再用string中的charat()方法获取单个字符*/
import java.util.Scanner;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an character: ");
String str=input.next();
char ch=str.charAt(0);
System.out.println("The Unicode for the character "+ch+" is "+(int)ch);/*将ASCII转换成对应字符的方式是强制转换成char*/
}
}
/*课后习题p136 4.10 猜测生日*/
/*注意string字符串用双引号“”,字符char用单引号‘’*/
import java.util.Scanner;
public class Java_1 {
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\n";
String set3="4 5 6 7\n"+
"12 13 14 15\n"+
"20 21 22 23\n"+
"28 29 30 31\n";
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;
Scanner input=new Scanner(System.in);
System.out.println("Is your birthday in Set1?\n");
System.out.println(set1);
System.out.print("\nEnter Y for yes and N for No:");
String str1=input.next();
char ch1=str1.charAt(0);
if (ch1=='Y'){/*这里不能省事直接用str1==“Y”,因为在输入时str1其实还包括最后输入进行确定的一个回车符,所以str1的值并不等于“Y”*/
day+=1;
}
System.out.println("Is your birthday in Set2?\n");
System.out.println(set2);
System.out.print("\nEnter Y for yes and N for No:");
String str2=input.next();
char ch2=str2.charAt(0);
if (ch2=='Y'){
day+=2;
}
System.out.println("Is your birthday in Set3?\n");
System.out.println(set3);
System.out.print("\nEnter Y for yes and N for No:");
String str3=input.next();
char ch3=str3.charAt(0);
if (ch3=='Y'){
day+=4;
}
System.out.println("Is your birthday in Set4?\n");
System.out.println(set4);
System.out.print("\nEnter Y for yes and N for No:");
String str4=input.next();
char ch4=str4.charAt(0);
if (ch4=='Y'){
day+=8;
}
System.out.println("Is your birthday in Set5?\n");
System.out.println(set5);
System.out.print("\nEnter Y for yes and N for No:");
String str5=input.next();
char ch5=str5.charAt(0);
if (ch5=='Y'){
day+=16;
}
System.out.println("\nYour birthday is "+day+" !");
}
}
/*课后习题p136 4.11 十进制转十六进制*/
/*字母ASCII码:A-Z:65-90*/
/*这是常用的将十进制转换成十六进制的方法,如果考试不记得字母的ASCII码,可以使用switch罗列法输出对应字母*/
import java.util.Scanner;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a decimal value(0 to 15): ");
int number = input.nextInt();
if (number < 0 || number > 15) {
System.out.print(number + " is an invalid input");
} else {
if (number > 0 && number < 10) {
System.out.println("The hex value is " + number);
}
else {
number=number+55;
System.out.println("The hex value is " + (char)number);
}
}
}
}
/*课后习题p137 4.13 元音还是辅音?*/
/*此题目的是熟练使用字母判别和大小写转换方法*/
import java.util.Scanner;
import static java.lang.Character.isLetter;
import static java.lang.Character.toLowerCase;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter : ");
String str=input.next();
char ch=str.charAt(0);
if(isLetter(ch)){/*isLetter方法判断是否是字母*/
toLowerCase(ch);/*toLowerCase将字母转换成小写*/
if(ch=='a'||ch=='o'||ch=='I'||ch=='i'||ch=='u'||ch=='e'){
System.out.println(ch+" is a vowel");
}
else {
System.out.println(ch+" is a consonant");
}
}
else {
System.out.println(ch+" is an invalid input");
}
}
}
/*课后习题p138 4.21 检查SSN*/
/*此题目的是熟练使用数字判别方法*/
import java.util.Scanner;
import static java.lang.Character.*;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a SSN: ");
String str=input.nextLine();
boolean flag=false;/*标记*/
if (str.length()==11&&str.charAt(3)=='-'&&str.charAt(6)=='-'){
for(int i=0;i<3;i++){
isDigit(str.charAt(i));
for (int j=4;j<6;j++){
isDigit(str.charAt(j));
for (int k=7;k<11;k++){
isDigit(str.charAt(k)) ;
flag=true;
}
}
}
}
if(flag){
System.out.println(str+" is an valid social security number");
}
else {
System.out.println(str+" is an invalid social security number");
}
}
}
/*课后习题p138 4.22 检测子串*/
/*此题目的是熟练使用查找字符串子串的方法*/
import java.util.Scanner;
import static java.lang.Character.*;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter string s1: ");
String str1=input.nextLine();
System.out.print("Enter string s2: ");
String str2=input.nextLine();
if(str1.indexOf(str2)!=-1){/*indexOf()返回下标,没有匹配则返回-1*/
System.out.println(str2+" is a substring of "+str1);
}
else {
System.out.println(str2+" is not a substring of "+str1);
}
}
}
/*课后习题p139 4.24 对三个城市排序*/
/*此题目的是熟练使用sort()方法*/
import java.util.Arrays;
import java.util.Scanner;
import static java.lang.Character.*;
import static java.util.Arrays.sort;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] str=new String[3];
System.out.print("Enter the first city: ");
str[0]=input.next();
System.out.print("Enter the second city: ");
str[1]=input.next();
System.out.print("Enter the third city: ");
str[2]=input.next();
for (int j=0;j<3;j++) {
sort(str);/*sort()排序*/
}
System.out.println("The three citise in alphabetical order are "+str[0]+" "+str[1]+" "+str[2]);
}
}