return true;
}
}
}
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter a credit card number: ");
String num = input.next();
if(isValid(num))
System.out.println(num+" is valid");
else
System.out.println(num+" is invalid");
}
public static boolean isValid(String number){
if(number.length()<13||number.length()>16)
return false;
else
{
return (sumOfDoubleEvenPlace(number)+sumOfOddPlace(number))%10==0;
}
}
public static int sumOfDoubleEvenPlace(String number){
int sum=0;
for(int i=number.length()-2;i>=0;i-=2)
sum+=getDigit(2*(number.charAt(i)-‘0’));
return sum;
}
public static int getDigit(int number){
if(number<10)
return number;
else
return number/10+number%10;
}
public static int sumOfOddPlace(String number){
int sum=0;
for(int i=number.length()-1;i>=0;i-=2)
sum+=(int)(number.charAt(i)-‘0’);
return sum;
}
public static boolean prefixMatched(String number){
char c1=number.charAt(0);
char c2=number.charAt(1);
return c1 == ‘4’ || c1 == ‘5’ || c1 == ‘6’ || (c1 == ‘3’) && (c2 == 7);
}
}
没按照题目上的格式来,不过方法是对的。
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args){
int count = 0;
for(int i=0;i<10000;i++){
if(throwDice())
count++;
}
System.out.println(“you won “+count+” times.”);
}
public static boolean throwDice()
{
int num1 = (int)(Math.random()*6)+1;
int num2 = (int)(Math.random()*6)+1;
int sum = num1+num2;
if(sum2||sum3||sum==12) {
System.out.printf(“You rolled %d+%d = %d\nYou lose\n”,num1,num2,sum);
return false;
}
else if(sum7||sum11) {
System.out.printf(“You rolled %d+%d = %d\nYou win\n”,num1,num2,sum);
return true;
}
else
{
System.out.printf(“You rolled %d+%d = %d\n”,num1,num2,sum);
System.out.println("point is "+sum);
int sum2 = sum;
do{
int num3 = (int)(Math.random()*6)+1;
int num4 = (int)(Math.random()*6)+1;
sum=num3+num4;
System.out.printf(“You rolled %d+%d = %d\n”,num3,num4,sum);
if(sum==7){
System.out.println(“You lose”);
return false;
}
else if(sum!=sum2)
System.out.println("point is "+sum);
}while(sum!=sum2); //这里使用do-while语句好处就是先执行一次,再判断。
System.out.println(“You win”);
return true;
}
}
}
=================================================================
咳,看6.25习题答案吧,稍微改改就行,实在不想做一遍了。。。。
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter full year (e.g., 2012): ");
int year = input.nextInt();
System.out.print("What day is January 1, “+year+” ? ");
int week = input.nextInt();
int month = 1, day = 0;
String monthString = “”;
boolean leapYear;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 && year % 3200 != 0) || year % 172800 == 0)
leapYear = true;
else
leapYear = false;
for (; month <= 12; month++) {
switch (month) {
case 1:
monthString = “January”;
break;
case 2:
day += 31;
monthString = “February”;
break;
case 3:
monthString = “March”;
if (leapYear)
day += 29;
else
day += 28;
break;
case 4:
day += 31;
monthString = “April”;
break;
case 5:
day += 30;
monthString = “May”;
break;
case 6:
day += 31;
monthString = “June”;
break;
case 7:
day += 30;
monthString = “July”;
break;
case 8:
day += 31;
monthString = “August”;
break;
case 9:
day += 31;
monthString = “September”;
break;
case 10:
day += 30;
monthString = “October”;
break;
case 11:
day += 31;
monthString = “November”;
break;
case 12:
day += 30;
monthString = “December”;
}
int days = (week + day) % 7;
System.out.print("\n " + monthString + " " + year + “\n---------------------------------”);
System.out.printf(“\n%-5s%-5s%-5s%-5s%-5s%-5s%-5s\n”, “Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”);
for (int n =1;n<=days;n++) {
System.out.printf(“%-5s”, “”);
}
int j = 1;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||
month == 12) {
for (; j <= 31; j++) {
System.out.printf(“%-5d”, j);
if ((days+j) % 7 == 0)
System.out.println();
}
}
else if (month == 2 && leapYear) {
for (; j <= 29; j++) {
System.out.printf(“%-5d”, j);
if ((days+j) % 7 == 0)
System.out.println();
}
}
else if (month == 2) {
for (; j <= 28; j++) {
System.out.printf(“%-5d”, j);
if ((days+j) % 7 == 0)
System.out.println();
}
}
else {
for (; j <= 30; j++) {
System.out.printf(“%-5d”, j);
if ((days + j) % 7 == 0)
System.out.println();
}
}
System.out.print(“\n”);
switch (days) {
case 0:
System.out.print(“Sun”);break;
case 1:
System.out.print(“Mon”);break;
case 2:
System.out.print(“Tue”);break;
case 3:
System.out.print(“Wed”);break;
case 4:
System.out.print(“Thu”);break;
case 5:
System.out.print(“Fri”);break;
case 6:
System.out.print(“Sat”);
}
System.out.println(" starts on the first day of "+monthString);
}
}
}
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args){
System.out.print("Enter the side: ");
Scanner input = new Scanner(System.in);
double s = input.nextDouble();
System.out.println("The area of the pentagon is "+area(s));
}
public static double area(double side){
double area;
area = (5Math.pow(side, 2))/(4Math.tan(Math.PI/5));
return area;
}
}
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args){
System.out.print("Enter the number of sides: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
System.out.print("Enter the side: ");
double s = input.nextDouble();
System.out.println("The area of the pentagon is "+area(n,s));
}
public static double area(int n,double side){
double area;
area = (nMath.pow(side, 2))/(4Math.tan(Math.PI/n));
return area;
}
}
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args){
System.out.print("Enter the number: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
System.out.print("Enter the width: ");
int w = input.nextInt();
System.out.println("The format of the number is "+format(n,w));
}
public static String format(int number,int width){
String str = Integer.toString(number);
int len = str.length();
if(len>=width)
return str;
else{
String str2=“”;
for(int i=1;i<=width-len;i++){
str2+=‘0’;
}
return str2+str;
}
}
}
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args) {
for(int i=1;i<=100;i++)
{
System.out.print(getRandomUpperCaseLetter()+" ");
if(i%10==0)
System.out.print(‘\n’);
}
for(int i=1;i<=100;i++)
{
System.out.print(getRandomDigitCharacter()+" ");
if(i%10==0)
System.out.print(‘\n’);
}
}
public static char getRandomCharacter(char ch1,char ch2)
{
return (char)(ch1+Math.random()*(ch2-ch1+1));
}
public static char getRandomUpperCaseLetter()
{
return getRandomCharacter(‘A’,‘Z’);
}
public static char getRandomDigitCharacter()
{
return getRandomCharacter(‘0’,‘9’);
}
}
=================================================================
package demo;
import java.util.Scanner;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter three points for p0,p1,p2: ");
double x0 = input.nextDouble();
double y0 = input.nextDouble();
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
if(leftOfTheLine(x0,y0,x1,y1,x2,y2))
System.out.print(“(”+x2+“,”+y2+“)”+" is on the left side of the line from “+”(“+x0+”,“+y0+”)“+” to “+”(“+x1+”,“+y1+”)");
else if(onTheLineSegment(x0,y0,x1,y1,x2,y2))
System.out.print(“(”+x2+“,”+y2+“)”+" is on the line segment from “+”(“+x0+”,“+y0+”)“+” to “+”(“+x1+”,“+y1+”)");
else if(onTheSameLine(x0,y0,x1,y1,x2,y2))
System.out.print(“(”+x2+“,”+y2+“)”+“is on the same line from “+”(”+x0+“,”+y0+“)”+" to “+”(“+x1+”,“+y1+”)");
else
System.out.print(“(”+x2+“,”+y2+“)”+“is on the right side of the line “+”(”+x0+“,”+y0+“)”+" to “+”(“+x1+”,“+y1+”)");
}
public static boolean leftOfTheLine(double x0,double y0,double x1,double y1,double x2,double y2){
return (x1-x0)(y2-y0)-(x2-x0)(y1-y0)>0;
}
public static boolean onTheSameLine(double x0,double y0,double x1,double y1,double x2,double y2){
return (x1-x0)(y2-y0)-(x2-x0)(y1-y0)==0;
}
public static boolean onTheLineSegment(double x0,double y0,double x1,double y1,double x2,double y2){
return (x1-x0)(y2-y0)-(x2-x0)(y1-y0)==0&&x2>=x0&&x2<=x1;
}
}
第六章习题 完
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
给大家送一个小福利
附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-ZTi6gLQH-1713175789568)]
[外链图片转存中…(img-h55pMARB-1713175789568)]
[外链图片转存中…(img-OzPZpYga-1713175789569)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
给大家送一个小福利
[外链图片转存中…(img-qMKniDFe-1713175789569)]
附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。
[外链图片转存中…(img-fm56cbne-1713175789569)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!