4.14
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter a letter grade: ");
Scanner input = new Scanner(System.in);
char a = input.nextLine().charAt(0);
String[] bins = {"0","1","2","3","4"};
if(a=='A')
System.out.printf("The numeric value for grade %c is %s", a,bins[4]);
else if(a=='B')
System.out.printf("The numeric value for grade %c is %s", a,bins[3]);
else if(a=='C')
System.out.printf("The numeric value for grade %c is %s", a,bins[2]);
else if(a=='D')
System.out.printf("The numeric value for grade %c is %s", a,bins[1]);
else if(a=='F')
System.out.printf("The numeric value for grade %c is %s", a,bins[0]);
else
System.out.printf("%c is an invalid grade",a);
}
}
4.15
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter a letter: ");
Scanner input = new Scanner(System.in);
String[] number={"2","3","4","5","6","7","8","9"};
char l = input.nextLine().charAt(0);
if(l=='a'||l=='b'||l=='c'||l=='A'||l=='B'||l=='C')
System.out.printf("The corresponding number is %s", number[0]);
else if(l=='d'||l=='e'||l=='f'||l=='D'||l=='E'||l=='F')
System.out.printf("The corresponding number is %s", number[1]);
else if(l=='g'||l=='h'||l=='i'||l=='G'||l=='H'||l=='I')
System.out.printf("The corresponding number is %s", number[2]);
else if(l=='j'||l=='k'||l=='l'||l=='J'||l=='K'||l=='L')
System.out.printf("The corresponding number is %s", number[3]);
else if(l=='m'||l=='n'||l=='o'||l=='m'||l=='n'||l=='o')
System.out.printf("The corresponding number is %s", number[4]);
else if(l=='p'||l=='q'||l=='r'||l=='s'||l=='P'||l=='Q'||l=='R'||l=='S')
System.out.printf("The corresponding number is %s", number[5]);
else if(l=='t'||l=='u'||l=='v'||l=='T'||l=='U'||l=='V')
System.out.printf("The corresponding number is %s", number[6]);
else if(l=='w'||l=='x'||l=='y'||l=='z'||l=='W'||l=='X'||l=='Y'||l=='Z')
System.out.printf("The corresponding number is %s", number[7]);
else
System.out.println(l+"is an invalid input");;
}
}
4.16
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
int l = (int)(Math.random()*26);
char letter=(char)(Math.random()*26+'A');
System.out.print("The random number is "+letter);
}
}
4.17
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = input.nextInt();
Scanner i = new Scanner(System.in);
System.out.print("Enter a month: ");
String month = i.nextLine();
boolean isLeapYear = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));
switch(month){
case "Jan":
case "Mar":
case "May":
case "Jul":
case "Aug":
case "Oct":
case "Dec":
System.out.println(month+" "+year+" has 31 day");
break;
case "Apr":
case "Jun":
case "Sep":
case "Nov":
System.out.println(month+" "+year+" has 30 days");
break;
case "Feb":
if(isLeapYear){
System.out.println(month+" "+year+" has 29 days");
}else{
System.out.println(month+" "+year+" has 28 days");
}
default:
System.out.printf("%s is not a correct month name", month);
}
}
}
4.18
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter two characters: ");
String s = input.nextLine();
char s1 = s.charAt(0);
char s2 = s.charAt(1);
switch(s1)
{
case 'M':
System.out.print("Mathematics ");
switch(s2){
case '1':
System.out.println("Freshman");
break;
case '2':
System.out.println("Sophomore");
break;
case '3':
System.out.println("Junior");
break;
case '4':
System.out.println("Senior");
break;
default:
;
}
break;
case 'C':
System.out.print("Computer science ");
switch(s2){
case '1':
System.out.println("Freshman");
break;
case '2':
System.out.println("Sophomore");
break;
case '3':
System.out.println("Junior");
break;
case '4':
System.out.println("Senior");
break;
default:
;
}
break;
case 'I':
System.out.print("information technology ");
switch(s2){
case '1':
System.out.println("Freshman");
break;
case '2':
System.out.println("Sophomore");
break;
case '3':
System.out.println("Junior");
break;
case '4':
System.out.println("Senior");
break;
default:
;
}
break;
default:
System.out.print("Invalid input");
}
}
}
4.19
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the first 9 digits of an ISBN as String: ");
String a = input.next();
int sum=0;
for(int i=1;i<=9;i++)
sum+=(a.charAt(i-1)-'0')*i;
int tail = sum%11;
if(tail==10)
a=a+"X";
else
a=a+tail;
System.out.println("The ISBN-10 number is "+a);
}
}
4.20
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String s1 = input.nextLine();
System.out.println("长度为: " + s1.length()+" ; "+"第一个字符为: "+s1.charAt(0));
}
}
4.21
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter a SSN: ");
Scanner input = new Scanner(System.in);
String a = input.next();
boolean valid = true;
if(a.length()!=11)
valid=false;
else
{
for(int i=0;i<=2;i++)
valid=valid&&(a.charAt(i)>='0'&&a.charAt(i)<='9');
valid=valid&&(a.charAt(3)=='-');
for(int i=4;i<=5;i++)
valid=valid&&(a.charAt(i)>='0'&&a.charAt(i)<='9');
valid=valid&&(a.charAt(6)=='-');
for(int i=7;i<=10;i++)
valid=valid&&(a.charAt(i)>='0'&&a.charAt(i)<='9');
}
if(valid)
System.out.println(a+" is a valid SSN");
else
System.out.println(a+" is an invalid SSN");
}
}
4.22
package demo;
import java.util.Scanner;
public class demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter string s1: ");
String s1 = input.nextLine();
System.out.print("Enter string s2: ");
String s2 = input.nextLine();
if(s1.indexOf(s2)>=0){
System.out.printf("%s is a substring of %s", s2,s1);
}else
System.out.printf("%s is not a substring of %s", s2,s1);
}
}
4.23
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter employee's name: ");
String name = input.next();
System.out.print("Enter number of hours worked in a week: ");
double hour = input.nextDouble();
System.out.print("Enter hourly pay rate: ");
double payRate = input.nextDouble();
System.out.print("Enter federal tax withholding rate: ");
double ftax = input.nextDouble();
System.out.print("Enter state tax withholding rate: ");
double stax = input.nextDouble();
System.out.printf("Employee name: %s\n",name);
System.out.printf("Hours worked: %.2f\nPay Rate: $%.2f\nGross Pay: $%.2f\n",hour,payRate,hour*payRate);
System.out.printf("Deductions:\n Federal Withholding (%f%%): $%.2f\n State Withholding (%f%%): $%.2f\n Total Deduction: $%.2f\n",ftax*100,ftax*hour*payRate,stax*100,stax*hour*payRate,ftax*hour*payRate+stax*hour*payRate);
System.out.printf("Net Pay: $%.2f\n",hour*payRate*(1-ftax-stax));
}
}
4.24
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter the first city: ");
String city1 = input.nextLine();
System.out.print("Enter the second city: ");
String city2 = input.nextLine();
System.out.print("Enter the third city: ");
String city3 = input.nextLine();
// 在Ascll字母a到z或者A到Z都是升序,考察compareTo方法。
if(city1.compareTo(city2)>0){
String city0 = city1;
city1 = city2;
city2 = city0;
}
if(city2.compareTo(city3)>0){
String city0 = city2;
city2 = city3;
city3 = city0;
}
if(city1.compareTo(city2)>0){
String city0 = city1;
city1 = city2;
city2 = city0;
}
System.out.printf(“The three cities in alphabetical order are %s %s %s”, city1,city2,city3);
}
}
4.25
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="";
for(int i = 0;i<3;i++)
str += (char)((int)('A'+Math.random()*26));
for(int i = 0;i<4;i++)
str += (char)('0'+Math.random()*10);
System.out.println("车牌号为: "+str);
}
}
4.26
package demo;
import java.util.Scanner;
public class Welcome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in string, for example 114.514: ");
String rawMoney = input.next();
int remains = 0;
int dot = rawMoney.indexOf('.');
if(dot==-1)
remains=Integer.parseInt(rawMoney)*100;
else
{
int dollars=Integer.parseInt(rawMoney.substring(0,dot));
int cents = 0;
if(rawMoney.length()-dot==1)
cents=10*Integer.parseInt(rawMoney.substring(dot+1));
else
cents=Integer.parseInt(rawMoney.substring(dot+1,dot+3));
remains=dollars*100+cents;
}
int numdoll = remains/100;
remains=remains%100;
int numqua = remains/25;
remains=remains%25;
int numdi = remains/10;
remains = remains%10;
int numni = remains/5;
remains = remains%5;
int nump = remains;
System.out.println("Your amount "+rawMoney+" consists of");
System.out.println(" "+numdoll+" dollars");
System.out.println(" "+numqua+" quarters");
System.out.println(" "+numdi+" dimes");
System.out.println(" "+numni+" nickels");
System.out.println(" "+nump+" pennies");
}
}