首页 > 编程语言 >Java入门练习2

Java入门练习2

时间:2022-12-27 22:34:37浏览次数:43  
标签:count Java 入门 int 练习 System number public Scanner

1、循环

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int balance = 0;
        while (true){
            System.out.print("请投币:");
            int amount = scan.nextInt();
            balance = balance + amount;
            if (balance >= 10){
                System.out.println("********************");
                System.out.println("********Java城际铁轨***");
                System.out.println("*****票价10元*********");
                System.out.println("*********************");
                System.out.println("找零:"+(balance-10));
                balance = 0;
            }
        }
    }
}

2、数数字

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        //判断一个数字的位数,0和负数除外
        int count = 0;
        Scanner scan = new Scanner(System.in);
        int number = scan.nextInt();
        while (number>0){
            number = number / 10;
            count = count + 1;
        }
        System.out.println(count);
    }
}

3、do-while循环

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        //判断一个数字的位数,包含0,不包含负数
        int count = 0;
        Scanner scan = new Scanner(System.in);
        int number = scan.nextInt();
        do {
            number = number / 10;
            count = count + 1;
        }while (number > 0);
        System.out.println(count);
    }
}

4、算平均数

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        //计算输入数字的平均数,输入-1时结束程序
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        int sum = 0;
        int count = 0;
        while (number != -1){
            sum = sum + number;
            count = count + 1;
            number = in.nextInt();
        }
        if (count > 0) {
            System.out.println("平均数为:" + (double) sum / count);
        }
    }
}
import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        //计算输入数字的平均数,输入-1时结束程序
        Scanner in = new Scanner(System.in);
        int number;
        int sum = 0;
        int count = 0;
        do {
            number = in.nextInt();
            if (number != -1) {
                sum = sum + number;
                count = count + 1;
            }
        }while (number != -1);
        if (count > 0){
            System.out.println("平均数:"+(double)sum/count);
        }
    }
}

5、猜数游戏

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        //猜数,Math.random() 产生一个【0,1)之间的浮点数
        int num = (int)(Math.random()*100 + 1);//[0,1)---[0,100)---[0,101)---[1,100]
        int a ;
        int count = 0;
        Scanner in = new Scanner(System.in);
        do {
            a = in.nextInt();
            count = count + 1;
            if (a > num) {
                System.out.println("偏大");
            } else if (a < num) {
                System.out.println("偏小");
            }
        }while(a != num);
        System.out.println("猜对了!共猜了"+ count+"次");
    }
}
import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        //猜数,Math.random() 产生一个【0,1)之间的浮点数
        int number = (int)(Math.random()*100 + 1);//[0,1)---[0,100)---[0,101)---[1,100]
        int a ;
        int count = 0;
        Scanner in = new Scanner(System.in);
        a = in.nextInt();
        count = count + 1;
        while (a != number){
            if (a>number){
                System.out.println("偏大");
            }else {
                System.out.println("偏小");
            }
            a = in.nextInt();
            count = count + 1;
        }
        System.out.println("猜对了!共猜了"+count+"次");
    }
}

6、整数分解

import java.util.Scanner;
//将整数逆序输出
public class Hello {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("请输入一位整数:");
        int number = in.nextInt();
        int result = 0;
        do {
            int digit = number % 10;
            result = 10 * result + digit;
            number = number / 10;
        }while(number > 0);
        System.out.println(result);
    }
}

7、阶乘

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int factor = 1;
        int i = 1;
        while (i <= n){
            factor = factor * i;
            i = i + 1;
        }
        System.out.println(factor);
    }
}

8、素数

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int isPrime = 1;
        int n = in.nextInt();
        for (int i = 2; i < n; i++){
            if(n%i == 0){
                isPrime = 0;
                System.out.println(n+"不是素数,i="+i);
                break;
            }
        }
        if(isPrime==1){
            System.out.println(n+"是素数");
        }else{
            System.out.println(n+"不是素数");
        }
    }
}

9、100以内的素数

import java.util.Scanner;

public class Hello {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        for(int n=2; n<100; n++){
            int isPrime = 1;
            for(int i=2; i<n; i++){
                if(n%i == 0){
                    isPrime = 0;
                    break;
                }
            }
            if(isPrime == 1){
                System.out.print(n+" ");
            }
        }
    }
}

10、九九乘法表

public class Hello {
    public static void main(String[] args){
        for(int i=1; i<=9; i++){
            for(int j=1; j<=i; j++){
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}

11、输出前50个素数

public class Hello{
    public static void main(String[] args){
        int add = 0;
        int n = 2;
        while(add!=50){
            int isPrime = 1;
            for(int i = 2; i<n; i++){
                if (n%i == 0){
                    isPrime = 0;
                    break;
                }
            }
            if(isPrime == 1) {
                System.out.print(n + " ");
                add++;
            }
            n++;
        }
    }
}

12、 凑硬币

import java.util.Scanner;

public class Hello{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int amount = in.nextInt();
        OUT:
        for(int one=0; one <=amount; one++){
            for(int five=0; five <= amount/5; five++){
                for(int ten=0; ten <= amount/10; ten++){
                    for(int twenty=0; twenty <= amount/20; twenty++){
                        if(one+five*5+ten*10+twenty*20==amount){
                            System.out.println(one+"张一元,"+five+"张五元,"+ten+"张十元,"+twenty+"张二十元。");
                            break OUT;
                        }
                    }
                }
            }
        }
    }
}

 

标签:count,Java,入门,int,练习,System,number,public,Scanner
From: https://www.cnblogs.com/feimanzhh/p/16930842.html

相关文章

  • 一文带你入门Transformer
    让我们初学一下Transformer,它确实相对难以理解,下面让我们开始吧!朋友们.Don'tworryaboutit前提在这里我们用x<t>表示文本位置→输入用Tx表示文本长度用y<t......
  • Java学习笔记------线程安全问题
    线程的安全问题同步机制解决线程安全问题方式一:同步代码块synchronized(obj){ //需要被同步的代码}synchronized(this){}synchronized(Windows.class){}......
  • Java学习笔记----线程基础
    线程线程,进程可进一步细化为线程,是一个程序内部的一条执行路径线程作为调度和执行的单位,每个线程拥有独立的运行栈和程序计数器,线程切换的开销小线程的创建与启动Java......
  • Java面试题-前端Vue
    Vue的生命周期beforeCreate(创建前)在数据观测和初始化事件还未开始created(创建后)完成数据观测,属性和方法的运算,初始化事件,$el属性还没有显示出来beforeMount(载入前)在......
  • 2022.12.26动态规划练习
    洛谷P1091[NOIP2004提高组]合唱队形题目描述\(n\)位同学站成一排,音乐老师要请其中的\(n-k\)位同学出列,使得剩下的\(k\)位同学排成合唱队形。合唱队形是指这样的......
  • 精心整理Java微服务最全面试题集(含答案)
    微服务架构相关大型网站架构演变过程网站架构演变演变过程传统架构 →分布式架构 → SOA架构 →微服务架构什么是分布式架构分布式架构就是将传统结构按照模块......
  • 泛型(java)
    1.泛型的理解和好处1.1看一个需求1)请编写程序,在ArrayList中,添加3个Dog对象Dog对象含有name和age,并输出name和age(要求使用getXxx())先使用传统的方法来解决->引出......
  • JavaScript普通函数与箭头函数有怎样的区别?
     比较点普通函数箭头函数具体案例简写/箭头函数如果没有参数,同时函数体的返回值只有一句,则{}和return都可以省略。1、函数简写......
  • 明解入门练习5-4
    #define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>#defineABC5intmain(){intx;inta[ABC];for(x=0;x<ABC;x++){printf("a[%d]:",x);sc......
  • 快速入门音视频技术的方法,有吗?
    最近有读者留言,说“想转行音视频开发,怎么做”,正巧,前几天我还在某乎上,看到有人在问音视频的学习资料,还是个大一的学生。 想说一句:真有眼光。 如今这个时代,想赚钱,一个共识是......