package com.homework;
import java.util.Scanner;
public class Fibonacci{
public static void main(String[] args) {
System.out.println("请输入你想打印的项数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] fib = new int[n];
System.out.println("打印为:");
fib[0] =1;
fib[1] = 1;
System.out.println(fib[0]);
System.out.println(fib[1]);
for(int i = 2;i < fib.length;i++){
fib[i] = fib[i-1] + fib[i-2];
System.out.println(fib[i]);
}
}
}
package Test1;
import java.util.Scanner;
public class Fib {//斐波那契数列
public static void main(String args[]){
int n;
System.out.println("请输入你想打印的项数:");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int a = 1;
int b = 1;
System.out.println("打印的斐波那契数列为:");
for(int i = 1;i <= n;i++){
System.out.println(a);
System.out.println(b);
a = a+b;
b = a+b;
}
}
}
2、字符串反转,单词内部字母顺序不变
package com.homework;
//单词反转,单词内字母顺序不变
public class swapString{
public static void main(String[] args) {
String str = "I am a student";
String[] arr = str.split(" ");
for(int i = 0;i < arr.length / 2;i++){
String temp = arr[i];
arr[i] = arr[arr.length -1 - i];
arr[arr.length - 1 - i] = temp;
}
System.out.println("反转结果输出为:");
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i]+" ");
}
}
3、1~n个数字中,1出现的次数
package com.homework;
//1~n的数字中1出现的次数
import java.util.Scanner;
public class NumOf1{
public static void main(String[] args) {
System.out.println("请输入:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int count = 0;
for(int i = 1;i <= num;i++){
int temp = i;
while(temp != 0){
if(temp % 10 == 1){
count++;
}
temp /= 10;
}
}
System.out.println("其中1出现的次数为:");
System.out.println(count);
}
}
4、输入字符串打印出数字
package com.homework;
import java.util.Scanner;
public class StrToNum{
private static int getBit(char[] arr) {
return arr.length;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
char[] arr = str.toCharArray();
int bit = getBit(arr);
int result = 0;
int flag = 1;
for(int i = 0;i < arr.length;i++){
if(arr[i] == '-'){
flag *= -1;
}else if(arr[i] == '+' || arr[i] == ' '){
continue;
}else{
int n = arr[i] - ' ';
result += (int)(n*Math.pow(10,--bit));
}
}
result = result*flag;
System.out.println(result);
}
}
5、从一副扑克中随机抽取五张,判断是否为顺子
package com.homework;
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
public class isContinuous{
private static int getCountZero(int[] arr){
int countZero = 0;
for(int i=0;i<arr.length && arr[i] == 0;i++){
countZero++;
if(arr[i] == 0){
countZero++;
}
}
if(countZero>2){//重新随机
isSeries();
// return false;
}
return countZero;
}
private static boolean isSeries(int[] arr){
Arrays.sort(arr);
//1.大小王个数
int countZero = getCountZero(arr);
//2.统计间隔数
int countGrap = 0;
for (int i = countZero;i<arr.length-1;i++){
if(arr[i] == arr[i+1]){
return false;
}
if(arr[i]+1 != arr[i+1]){
//统计间隔数 0 0 1 2 3 4 9
for(int j=arr[i];j<arr[i+1]-1;j++){
//
countGrap++;
}
}
}
if(countZero >= countGrap){
return true;
}
return false;
}
public static void isSeries(){
Random random = new Random();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入牌个数");
int count = scanner.nextInt();
int[] arr = new int[count];
for(int i=0;i<arr.length;i++){
arr[i] = random.nextInt(14);//[0,14)
}
System.out.println(Arrays.toString(arr));
boolean flag = isSeries(arr);
if(flag){
System.out.println("是顺子");
}else{
System.out.println("不是顺子");
}
}
}
6、输入一个矩阵,从外到内顺时针打印输出
package com.homework;
//输入一个矩阵,从外到内以顺时针方向打印输出
public class printMatrix {
public static void main(String[] args) {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
//圈数,最外圈假设是0
for (int j = 0; j < arr[0].length; j++) {
//上
for (int i = 0 + j; i < arr.length - j; i++) {//i表示列
System.out.print(arr[0 + j][i] + " ");
}
//右
for (int i = 1 + j; i < arr.length - j; i++) {//i表示行
System.out.print(arr[i][arr.length - 1 -j] + " ");
}
//下
for (int i = arr.length - 1 - 1 - j; i >= 0 + j; i--) {//i表示列
System.out.print(arr[arr.length - 1 - j][i] + " ");
}
//左
for (int i = arr.length - 1 - 1 - j; i >= 1 + j; i--) {//i表示行
System.out.print(arr[i][0 + j] + " ");
}
}
}
}
7、求1/1+1/2+1/3+…的值
import java.util.Scanner;
//求1/1+1/2-1/3+1/4.....的和
public class getSum {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
int n = scanner.nextInt();
double sum = 0;
int flag = 1;
for(int i = 1;i < n;i++){
sum = sum + (1.0/i)*flag;
flag *= -1;
}
System.out.println(sum);
}
}
8、输入一个数判断是否为素数
import java.util.Scanner;
public class Test1 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
int num = scanner.nextInt();
if (num == 1 || num == 2){
System.out.println(num + "是素数");
return;
}
int i = 2;
for(i = 2;i < num;i++){//变量的生命周期
if(num % i == 0){
System.out.println(num + "不是素数");
break;
}
else{
System.out.println(num + "是素数");
break;
}
}
}
}
import java.util.Scanner;
public class Test2 {
public static boolean isPrime(int num) {
if (num == 1 || num == 2) {
return true;
}
int i = 0;
for (i = 2; i < num; i++) {//变量的生命周期
if (num % i != 0) {
return true;
} else {
return false;
}
}
return false;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
int num = scanner.nextInt();
boolean flag = isPrime(num);
if (flag = true) {
System.out.println(num + "是素数");
} else{
System.out.println(num + "不是素数");
}
}
}
9、输入一个数字,输出它的位数
import java.util.Scanner;
public class getBit {
public static int getBit(int num){
int count = 0;
while(num != 0){
num = num/10;
count++;
}
return count;
}
public static void getEveryBitNumber(int num){
int Bit = getBit(num);
int count = 1;
for(int i = 0;i < Bit;i++){
count = count * 10;
for(count = 1;count > 0;count = count/10){
System.out.println(num/count);
num = num % count;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = scanner.nextInt();
int count = getBit(num);
System.out.println(count);
}
}
10、猜价格的小游戏代码
import java.util.Random;
import java.util.Scanner;
public class Price {
public static void main(String[] args) {
while (true) {
Random random = new Random();
int goodsPrice = random.nextInt(1000) + 1;
System.out.println("请输入你猜的价格:");
//计数器
int count = 0;
Scanner scanner = new Scanner(System.in);
//猜价格,三次机会
while (count < 3) {
count++;
int guessPrice = scanner.nextInt();
if (guessPrice > goodsPrice) {
System.out.println("你猜贵了呢!");
} else if (guessPrice < goodsPrice) {
System.out.println("你猜少了哦!");
} else {
System.out.println("恭喜你,猜对啦!");
break;
}
}
System.out.println("请选择是否重新猜价格: 1.是 2.否");
//处理方法二,比较字符串
String selectSwitch = scanner.next();
if (selectSwitch.equals("是")){
continue;
}if (selectSwitch.equals("否")){
break;
}
// 处理方式1,数字1代表继续2代表返回
int selectSwitchNumber = scanner.nextInt();
if (selectSwitchNumber == 1) {
continue;
}
else if(selectSwitchNumber == 2){
break;
}
}
}
}
11、使用integer的静态方法parseInt将字符串转变为制指定进制的数字
public class Test2 {
public static void main(String args[]){
String s = "-123";//定义字符串
int b = Integer.parseInt(s,10);//使用integer的静态方法parseInt将字符串转变为制指定进制的数字
System.out.println(b);//输出
}
}