好猫网手机站
入门的java游戏小程序教程(简单java小程序代码)
2022-07-26 12:21:56  浏览:27

入门的java游戏小程序教程(简单java小程序代码)

如果是刚接触或者刚学习java,练习一些基础的算法还是必须的,可以提升思维和语法的使用。

1、输出两个int数中的最大值

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请依次输入两个整数:a,b(以空格隔开)”);

int a = scanner.nextInt();

int b = scanner.nextInt();

int max;

if(a >= b){

max = a;

}else {

max = b;

}

System.out.println(“最大值为”+max);

}

}

}

2、输出三个int数中的最大值

package demo;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请依次输入两个整数:a,b(以空格隔开)”);

int a = scanner.nextInt();

int b = scanner.nextInt();

int c = scanner.nextInt();

scanner.close();

int d=(a>b)?a:b;

int e=(d>c)?d:c;

System.out.println(“最大值为”+e);

if(a>b && a>c){

System.out.println(“最大值为”+a);

}else if(b>c && b>a){

System.out.println(“最大值为”+b);

}else if(c>b && c>a){

System.out.println(“最大值为”+c);

}else{

System.out.println(“出现异常”);

}

}

}

3、编写程序判断某一个年份是否是闰年

package demo;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请输入年份:”);

int year = scanner.nextInt();

if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){

System.out.println(“这个年份是闰年”);

}else{

System.out.println(“这个年份不是闰年”);

}

boolean isLeapYear = (year % 4 ==0 && year % 100 !=0) || year%400 ==0;

String string = isLeapYear?year+”是闰年”:year+”不是闰年”;

System.out.println(string);

}

}

4、完成成绩等级输出程序

如果用户输入的分数正确(0-100),则根据表-1中的规则计算该分数所对应的的级别,并计算结果。

package demo;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请输入分数:”);

double score = scanner.nextDouble();

scanner.close();

if(score < 0 || score >100){

System.out.println(“输入的分数不在0-100之间,不符合要求”);

}else if(score >= 90){

System.out.println(“A”);

}else if(score >= 80){

System.out.println(“B”);

}else if(score >= 60){

System.out.println(“C”);

}else{

System.out.println(“D”);

}

}

}

5、完成命令解析程序

问题:有一个命令解析程序,该程序提供三个功能选型供用户选择,用户选择某功能后,程序在界面上输出用户所选择的的功能名称。程序的交互如图:

package demo;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请选择功能:1.显示全部记录 2.查询登录记录 0.退出”);

int command = scanner.nextInt();

scanner.close();

switch (command) {

case 0:

System.out.println(“欢迎使用”);

break;

case 1:

System.out.println(“显示全部记录……”);

break;

case 2:

System.out.println(“查询登录记录……”);

break;

default:

System.out.println(“输入错误!”);

}

}

}

6、完成收银柜台收款程序

编写一个收银柜台收款程序。根据单价、购买数量以及收款进行计算并输出应收金额和找零;当总价大于或者等于500,享受8折优惠。控制台交互如下:

package demo;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请输入单价(¥):”);

double price = scanner.nextDouble();

System.out.println(“请输入数量:”);

double amount = scanner.nextDouble();

System.out.println(“请输入收款金额:”);

double count = scanner.nextDouble();

double totalMoney = price*amount;

if(totalMoney > 500){

totalMoney = totalMoney*0.8;

}

double change = count – totalMoney;

System.out.println(“应收金额为:”+totalMoney + “找零为:”+change);

}

}

7、java从键盘输入三个整数,实现从小到大排序

package demo;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请输入三个整数,以空格隔开:”);

int a = scanner.nextInt();

int b = scanner.nextInt();

int c = scanner.nextInt();

scanner.close();

System.out.println(“输入的值为:a = ” + a + “, b = ” + b + “, c = ” + c);

if(a > b){

if ( b > c) {

System.out.println(“排序后的值为:” + c + “,” + b + “,” + a);

}else if( c > a){

System.out.println(“排序后的值为:” + b + “,” + a + “,” + c);

}else{

System.out.println(“排序后的值为:” + b + “,” + a + “,” + c);

}

}else{

if(c < a){

System.out.println(“排序后的值为:” + c + “,” + a + “,” + b);

}else if(c > b){

System.out.println(“排序后的值为:” + a + “,” + b + “,” + c);

}else{

System.out.println(“排序后的值为:”+ a + “,” + c + “,” + b);

}

}

}

}

8、计算个人所得税

北京地区的个人所得税计算公式:

纳税额 = (工资薪金所得 – 扣除数)*适用税率 – 速算扣除数

其中,扣除数为3500,适用税率以及速算扣除数如下表所示:

package demo;

import java.util.Scanner;

public class demo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请输入你的税前工资:”);

int salaryBeforeTax = scanner.nextInt();

scanner.close();

int taxSalary = salaryBeforeTax – 3500;

double tax;

tax = taxSalary<0?0.0:

taxSalary<=1500?taxSalary*0.03:

taxSalary<=4500?taxSalary*0.1-105:

taxSalary<=9000?taxSalary*0.2-555:

taxSalary<=35000?taxSalary*0.25-1005:

taxSalary<=55000?taxSalary*0.3-2755:

taxSalary<=80000?taxSalary*0.35-5505:

taxSalary*0.45-13505;

System.out.println(“个人应缴纳税款为:”+tax);

if( taxSalary < 0 ){

tax = 0;

}else if( taxSalary <= 1500){

tax = taxSalary*0.03;

}else if( taxSalary <= 4500){

tax = taxSalary*0.1-105;

}else if( taxSalary <= 9000){

tax = taxSalary*0.2-555;

}else if( taxSalary <= 35000){

tax = taxSalary*0.25-1005;

}else if( taxSalary <= 55000){

tax = taxSalary*0.3-2755;

}else if( taxSalary <= 80000){

tax = taxSalary*0.35-5505;

}else{

tax = taxSalary*0.45-13505;

}

System.out.println(“个人应缴纳税款为:”+tax);

}

}

9、输入年份和月份,输出天数

package demo;

import java.util.Scanner;

public class demo{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“请输入年份:”);

int year = scanner.nextInt();

System.out.println(“请输入月份:”);

int month = scanner.nextInt();

int dayNum = theDayNum(month); //先根据月份得出天数,如果是闰年,对2月份的天数重新获取

if(isLeapYear(year)){

if(month == 2){

dayNum ++; //如果是闰年,2月份增加一天

}

System.out.print(year + “是闰年,”);

}else{

System.out.print(year + “不是闰年,”);

}

System.out.println(year + “年” + month + “月份共有” + dayNum + “天”);

}

public static boolean isLeapYear(int year) {

if((year % 4 ==0 && year % 100 !=0) || year%400 ==0){

return true;

}else{

return false;

}

}

public static int theDayNum(int month) {

switch (month) {

case 1:

return 31;

case 2:

return 28;

case 3:

return 31;

case 4:

return 30;

case 5:

return 31;

case 6:

return 30;

case 7:

return 31;

case 8:

return 31;

case 9:

return 30;

case 10:

return 31;

case 11:

return 30;

case 12:

return 31;

default:

System.out.println(“对不起,您输入的月份有误!”);

return 0;

}

}

}

10、输出九九乘法表

package demo;

public class demo {

public static void main(String[] args) {

//i变量用于控制行数

for(int i = 0; i <= 9; i++) {

//j变量用于控制每行中参与计算的数值

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

System.out.print(j + “*” + i + “=” + i*j + “t”);

}

//每行输出之后需要换行

System.out.println();

}

}

}

11、随机产生一个从0-100之间的整数,判断是否是质数

质数又称素数,是指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数 。

package demo;

import java.util.Random;

public class primeNum {

public static void main(String[] args) {

int num;

Random random = new Random();

num = random.nextInt(100);

System.out.println(“随机产生的数为:” + num);

System.out.println(isPrime(num));

}

public static boolean isPrime(int num) {

if(num < 2) {

return false;

}

if(num == 2) {

return true;

}

if(num % 2 == 0) {

return false;

}

for(int i = 3; i <= Math.sqrt(num); i += 2) {

if(num % i == 0) {

return false;

}

}

return true;

}

}

12、查找数组最小值,并将数组扩容成新数组

package demo;

import java.util.Arrays;

import java.util.Random;

public class copyOf {

public static void main(String[] args) {

int [] arr = new int[10];

//随机产生10个 0-100之间的整数

Random random = new Random();

for(int i = 0; i < 10; i ++) {

arr[i] = random.nextInt(100);

}

//打印数组的内容

System.out.println(“随机产生的数组为:” + Arrays.toString(arr));

//查找最小的值

int min = arr[0];

for(int j = 1; j < 10; j ++) {

if(min > arr[j]) {

min = arr[j];

}

}

System.out.println(“该数组最小的值为:” + min);

//扩容,将最小值存在扩容之后的第一个

int [] newArr = Arrays.copyOf(arr, 11);

//从后往前遍历,将前面的值赋给后面的值,然后将第一个的值赋为最小值min

for(int k = newArr.length-1; k >=1; k –) {

newArr[k] = newArr[k-1];

}

//将第一个的值赋为最小值min

newArr[0] = min;

//打印数组的内容

System.out.println(“扩容之后的数组为:”+ Arrays.toString(newArr));

}

}