2024年1月22日发(作者:)
1百分制分数到等级分数
package pm;
public class SwitchTest {
//编写程序,实现从百分制分数到等级分数的转换
//
//>=90 A
// 80~89 B
// 70~79 C
// 60~69 D
// <60 E
public static void main(String[] args) {
int s=87;
switch(s/10){
case 10 :n("A");break;
case 9 :n("A");break;
case 8 :n("B");break;
case 7 :n("c");break;
case 6 :n("D");break;
default :n("E");break;
}
}
}
2成法口诀阵形
package pm;
public class SwitchTest{
public static void main(String[] args){
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
(j+"*"+i+"="+(i*j)+"t");
}
n();
}
}
}
3华氏和摄氏的转换法
package pm;
import r;
public class SwitchTest {
public static void main(String[] args) {
Scanner sc=new Scanner();
while (true) {
n("请输入要转换的温度类型:C 或 F");
String s = ().trim();
if ("c".equalsIgnoreCase(s)) {
//做摄氏向华摄的转换
n("请输入要转换摄氏的温度:..");
double db = uble();
double db2 = (db * 9 / 5) + 32;
n("对应的华氏温度:" + db2 + "F");
} else if ("f".equalsIgnoreCase(s)) {
//做华摄向摄氏的转换
n("请输入要转换华氏的温度:..");
double db = uble();
double db2 = (db - 32) * 5 / 9;
n("对应的摄氏温度:" + (db2) + "C");
}else if("exit".equalsIgnoreCase(s)){
break;
}
}
}
}
package pm;
import r;
public class SwitchTest{
public static void main(String[] args) {
Scanner sc=new Scanner();
boolean flag=true;
while (flag) {
n("请输入要转换的温度,如:50c或100f");
String str = ne().trim();
if (th("c") || th("C")) {
//做摄氏向华摄的转换 30c
String st = ing(0, () - 1);
double db = ouble(st);//[0,2)
//2 double db=f(st).doubleValue();
double db2 = (db * 9 / 5) + 32;
n("对应的华氏温度:" + db2 + "F");
} else if (th("f") || th("F")) {
//做华摄向摄氏的转换
String st = ing(0, () - 1);
double db = ouble(st);//[0,2)
//2 double db=f(st).doubleValue();
double db2 = (db - 32) * 5 / 9;
n("对应的摄氏温度:" + (db2) + "C");
}else if("exit".equalsIgnoreCase(str)){
flag=false;
}
}
}
}
4三个数的最大数
package pm;
public class SwitchTest {
public static void main(String[] args) {
int a=1,b=2,c=3,d=0;
d=a>b?a:b;
d=a>b?(a>c?a:c):(b>c?b:c);
n("最多数为:"+d);
}
}
5简单计算器的小程序
package one;
import Layout;
import yout;
import Event;
import Listener;
import n;
import ;
import ;
import ield;
public class Jsq implements ActionListener {
private JFrame frame;
private JButton[] bus;
private JTextField jtx;
private JButton bu;
private char[] strs;
private String d_one = "";
private String operator;
public static void main(String[] args) {
new Jsq();
}
/* 利用构造进行实例化 */
public Jsq() {
frame = new JFrame("计算器");
jtx = new JTextField(14);
bus = new JButton[16];
strs = "789/456*123-0.+=".toCharArray();
for (int i = 0; i < 16; i++) {
bus[i] = new JButton(strs[i] + "");
bus[i].addActionListener(this);
}
bu = new JButton("C");
ionListener(this);
init();
}
/* GUI 初始化 */
public void init() {
JPanel jp1 = new JPanel();
(jtx);
(bu);
(jp1, );
}
/* 事件的处理 */
public void actionPerformed(ActionEvent arg0) {
/*获取输入字符*/
String conn = ionCommand();
/*清除计算器内容*/
if ("C".equals(conn)) {
JPanel jp2 = new JPanel();
out(new GridLayout(4, 4));
for (int i = 0; i < 16; i++) {
(bus[i]);
}
(jp2, );
();
ation(300, 400);
ible(true);
aultCloseOperation(_ON_CLOSE);
d_one = "";
operator = "";
t("");
return;
}
/*暂未实现该功能*/
if(".".equals(conn)){
return;
}
/*记录运算符,保存运算数字*/
if (("+-*/".indexOf(conn)) != -1) {
if("".equals(d_one) && "".equals(t())) return;
d_one = t();
operator = conn;
t("");
return;
}
/*计算结果*/
if ("=".equals(conn)) {
if("".equals(d_one) && "".equals(t())) return;
double db = 0;
if ("+".equals(operator)) {
db = ouble(d_one)
+ ouble(t());
t(db + "");
}
if ("-".equals(operator)) {
db = ouble(d_one)
- ouble(t());
t(db + "");
}
if ("*".equals(operator)) {
db = ouble(d_one)
* ouble(t());
t(db + "");
}
if ("/".equals(operator)) {
db = ouble(d_one)
/ ouble(t());
t(db + "");
}
d_one = db + "";
return;
}
//界面显示
t(t() + conn);
}
}
6三角形图案
package pm;
public class SwitchTest{
public static void main(String[] args){
int n=5;
for(int i=0;i<=n;i++){
for(int j=0;j (" "); } for(int k=0;k<2*i-1;k++){ ("*"); } n(); } } } 7输出输入的姓名 package pm; import r; public class SwitchTest{ public static void main(String[] args){ String name=null; Scanner sca=new Scanner (); char firstChar; do{ n("please input your name"); name=ne(); firstChar=(0); }while(!(firstChar>='a' && firstChar<='z' || firstChar>='A' && firstChar<='Z')); n("OK! your name is:"+name); } } 8一小时倒计时小程序 package pm; import ; import ; import ; public class SwitchTest { private JFrame frame; private JLabel jl1; private JLabel jl2; private JLabel jl3; /*主方法*/ public static void main(String[] args) { new SwitchTest().getTime(); } /*倒计时的主要代码块*/ private void getTime(){ long time=1*3600; long hour =0 ; long minute =0 ; long seconds=0; while(time>=0){ hour=time/3600; minute=(time-hour*3600)/60; seconds=time-hour*3600-minute*60; t(hour+"时"); t(minute+"分"); t(seconds+"秒"); try { (1000); } catch (InterruptedException e) { tackTrace(); } time--; } } /*构造 实现界面的开发 GUI */ public SwitchTest(){ frame = new JFrame("倒计时"); jl1 = new JLabel(); jl2 = new JLabel(); jl3 = new JLabel(); init(); } /*组件的装配*/ private void init(){ JPanel jp=new JPanel(); (jl1); (jl2); (jl3); (jp); ible(true); ation(300,400); e(300, 200); aultCloseOperation(_ON_CLOSE); } } 9棋盘图案 public class Sjx{ public static void main(String[] args){ int SIZE=19; for(int i=0;i if(i<10){ (" "+i);//两个空格 }else{ (" "+(char)(i+'a'-10));//两个空格 } } n(); // (i<10?(char)(i+'0'):(char)(i+'a'-10)); for(int i=0;i if(i<10){ (i+" ");//一个空格 }else{ ((char)(i+'a'-10)+" ");//一个空格 } for(int j=0;j ("*"+" ");//两个空格 } n(); } } } 10数组输出唐诗 package day04; public class ArrayTest { public static void main(String[] args){ char[][] arr=new char[4][7]; String s="朝辞白帝彩云间千里江陵一日还两岸猿声啼不住轻舟已过万重山"; for(int i=0;i<;i++){ for(int j=0;j arr[i][j]=(i*7+j); } } for(int i=0;i for(int j=0;j<;j++){ (arr[-j-1][i]+" "); } n(); } } } 11找出满足条件的最小数 package day02; public class Fangk{ public static void main(String[] args){ // for(int i=1000;i<=9999;i++){ // int q=i/1000; // int b=i/100%10; // int s=i/10%10; // int g=i%10; // if(q>b && s>g && q+g==b+s && b%2!=0){ // n("The Min Number is:"+i); // break; // } // } loop1: for(int q=1;q<=9;q++){ loop2: for(int b=0;b<=9;b++){ if(b%2==0){ continue loop2; } for(int s=0;s<=9;s++){ for(int g=0;g<=9;g++){ if(q>b && s>g && q+g==b+s){ n("The is:"+(q*1000+b*100+s*10+g)); break loop1; } } } } } } } Min Number 12判断一个数是否是素数 package day02; public class Fangk{ public static void main(String[] args){ int num=14; boolean flag=true; for(int i=2;i<=num/2;i++){ if(num%i==0){ flag=false; break; } } if(flag){ n(num+" is a prime number!"); }else{ n(num+" is not a prime number!"); } } } ////////////////////////////////////////////////////////////////////// package day04; import r; public class A1{ public static void main(String[] args){ int n; Scanner sca=new Scanner(); n("please input a number:"); n=t(); if(isPrimeNumber(n)){ n(n+"is a prime number!"); }else{ n(n+"is not a prime number!"); } } public static boolean isPrimeNumber(int n){ for(int i=2;i<=n/2;i++){ if(n%i==0){ return false; } } return true; } } 13一个数倒序排列 package day02; public class Daoxu{ public static void main(String[] args){ int olddata=3758; int newdata=0; while(olddata!=0){ for(int i=0;i<4;i++){ newdata=newdata*10+olddata%10; olddata=olddata/10; } n("newdata="+newdata); } } } 14将一个整数以二进制输出 package day04; import r; public class ArrayTest { public static void main(String[] args){ int n; Scanner s=new Scanner(); n("please input a number:"); n=t(); for(int i=31;i>=0;i--){ if((n&(1< ("1"); }else{ ("0"); } if((32-i)%8==0){ (" "); } } } } 15矩形图案 package day02; public class Fangk { public static void main(String[] args){ int m=5,n=6; for(int i=0;i ("* "); } n(); for(int i=0;i ("* "); for(int j=0;j (" "); } ("*"); n(); } for(int i=0;i ("* "); } } } 16猜数字 package day02; import r; public class Csz { public static void main(String[] args) { Scanner s = new Scanner(); int num = (int) (() * 1000); int m=0; for(int i=9;i>=0;i--){ n("please input your number! "); m=t(); if(m>num){ n("Too large!"); }else if(m n("Too small!"); }else{ n("You are right!"); break; } if(i>0){ n("还有"+i+"次机会!"); } } if( m!=num){ n("下次再来吧!"); } } } anager package hotel; import r; public class HotelManager { private static String[][] rooms;// 表示房间 public static void main(String[] args) { rooms = new String[10][12]; String comm;// 表示用户输入的命令 for (int i = 0; i < ; i++) { for (int j = 0; j < rooms[0].length; j++) { rooms[i][j] = "EMPTY"; } } // while (true) { n("请输入命令:"); Scanner sca = new Scanner(); (); comm = (); if ("search".equalsIgnoreCase(comm)) { search(); } else if ("in".equalsIgnoreCase(comm)) { int roomNo = t(); String name = (); in(roomNo, name); } else if ("out".equalsIgnoreCase(comm)) { int roomNo = t(); out(roomNo); } else if ("exit".equalsIgnoreCase(comm)) { n("程序退出..."); break; } else { n("命令输入错误,请重新输入:"); } } } private static void out(int roomNo) { if("EMPTY".equals(rooms[(roomNo/100)-1][(roomNo%100)-1])){ n("该房间没有客人入住,退房失败!"); } } return; } rooms[(roomNo/100)-1][(roomNo%100)-1]="EMPTY"; n(roomNo+"退房成功!"); private static void in(int roomNo, String name) { if(!"EMPTY".equals(rooms[(roomNo/100)-1][(roomNo%100)-1])){ n("该房间已经有客人入住!"); return; } rooms[(roomNo/100)-1][(roomNo%100)-1]=name; n(name+"成功入住"+roomNo+"房间!"); } private static void search() { for (int i = 0; i < ; i++) { //打印房间号 for (int j = 0; j < rooms[0].length; j++) { if (j + 1 < 10) { (i + 1 + "0" + (j + 1) + " "); } else { (i + 1 + "" + (j + 1) + " "); } } //打印房间状态 n(); for (int j = 0; j < rooms[0].length; j++) { (rooms[i][j] + " "); } n(); } } tManager package t_manager; import r; public class StudentManager { static int[][] scores=new int[6][5]; static String[] students={"zhangsan","lisi","wangwu","zhaoliu","qianqi","liuba"}; static String[] courses={"corejava","jdbc","servlet","jsp","ejb"}; public static void main(String[] args) { for(int i=0;i<;i++){ for(int j=0;j scores[i][j]=(int)(()*100); } } Scanner s=new Scanner(); String comm; while(true){ n("请输入命令:"); comm=(); if(("AVG")){ String para=(); avg(para); }else if(("SORT")){ String course=(); sort(course); }else if(("GET")){ String student=(); String course=(); get(student,course); }else if(("EXIT")){ break; }else{ n("命令格式不正确,请重新输入!"); } } }//main() end! public static void avg(String para){ int sIndex=-1; // int cIndex=-1; for(int i=0;i<;i++){ if(students[i].equals(para)){ sIndex=i; } } if(sIndex==-1){ for(int i=0;i<;i++){ if(courses[i].equals(para)){ cIndex=i; } } } if(sIndex==-1 && cIndex==-1){ n("找不到学生或课程!"); return; } double avg=0.0; if(sIndex!=-1){ for(int i=0;i avg+=scores[sIndex][i]; } avg/=scores[sIndex].length; n("学生:"+para+"的平均分是:"+avg); }else{ for(int i=0;i<;i++){ avg+=scores[i][cIndex]; } avg/=; n("课程:"+para+"的平均分是:"+avg); } } public static void sort(String course){ int[] courseScore=new int[]; if(("sum")){//如果求总分的排名 //求出每个学生的总分,将成绩存放在courseScore数组中 for(int i=0;i<;i++){ int studentSum=0; for(int j=0;j studentSum+=scores[i][j]; } courseScore[i]=studentSum; } }else{//如果不是求总分排名 int cIndex=-1; for(int i=0;i<;i++){//找到这门课程的下标 if(courses[i].equals(course)){ cIndex=i; } } if(cIndex!=-1){//如果是一门有效的课程 //把scores数组中这一列的值放到courseScore数组中! for(int i=0;i<;i++){ courseScore[i]=scores[i][cIndex]; } }else{//如果不是一门有效的课程 n("课程名不正确,请重新输入!"); return; } } String[] studentCopy=new String[]; opy(students, 0, studentCopy, 0, ); for(int i=0;i<-1;i++){ for(int j=i+1;j<;j++){ if(courseScore[i] int temp=courseScore[i]; courseScore[i]=courseScore[j]; courseScore[j]=temp; String stemp=studentCopy[i]; studentCopy[i]=studentCopy[j]; studentCopy[j]=stemp; } } } int order=1; n("名次t学生t成绩"); for(int i=0;i<;i++){ if(i!=0 && courseScore[i]==courseScore[i-1]){ order--; }else{ order=i+1; } (order+"t"); (studentCopy[i]+"t"); n(courseScore[i]); order++; } } public static void get(String student,String course){ int sIndex=-1; int cIndex=-1; for(int i=0;i<;i++){ if(students[i].equals(student)){ sIndex=i; } } if(sIndex==-1){ n("没有这个学生:"+student); return; } if(("sum")){//如果求总分 int studentSum=0; for(int j=0;j studentSum+=scores[sIndex][j]; } n(student+"学生的总分为:"+studentSum); return; } for(int i=0;i<;i++){ if(courses[i].equals(course)){ cIndex=i; } } if(cIndex==-1){ n("没有这门课程:"+course); return; } n(student+"学生的"+course+"课程的成绩为 : "+scores[sIndex][cIndex]); } } package hotel; import r; /** * 首先在程序第一次运行的时候,构建出棋盘,切以后 * 不能再从新构建,知道结束,所以将其放到静态代码块中。 * @author zhz * */ public class Five { //由于矩阵要保存输入的信息,所以要设置成全局变量,整个程序就一个 private static String[][] five=new String[17][17]; static{ //构建并打印棋盘 drawFive(); } public static void main(String[] args) { //请白棋下子 while(true){ n("请白棋下子"); Point p1=pleaseIn(); if(!five[nt(())][nt(())].equals("*")){ n("无效的位置,视为自动放弃"); } runPoint(p1,"white"); gameover(p1); n("请黑棋下子"); Point p2=pleaseIn(); if(!five[nt(())][nt(())].equals("*")){ n("无效的位置,视为自动放弃"); } runPoint(p2,"black"); gameover(p2); } } //根据用户的输入信息,对棋盘的坐标进行赋值,白棋@ 黑棋O,并且将当前棋盘的信息输出 private static void runPoint(Point p,String str){ //根据px py来获取行和列的坐标 int x=nt(()),y=nt(()); if(("white")){ five[x][y]="@"; }else{ five[x][y]="O"; } outFive(); } //用户输入要放棋子的坐标 并且自动转换为具体的坐标 private static Point pleaseIn(){ Scanner sc=new Scanner(); Point p=new Point((),()); String px=(); String py=(); //根据px py来获取行和列的坐标 int x=0,y=0; for(int i=1;i<;i++){ if(five[i][0].equals(px)){ x=i; } if(five[0][i].equals(py)){ y=i; } } return new Point(x+"",y+""); } /**判断胜负,根据当前点,开始计算,如果有五个点连成线,则盛,程序提示结束,并显示输赢 * * @param p */ public static void gameover(Point p) { int x=nt(()),y=nt(()); int c1 = 0, c2 = 0, c3 = 0, c4 = 0; boolean l1 = true, l2 = true, l3 = true, l4 = true, l5 = true, l6 = true, l7 = true, l8 = true; for (int i = 1; i <5 ; i++) { // 竖直向上 if (y - i >= 1 && five[x][y - i].equals(five[x][y]) && l1) { c1++; } else { l1 = false; } // 竖直向下 if (y + i <= 16 && five[x][y + i].equals(five[x][y]) && l2) { c1++; } else { l2 = false; } // 水平向左 if (x - i >= 1 && five[x - i][y].equals(five[x][y]) && l3) { c2++; } else { l3 = false; } // 水平向右 if (x + i <= 16 && five[x + i][y].equals(five[x][y]) && l4) { c2++; } else { l4 = false; } // 斜向左上 if (x - i >= 1 && y - i >= 1 && five[x - i][y - i].equals(five[x][y]) && l5) { c3++; } else { l5 = false; } // 斜向右下 if (x + i <= 16 && y + i <= 16 && five[x + i][y + i].equals(five[x][y]) && l6) { c3++; } else { l6 = false; } // 斜向左下 if (x - i >= 1 && y + i <= 16 && five[x - i][y + i].equals(five[x][y]) && l7) { c4++; } else { l7 = false; } // 斜向右上 if (x + i <= 16 && y - i >= 1 && five[x + i][y - i].equals(five[x][y]) && l8) { c4++; } else { l8 = false; } } if (c1 >= 4 || c2 >= 4 || c3 >= 4 || c4 >= 4) { if (five[x][y].equals("1")) { n("黑棋获胜!"); (0); } else { n("白棋获胜!"); (0); } } } //构造棋盘 private static void drawFive(){ //构建棋盘边框 String[] sr=new ","0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; five[0]=sr; String[]{" for(int i=0;i<;i++){ five[i][0]=sr[i]; } //构建棋盘内容 for(int i=1;i<;i++){ for(int j=1;j five[i][j]="*"; } } //打印棋盘 outFive(); } //打印棋盘 private static void outFive(){ for(int i=0;i<;i++){ for(int j=0;j (five[i][j]+" "); } n(); } } } me package me; import r; public class CardGame { static int[] twoHandsCards=new int[6]; static int userAMoney=0; static int userBMoney=0; public static void main(String[] args) { Scanner s=new Scanner(); n("请输入总赌本:"); userAMoney=t(); userBMoney=userAMoney; int currentMoney=0; while(userAMoney>0 && userBMoney>0){ n("请输入本局赌注:"); currentMoney=t(); if(currentMoney<=0 || currentMoney>userAMoney || currentMoney>userBMoney){ n("赌注不正确,请重新输入!"); continue; } dispatchCards(); printCards(twoHandsCards); if(compare()>0){ userAMoney+=currentMoney; userBMoney-=currentMoney; n("玩家获胜!"); }else if(compare()<0){ userAMoney-=currentMoney; userBMoney+=currentMoney; n("电脑获胜!"); }else{ n("平局!"); } n("玩家赌本:"+userAMoney+";电脑赌本:"+userBMoney); } } public static void dispatchCards(){ int i=0; while(i<){ int h=(int)(()*4)+1; int d=(int)(()*13)+2; int j=0; for(;j if(twoHandsCards[j]==h*100+d){ break; } } if(j continue; }else{ twoHandsCards[i++]=h*100+d; } } } public static void printCards(int[] oneHandCards){ for(int i=0;i<;i++){ switch(oneHandCards[i]/100){ case 1:("黑");break; case 2:("红");break; case 3:("梅");break; case 4:("方");break; } if(oneHandCards[i]%100<=10){ (oneHandCards[i]%100); }else{ switch(oneHandCards[i]%100){ case 11: ("J");break; case 12: ("Q");break; case 13: ("K");break; case 14: ("A");break; } } (" "); } n(); } public static int compare(){ int[] aCards=new int[3]; int[] bCards=new int[3]; for(int i=0;i<3;i++){ aCards[i]=twoHandsCards[i]; } for(int i=3;i<6;i++){ bCards[i-3]=twoHandsCards[i]; } return fromCardsToNumber(aCards)-fromCardsToNumber(bCards); } public static int fromCardsToNumber(int[] oneHandCards){ for(int i=0;i<-1;i++){ for(int j=i+1;j<;j++){ if(oneHandCards[i]%100 int temp=oneHandCards[i]; oneHandCards[i]=oneHandCards[j]; oneHandCards[j]=temp; } } } int num=0; if(oneHandCards[0]%100==oneHandCards[1]%100 && oneHandCards[1]%100==oneHandCards[2]%100){ num=6*1000000+oneHandCards[0]%100*10000+oneHandCards[1]%100*100+oneHandCards[2]%100; }else if(oneHandCards[0]/100==oneHandCards[1]/100 && oneHandCards[1]/100==oneHandCards[2]/100 && oneHandCards[0]%100==oneHandCards[1]%100+1 && oneHandCards[1]%100==oneHandCards[2]%100+1 ){ num=5*1000000+oneHandCards[0]%100*10000+oneHandCards[1]%100*100+oneHandCards[2]%100; }else if(oneHandCards[0]/100==oneHandCards[1]/100 && oneHandCards[1]/100==oneHandCards[2]/100){ num=4*1000000+oneHandCards[0]%100*10000+oneHandCards[1]%100*100+oneHandCards[2]%100; }else if(oneHandCards[0]%100==oneHandCards[1]%100+1 && oneHandCards[1]%100==oneHandCards[2]%100+1){ num=3*1000000+oneHandCards[0]%100*10000+oneHandCards[1]%100*100+oneHandCards[2]%100; }else if(oneHandCards[0]%100==oneHandCards[1]%100 ){ num=2*1000000+oneHandCards[0]%100*10000+oneHandCards[1]%100*100+oneHandCards[2]%100; }else if(oneHandCards[1]%100==oneHandCards[2]%100){ num=2*1000000+oneHandCards[1]%100*10000+oneHandCards[2]%100*100+oneHandCards[0]%100; }else{ num=1*1000000+oneHandCards[0]%100*10000+oneHandCards[1]%100*100+oneHandCar ds[2]%100; } return num; } }
发布者:admin,转转请注明出处:http://www.yc00.com/news/1705915564a1428029.html
评论列表(0条)