Java输入输出(经典题)

目录

二进制序列

模拟登陆

输出一个整数的每一位

输出乘法口诀表

找出出现一次的数字

斐波那契数

求阶乘和

求 N 的阶乘 

奇数位于偶数之前

求最大值

求最大值方法的重载

求和的重载


二进制序列


 获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列

import java.util.Scanner;

public class TestDemo {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入数字:");
        int value=scan.nextInt();
        System.out.println("偶数序列:");
        for(int i=31;i>0;i-=2){
            System.out.print((value>>i)&1);
        }
        System.out.println("");
        System.out.println("奇数序列:");
        for(int i=30;i>=0;i-=2){
            System.out.print((value>>i)&1);
        }
    }
}

模拟登陆


 编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序

import java.util.Scanner;
public class Test14{
    public static void main(String[] args) {
        System.out.println("input password:");
        for(int count=1;count<=3;count++){
        Scanner sc=new Scanner(System.in);
        String password=sc.nextLine();
            if(password.equals("123456")){
                 System.out.println("ture");
                 break;
        }else{
            System.out.println("false"); 
        }
      } 
    }
}

 

输出一个整数的每一位


 输出一个整数的每一位,如:123的每一位是1 , 2 , 3

import java.util.Scanner;
public class IntegerEachDigit {
    //输出一个整数的每一位,如:123的每一位是1 , 2 , 3
    public static void Print(int num){
        if(num < 0){            //判断是负数先打印出负号,再将负数变为正数
            System.out.print("-,");
            num *=-1;
        }
        if(num > 9){           //判断是否到个位数,没到就继续递归
            Print(num / 10);
        }
        System.out.print(num % 10+",");//打印当前数字的最低位
    }
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      while(scanner.hasNextInt()){   //循环输入
          int num = scanner.nextInt();
          Print(num);   //打印方法的调用
          System.out.println();
      }
    }
}

输出乘法口诀表


public class TestDemo {

public static void multiplicationTable(int n) {

int i=1;

int j=1;

for(i=1;i<=n;i++) {

for(j=1;j<=i;j++) {

System.out.print(j+"*"+i+"="+j*i+" " );

}

System.out.println();

}

}

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

int n = scan.nextInt();

multiplicationTable(n);

}

}

 

找出出现一次的数字


 有一组数据,只有一个数字是出现一次,其他是两次,请找出这个数字。

public class TestDemo{
    public static void main(String[] args) {
        int[] arr = {1, 1, 2, 2, 3, 3, 4, 4, 5};
        frequency(arr);
    }

    public static void frequency(int[] a) {
        for (int i = 0; i < a.length; i++) {
            int count = 0;//判断出现次数
            for (int j = 0; j < a.length; j++) {
                if (a[i] == a[j]) count++;
            }
            if (count == 1)
                System.out.printf("只出现一次的数字是%d",a[i]);//只出现一次则输出该数据

        }
    }
}
    //运行结果只出现一次的数字是5

斐波那契数

import java.util.Scanner;
public class Test20{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int sum=Feibo(n);
        System.out.println("sum="+sum);
       sc.close();
    }public static int Feibo(int n){
        int sum=0;
        if(n==1||n==2){
            sum=n;
        }int a=1;int b=1;
        for(int i=3;i<=n;i++){
            sum=a+b;
             a=b;
             b=sum;
        }return sum;
        }
}

求 N 的阶乘


 求1!+2!+3!+4!+........+n!的和

public class TestDemo {
    public static void main(String[] args){
        System.out.printf("输入任意一个数:");
        Scanner input = new Scanner(System.in);  //用于接收输入数据
        int n = input.nextInt();                 //将输入的数据赋给 n
        int a = 1;                               //用于存储阶乘的值
        for(int i = 1;i <= n;i++){
            a *= i;                              // 等同于 a = a*i; ---->阶乘运算公式
            System.out.printf("%d\n",a);         //每计算一次就打印一次
        }
        System.out.printf("%d的阶乘为:%d\n",n,a); //打印最后计算的结果
    }
}

 

 求阶乘和


public class SumFactorial{
  public static void main(String[] args){
 
        System.out.println("sum=" + Factorial(5));
 
    }
 
 
 
 
public static long Factorial(int n){   //定义阶乘相加函数
 
         int sum, s,j;
		sum = 0;
		for (int i = 1; i <= n; i++) {   //外循环控制次数
			j = 1;
			for ( s = 1; j <= i; j++) {     //内循环计算每一个阶乘
				s = s * j;
			}
			sum = sum + s;                  //每一个阶乘相加
		}
      return sum;                          //返回结果
}
 
 
}
public class TestDemo {
	public static void main(String args[]) {
		long sum=0,num=1;           //sum用于加和,num作为每一个数阶乘后的结果
		for(int i=1;i<=20;i++) {
			num*=i;                 //num始终保留上一次阶乘的结果,所以只需要乘i
			sum+=num;               //每次阶乘后相加
		}
		System.out.println("1+2!+3!+...+20!="+sum);
	}
}

奇数位于偶数之前


 调整数组顺序使得奇数位于偶数之前。调整之后,不关心大小顺序

 public class Test17{
    public static void main(String[] args) {     
        int []arr={1,2,3,4,5,6,7,8,9,10};
        int i;
        int j;
        for(i=0;i<10;i++){
            for(j=i+1;j<10;j++){
            if(arr[j]%2!=0){
                int tmp=arr[j];
                arr[j]=arr[i];
                arr[i]=tmp;
            }
        } 
        System.out.print(arr[i]+" ");
      }
 }
}

 

求最大值 


 创建方法求两个数的最大值max2,随后再写一个求3个数的最大值的函数max3。

​ 要求:在max3这个函数中,调用max2函数,来实现3个数的最大值计算

import java.util.Scanner;
public class TestDemo{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        double a=sc.nextDouble();
        double b=sc.nextDouble();
        double c=sc.nextDouble();
        double max=Max3(a, b,c);
        System.out.println(max);
    }
    public  static double Max2(double a,double b) {
            double max2;
            if(a>b){
                max2=a;
            }else{
                max2=b;
            }
            return max2;
    }
    public static double Max3(double a,double b,double c){
        double max3;
       double max2=Max2(a,b);
       if(max2>c){
           max3=max2;
       }else{
           max3=c;
       }
       return max3;
    }
}

求最大值方法的重载

 在同一个类中定义多个方法:要求不仅可以求两个整数的最大值,还可以求两个小数的最大值,以及两个小数和一个整数的大小关系


import java.util.Scanner;

public class TestDemo {

 public static int max(int a,int b) {

  int max = a>=b?a:b;

 return max;

 }

 public static double max1(double a,double b) {

 double max = a>=b?a:b;

 return max;

 }

 public static double max(double a,double b,int c) {

 double max = max1(a,b)>=c?max1(a,b):c;

 return max;

 }

 public static void main(String[] args) {

 Scanner scan = new Scanner(System.in);

 int a1 = scan.nextInt();

 int b1 = scan.nextInt();

 System.out.println(max(a1,b1));

 double a2 = scan.nextDouble();

 double b2 = scan.nextDouble();

 System.out.println(max1(a2,b2));

 double a3 = scan.nextDouble();

 double b3 = scan.nextDouble();

 int c1 = scan.nextInt();

 System.out.println(max(a3,b3,c1));

 }

}

 

求和的重载


 在同一个类中,分别定义求两个整数的方法 和 三个小数之和的方法。 并执行代码,求出结果

public class TestDemo{

 public static void main(String[] args) {

  Scanner scan = new Scanner(System.in);

  int a = scan.nextInt();

  int b = scan.nextInt();

  System.out.println("两个整数的和是"+add(a,b));

  double i = scan.nextDouble();

  double j = scan.nextDouble();

  double k = scan.nextDouble();

  System.out.println("三个小数的和是"+add(i,j,k));

 }

 public static int add(int a,int b) {

  return a+b;

 }

 public static double add(double i,double j,double k) {

  return i+j+k;

 }

二刷 补交作业

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值