java入门练习题(if,switch,while循环)

import java.util.Scanner;

class Demo1 {
	public static void main(String[] args) {
		/*
		1. 表达式(立方)	
		编写程序,计算用户输入数据的立方
		*/
		/*
				需求判断
		1. 首先用户需手动输入,需导入Scanner的包
		2.需要两个整型变量,一个用来接收用户输入数据
			另一个用来计算立方,
		*/
		int cube = 0;
		int total = 0;
		
		Scanner sc = new Scanner(System.in);
		//提示输入
		System.out.println("请输入立方的值");
		cube = sc.nextInt();
		//计算长宽高相乘结果,
		total = cube * cube * cube;
		System.out.println("这个立方为:" + total);
	}
}

import java.util.Scanner;

class Demo2 {
	public static void main(String[] args) {
		
		/*
		2. 表达式(取值操作)	
        输入4个数,若第一个数第二个数相等,第三个数和第四个数的
        和与第一个数和第二个数的和相等,输出1,否则输出0
		*/
		
		/*
					条件分析
		1. 需要用到Scanner的包
		2. 需要用到分支判断
		*/
		int num1 = 0;
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入四个数");
		num1 = sc.nextInt();
		num2 = sc.nextInt();
		num3 = sc.nextInt();
		num4 = sc.nextInt();
		if (num1 == num2 && (num1 + num2) == (num3 + num4)) {
			System.out.println("1");
		} else {
			System.out.println("0");
		}
		
	}
}

import java.util.Scanner;

class Demo3 {
	public static void main(String[] args) {
		
		/*
		    3. 流程控制(数值比较1)	
    	定义两个整型变量x,y,从键盘初始化变量值,判断两个变量的大小,将较大的值赋
    	给变量max,将max输出,注意输入使用Scanner输入
		*/
		int x = 0;
		int y = 0;
		int max = 0;
		Scanner sc = new Scanner(System.in);
		System.out.println("请先后输入两个数");
		x = sc.nextInt();
		y = sc.nextInt();
		if (x > y) {
			System.out.println("max的值是:" + (max = x));
		} else {
			System.out.println("max的值是:" + (max = y));
		}
	}
}

import java.util.Scanner;

class Demo4 {
	public static void main(String[] args) {
		
		/*
		 4. 流程控制(数值比较2)	
    	定义三个整型变量x,y,z,从键盘初始化变量值,判断三个变量的大小,将较大的值
    	赋给变量max,将max输出,注意输入使用Scanner输入
		*/
		int x = 0;
		int y = 0;
		int z = 0;
		int max = 0;
		Scanner sc = new Scanner(System.in);
		System.out.println("请先后输入三个数值");
		x = sc.nextInt();
		y = sc.nextInt();
		z = sc.nextInt();
		if (x >= y && x >= z) {
			System.out.println("max的值为" + (max = x));
		} else if (y > x && y > z) {
			System.out.println("max的值为" + (max = y));
		} else {
			System.out.println("max的值为" + (max = z));
		}
	}
}

import java.util.Scanner;

class Demo5 {
	public static void main(String[] args) {
		
		/*
		 5. 流程控制(月份天数判断)	
    	输入一个月数,然后输出对应月份有多少天(不考虑闰年),将天数输出,注意输入
    	使用Scanner输入 
    	比如:
       		输入 6  输出为30
        	输入 2  输出为28
		*/
		
		int month = 0;
		int a = 30;
		int b = 31;
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入几月份");
		month = sc.nextInt();
		switch (month) {
			case 1 :
			case 3 :
			case 5 :
			case 7 :
			case 8 :
			case 10 :
			case 12 :
				System.out.println(month + "月有31天");
				break;
			case 4 :
			case 6 :
			case 9 :
			case 11 :
				System.out.println(month + "月有30天");
				break;
			case 2 :
				System.out.println(month + "月有28天");
				break;
			default :
				System.out.println("输入月份错误");
				break;
		}
	}
}

class Demo6 {
	public static void main(String[] args) {
		
		/*
		6. 完成一个9*9乘法表
		*/
		
		//外循环控制行数,内循环控制列数。
		
		int i = 1;
		int j = 1;
		int total = 0;
		while (i <= 9) {
			j = 1;
			while (j <= i) {
				
				System.out.print(i + "*" + j + "=" + i * j + " ");
				
				j ++;
				
			}

			
			i ++ ;
			System.out.println();
		}
		System.out.println("over... ...");
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值