Java零基础学习03(有C语言基础)
本章主要熟练else-if选择语句。
文章目录
/*课后习题p98 3.4 随机月份:编写一个随机产生1和12之间的整数程序,并且根据数字,输出对应的月份:January,February...*/
/*此题主要switch的使用,switch小括号之后要大括号才开始case。
* 并且一定要记得在case后加上break;否则就可能会发生穿透,出现不理想的结果,像图1那样*/
public class Java_1 {
public static void main(String[] args) {
int number=(int)(Math.random()*(12-1+1)+1);/*random()产生【0,1】之间的随机浮点数,要变成整数要强制转换。random返回指定范围[m,n]的公式:Math.random()*(n-m+1)+m */
System.out.println(number);
switch (number){
case 1: System.out.println("January");break;
case 2: System.out.println("February");break;
case 3: System.out.println("March");break;
case 4: System.out.println("Forth");break;
case 5: System.out.println("May");break;
case 6: System.out.println("June");break;
case 7: System.out.println("July");break;
case 8: System.out.println("August");break;
case 9: System.out.println("September");break;
case 10: System.out.println("October");break;
case 11: System.out.println(" November ");break;
case 12: System.out.println("December");break;
default:System.out.println("无效值,程序设计可能出错,请检查!");
}
}
}
图1
/*课后习题p98 3.5给出将来的日期*/
/*此题主要是了解计算星期公式,并且熟练使用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 today's day: ");
int todaynumber=input.nextInt();
String str1;
switch (todaynumber){
case 0: str1="Sunday";break;
case 1: str1="Monday";break;
case 2: str1="Tuesday";break;
case 3: str1="Wednesday";break;
case 4: str1="Thursday";break;
case 5: str1="Friday";break;
case 6: str1="Saturday";break;
default:str1="无效值,程序设计可能出错,请检查!";
}
System.out.print("Enter the number of days elapsed since today: ");
int elsedaynumber=input.nextInt();
int number=(todaynumber+elsedaynumber)%7;/*计算星期的公式*/
String str2;
switch (number){
case 0: str2="Sunday";break;
case 1: str2="Monday";break;
case 2: str2="Tuesday";break;
case 3: str2="Wednesday";break;
case 4: str2="Thursday";break;
case 5: str2="Friday";break;
case 6: str2="Saturday";break;
default:str2="无效值,程序设计可能出错,请检查!";
}
System.out.println("Today is "+str1+"and the future day is "+str2);
}
}
/*课后习题p99 3.11给出某月份的天数*/
/*1、此题主要是了解闰年判断条件,两个条件缺一不可。1、能被 4 整除而不能被 100 整除。 2、能被 400 整除*/
/*2、注意整型变量和字符变量定义时,都要赋予初值,否则,后面输出会报错!!!奇奇怪怪*/
/*3、switch中的case可以按照需求排序,不一定要按照升序排。*/
import java.util.Scanner;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of years: ");
int year = input.nextInt();
System.out.print("Enter the number of months: ");
int months = input.nextInt();
int day=0;
String str="str";/*整型变量和字符变量定义时,都要赋予初值,否则,后面输出会报错!!!奇奇怪怪*/
switch (months) {
case 1:
str = "January";
break;
case 2:
str = "February";
break;
case 3:
str = "March";
break;
case 4:
str = "Forth";
break;
case 5:
str = "May";
break;
case 6:
str = "June";
break;
case 7:
str = "July";
break;
case 8:
str = "August";
break;
case 9:
str = "September";
break;
case 10:
str = "October";
break;
case 11:
str = "November";
break;
case 12:
str = "December";
break;
}
/*以上的case是按升序排,一般没有特殊情况也是按照顺序排,这样不容易漏*/
if(months!=2){
switch (months) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day=31;break;
case 4:
case 6:
case 9:
case 11:day=30;break;
/*以上的case是按照需求排序,没有出错,可以这样用。但是用得少*/
}
}
else {
if(year%4==0&&year%100!=0||year%400==0)
{day=29;}/* 闰年判断条件:1。能被 4 整除而不能被 100 整除。 2。能被 400 整除*/
else
{day=28;}
}
System.out.println(str+" "+year+" has "+day+"days");
}
}
/*课后习题p100 3.21 科学星期几*/
/*这题不用看太多,题目公式说的很清楚,主要是练else-if*/
import java.util.Scanner;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter years: ");
int year = input.nextInt();
System.out.print("Enter months: ");
int months = input.nextInt();
System.out.print("Enter the day of the month: ");
int day = input.nextInt();
if (months==1){
months=13;
year=year-1;
} else if (months==2) {
months=14;
year=year-1;
}/*公式里1、2月特殊,题目有说明*/
int weeks=0;
weeks=(day+(26*(months+1))/10+year%100+(year%100)/4+(year/100)/4+5*(year/100))%7;
String str2="str";/*整型变量和字符变量定义时,都要赋予初值,否则,后面输出会报错!!!奇奇怪怪*/
switch (weeks){
case 1: str2="Sunday";break;
case 2: str2="Monday";break;
case 3: str2="Tuesday";break;
case 4: str2="Wednesday";break;
case 5: str2="Thursday";break;
case 6: str2="Friday";break;
case 0: str2="Saturday";break;
default:str2="无效值,程序设计可能出错,请检查!";
}
System.out.println("Day of the week is "+str2);
}
}
*课后习题p105 3.33 科学星期几*/
/*此题目的在熟练使用if-else,并且使用Math.min()函数找两个数中的最小值*/
import java.util.Scanner;
public class Java_1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter weight and price for package 1: ");
double w1=input.nextDouble();
double p1=input.nextDouble();
double x=p1/w1;
System.out.print("Enter weight and price for package 2: ");
double w2=input.nextDouble();
double p2=input.nextDouble();
double y=p2/w2;
if(x==y){
System.out.println("Two packages have the same price");
}
else {
double result=Math.min(x,y);/*java中Math.min()比较最小值的方法*/
if (result==x)
System.out.println("Packages 1 have a better price");
else {
System.out.println("Packages 2 have a better price");
}
}
}
}