所有的语言实现起来都是类似的
都是简单的示例,新手练手用的大佬勿喷,点击标题即可展开
石头剪刀布
import java.util.Scanner;
public class caiquan {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] r = {"石头", "剪刀", "布"};
int computerChoice = (int) (Math.random() * 3); // 随机生成计算机出拳
System.out.println("石头剪刀布游戏开始!");
System.out.println("请出拳:0 - 石头,1 - 剪刀,2 - 布");
int userChoice = scanner.nextInt();
if (userChoice < 0 || userChoice > 2) {
System.out.println("无效的输入,请输入0、1或2。");
return;
}
System.out.println("你出了:" + r[userChoice]);
System.out.println("我出了:" + r[computerChoice]);
if (userChoice == computerChoice) {
System.out.println("平局!");
} else if (userChoice == 0 && computerChoice == 1
|| userChoice == 1 && computerChoice == 2
|| userChoice == 2 && computerChoice == 0) {
System.out.println("你赢了!");
} else {
System.out.println("你输了!");
}
}
}
打印正方形和圆形
import java.util.Scanner;
public class Graph {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请选择要打印的图形(1.圆形 2.正方形):");
int choice = scanner.nextInt(); // 获取用户选择
switch (choice) {
case 1 -> {
System.out.print("请输入圆的半径:");
int r = scanner.nextInt(); // 获取圆的半径
round(r); // 打印圆形
}
case 2 -> {
System.out.print("请输入正方形的边长:");
int s = scanner.nextInt(); // 获取正方形的边长
Square(s); // 打印正方形
}
default -> System.out.println("无效的选择!");
}
scanner.close();
}
public static void round(int r){
//i代表每一行
for(int i=0;i<=2*r;i+=2) {
//圆上点到竖着半径的距离
int y = (int) Math.round(Math.sqrt(Math.pow(r,2)- Math.pow(r-i,2)));
//圆上点到外切正方形边的距离
int x = r-y;
//先打印出正方形左边边到圆上点的空白部位
for(int j=0;j<x;j++) {
System.out.print(" ");
}
//打印出左边的圆上点
System.out.print("*");
//打印出中间空白部分
for(int k=0;k<2*(r-x);k++) {
System.out.print(" ");
}
//打印出右边的圆上点 右边空白不要打直接换行
System.out.println("*");
}
}
// 打印正方形
public static void Square(int s) {
for (int i = 1; i <= s; i++) {
for (int j = 1; j <= s; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
冒泡排序
public class arr {
public static void main(String[] args) {
int[] arr = {10, 2, 3, 4, 5,};
ot(arr);
System.out.println("冒泡排序前");
px(arr);
ot(arr);
System.out.println("冒泡排序后");
}
public static void ot(int[] arr) {
for (int j : arr) {
System.out.print(j+" - ");
}
}
public static void px(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
int max = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = max;
}
}
}
}
}
银行存取钱
import java.util.Scanner;
public class Money {
int moneys =500;
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Money M =new Money();
while (true) {
System.out.println("1.存钱功能");
System.out.println("2.取钱功能");
System.out.println("3.查钱功能");
System.out.println("4.退出功能");
System.out.println("请输入您的操作:");
try {
int num = Integer.parseInt(sc.next());
switch (num) {
case 1 -> M.saveMoney();
case 2 -> M.getMoney();
case 3 -> M.checkMoney();
case 4 -> {
return;
}
default -> System.out.println("无效的操作,请重新输入。");
}
} catch (NumberFormatException e) {
System.out.println("输入错误,请输入数字。");
}
System.out.println("------------------");
}
}
public void saveMoney(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入您需要存入的金额:");
int num =Integer.parseInt(sc.next());
if(num>0){
this.moneys =num+this.moneys;
System.out.println("存入成功,您当前的金额为:"+ this.moneys);
}
}
public void getMoney(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入您需要取出的金额:");
int num =Integer.parseInt(sc.next());
if(num > this.moneys){
System.out.println("取出失败,您当前的金额不足:"+ this.moneys);
}else {
this.moneys =this.moneys -num;
System.out.println("取出成功,您当前的金额为:"+ this.moneys);
}
}
public void checkMoney(){
System.out.println("查询成功,您当前的金额为:"+ this.moneys);
}
}
学生投票
import java.util.Scanner;
public class toupiao {
static String[] name_arr = new String[10]; // 候选人名字数组
static int[] vote_arr = new int[10]; // 候选人得票数组
static int vote_count = 0; // 投票数计数器
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 投票");
System.out.println("2. 查询结果");
System.out.println("3. 结束投票");
System.out.println("------------------");
int choice = sc.nextInt();
switch (choice) {
case 1 -> vote();
case 2 -> check();
case 3 -> {
System.out.println("结束投票");
return;
}
default -> System.out.println("无效操作,请重新选择");
}
}
}
public static void vote() {
System.out.println("投票功能");
Scanner sc = new Scanner(System.in);
System.out.println("请输入您需要投票的名字:");
String name = sc.next();
for (int i = 0; i < name_arr.length; i++) {
if (name_arr[i] == null) { // 如果该位置为空,则说明还没有投过票
System.out.println("投票成功!");
name_arr[i] = name;
vote_arr[i]++;
vote_count++;
return;
} else if (name_arr[i].equals(name)) { // 如果该位置不为空,且已经投过票给该人
System.out.println("请勿重复投票!");
return;
}
}
System.out.println("投票人数已达上限!");
}
public static void check() {
System.out.println("查询结果");
for (int i = 0; i < name_arr.length; i++) {
if (name_arr[i] != null) { // 如果该位置不为空,说明该候选人有得票
System.out.println(name_arr[i] + ":" + vote_arr[i] + "票");
}
}
System.out.println("总共有" + vote_count + "人投票");
}
}
THE END
暂无评论内容