文章目录
1~100之间出现多少个9
public class Test {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i < 100; i++) {
if(i % 10 == 9 ){
System.out.println(i);
count++;
}
if(i / 10 == 9){
System.out.println(i);
count++;
}
}
System.out.println("9出现的次数是"+count);
}
}
判断一个数字是不是素数
//8 1*8 2*4
//20 1*20 2*10 4*5
//2~n-1之间能整除就说明不是素数
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
int num= scanner.nextInt();
int i = 2;
for (; i <num; i++) {
if(num % i==0){
System.out.println(num+"不是素数");
break;
}
}
//走到这,要么是break走到这,要么是已经遍历完没有进入第一个if语句走到这,那么就说明num是素数
if(num==i){
System.out.println(num+"是素数");
}
}
}
1~100之间是素数的(不太掌握)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
for (int j = 1; j <num ; j++) {
int i = 2;
for (; i < j; i++) {
if (j % i == 0) {
//System.out.println(j + "不是素数");
break;
}
}
if (j == i) {
System.out.println(j + "是素数");
}
}
}
}
优化:如果有一个数小于等于j开根号,说明它不是素数(重点)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
for (int j = 1; j <=num ; j++) {
int i = 2;
for (; i <= Math.sqrt(j); i++) { //j=3 i=2
if (j % i == 0) {
//System.out.println(j + "不是素数");
break;
}
}
//走到这,要么是break走到这,要么是已经遍历完没有进入第一个if语句走到这,那么就说明num是素数
if (i > Math.sqrt(j)) {
System.out.println(j + "是素数");
}
}
}
}
求两个数的最大公约数(不熟)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int a= scanner.nextInt(); //24
int b= scanner.nextInt(); // 18
int c = a % b ;// c=6
while(c != 0){
a = b; //a=18
b = c; //b=6
c =a % b;
}
System.out.println(b);
}
}
求1/1 - 1/2 + 1/3 - 1/4 …+1/99-1/100的值
public class Test {
public static void main(String[] args) {
double sum =0;
int flg =1;
for (int i = 1; i <= 100; i++) {
sum += 1.0/i * flg;
flg = -flg;
}
System.out.println(sum);
}
}
打印x图案(不熟)
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNextInt()){
int a =in.nextInt();
for (int i = 0; i <a ; i++) {
for (int j = 0; j < a; j++) {
if(i==j || (i+j)==a-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
输入登录密码
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 3;
while(count != 0){
System.out.println("请输入你的密码,你还有"+count+"次机会!");
String password=in.nextLine();
if(password.equals("12345")){
System.out.println("登录成功!");
break;
}else{
System.out.println("输入错误");
count--;
}
}
}
}
♥♥ 判断是否相等的库函数用 .equals()
♥♥ idea查看原码的方法
按住contrl,移动鼠标到库函数上,鼠标变成一个小手后,点击库函数。
出现下面这个页面,但很多代码,难以找到想看的库函数怎么办呢
点击左侧的strcture即可查看目录
打印九九乘法口诀表
最主要的是格式排版问题
打印 i 与 i+1 之间的空格,j和j+1之间的空格
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int a= in.nextInt();
for (int i = 1; i <= a; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(i+ "*"+ j +"=" +i*j+" ");//隔一个空
}
System.out.println();//一行打完了,跳到下一行打印
}
}
}
计算水仙花数(不熟)
// 1^3 + 5^3 + 3^3 = 153
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 9999999; i++) {
//先求有多少位数,才能知道是几平方
int count = 0;
int tmp = i;
while( tmp != 0){
count ++;
tmp = tmp / 10;
}
//求每一位的数字
tmp = i;
int sum = 0;
while (tmp!= 0){
//把tmp%10得到Math.pow括号左边的数,再把tmp/10得到下一位%10的数
sum += Math.pow(tmp%10,count);//3 15%10=5
tmp /= 10; //153/10=15 5/10=1
}
//如果此时i==sum,说明符合水仙花数
if(sum == i){
System.out.println(i);
}
}
}
}
写一个函数返回二进制中1的个数(不熟)
每&一次,就是一次1
public class Test {
public static void main(String[] args) {
int n = 6;
int count = 0;
while(n !=0){
count++;
n = n&(n-1);
}
System.out.println(count);
}
}
♥♥ 二进制每一位代表的数
♥♥ 方法的使用(重点)
语法格式:
注意:
-
修饰符:现阶段直接使用public static 固定搭配
-
返回值类型:如果方法有返回值,返回值类型必须要与返回的实体类型一致,如果没有返回值,必须写成void
-
方法名字:采用小驼峰命名
-
参数列表:如果方法没有参数,()中什么都不写,如果有参数,需指定参数类型,多个参数之间使用逗号隔开
-
方法体:方法内部要执行的语句
-
在java当中,方法必须写在类当中
-
在java当中,方法不能嵌套定义
-
在java当中,没有方法声明一说
♥♥ 函数调用(重中之重)
方法的调用是需要开辟内存的,方法调用结束,这块内存就销毁了
练习:
调用方法计算 1! + 2! + 3! + 4! + 5!
public class Test {
//求某个数的阶层
public static int fac(int n){
int ret = 1;
for (int i = 0; i <=n ; i++) {
ret *= i;
}
return ret;
}
//求阶层的和
public static int facNum(int k){
int sum = 0;
for (int i = 1; i <= k; i++) {
sum += fac(i);//把每个数的阶层加起来
}
return sum;
}
//
public static void main(String[] args) {
System.out.println(facNum(5));
}
}
方法重载
在Java中,如果多个方法的名字相同,参数列表不同,则称该几种方法被重载了
注意:
1 方法名必须相同
2. 参数列表必须不同(参数的个数不同、参数的类型不同、类型的次序必须不同)
3. 与返回值类型是否相同无关
public class Date {
public static int add(int a,int b){
return a + b;
}
public static double add(double a,double b){
return a + b;
}
public static double add(double a,double b,double c){
return a + b + c;
}
public static void main(String[] args) {
System.out.println(add(1.2,3.2));
System.out.println(add(7,9));
}
}
总结
今日写了超多逻辑控制的题,总算是找回熟悉的感觉了,还有对方法的使用,再好好地看了一遍终于清晰了,这两天得找个时间巩固复习方法这一块内容,趁热打铁。今天又坚持下来,很棒!