1.输入一个年份月份,求天数 public static void main(String[] args) {
System.out.println(“请输入你的年份”); Scanner sc1 = new Scanner(System.in); int year = sc1.nextInt(); System.out.println(“请输入你的月份”); int month = sc1.nextInt(); if(year>=0 && (month>=0 && month <=12)){ if(year%4 0 && year0 !=0 ||year@0 0){ if(month == 1||month 3||month == 5||month == 7||month == 8||month 10||month == 12){ System.out.println(year+“年”+month+“有31天”); }else if(month == 2){ System.out.println(year+“年”+month+“有29天”); }else{ System.out.println(year+“年”+month+“有30天”); } } else{ if(month == 1||month == 3||month5||month7||month8||month10||month==12){ System.out.println(year+“年”+month+“有31天”); }else if(month == 2){ System.out.println(year+“年”+month+“有28天”); }else{ System.out.println(year+“年”+month+“有30天”); System.out.println(year); } } } else{ System.out.println(“输入有误”); }
Switch 方法: public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(“请输入年份”); Scanner sc = new Scanner(System.in); int year = sc.nextInt(); System.out.println(“请输入月份”); int month = sc.nextInt(); switch(month){ case 2:if(year%4 == 0&&year0 !=100 ||year@0 == 0){ System.out.println(year+“年”+month+“有29天”);//day= year%4 == 0&&year0 !=0||year@0 == 0?29:28; }else{ System.out.println(year+“年”+month+“有28天”); } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12:System.out.println(year+“年”+month+“有31天”);break; case 4: case 6: case 9: case 11:System.out.println(year+“年”+month+“有30天”);break; }
}2.While 发牌 public static void main(String[] args) { // TODO Auto-generated method stub String [] player = {“周星驰”,“周润发”,“小沈阳”}; String [] cards = {“黑桃A”,“黑桃K”,“黑桃Q”,“红桃2”,“红桃3”,“红桃4”,“方片J”,“方片k”,“方片5”}; int index = 0; while(index <cards.length){ String card = cards[index]; String people = player[index++%3]; System.out.print(people+":"+card+" "); if(index % 3==0){ System.out.println(); } } 3. While 猜数字 public static void main(String[] args) { // TODO Auto-generated method stub int boss = 500;//int number =(int)(Math.random*1000); System.out.println(“请开始猜数字:”); Scanner sc = new Scanner(System.in); int guess = sc.nextInt(); while(guess!= boss){ if(guess<boss){ System.out.println(“猜小了”); } else{ System.out.println(“猜大发了”); } System.out.println(“再给你一次机会:”); guess = sc.nextInt(); } if(guess == boss){ System.out.println(“恭喜通关”); }
}Do-While 猜数字 public static void main(String[] args) { // TODO Auto-generated method stub int boss=(int)(Math.random()*1000); System.out.println(“boss=”+boss); System.out.println(“请输入一个整数:”); Scanner sc = new Scanner(System.in); int guess ; do{ guess = sc.nextInt(); if(guess < boss){ System.out.println(“输入过小”); }else{ System.out.println(“输入过大”); } System.out.println(“请重新输入:”); guess= sc.nextInt(); }while(guess != boss); if(guess == boss){ System.out.println(“恭喜恭喜!!!!!!”); } } 4.switch 表达式的取值:byte,short , int ,char ;;;;;JDK5加入了枚举 JDK7加入了String
package cn.ll.day02;
import java.awt.Color; import java.awt.Font; import java.awt.Graphics;
import javax.swing.JPanel;
public class MyJpane extends JPanel implements Runnable{ int x1=300; int y1=300; int y2=390; int x4=260; int x3=400; int x5=450; int x6=40; int x7=600; //钱----存储所有的x和y坐标 int[] x12=new int[50]; int[] y12=new int[50];
public MyJpane(){ for(int i =0;i<x12.length;i++){ x12[i]= (int)(Math.random()*1400); y12[i]=(int)(Math.random()*800); } //创建线程 Thread t=new Thread(this); t.start(); public void paint(Graphics g){ //g:画笔 super.paint(g); g.setColor(Color.BLUE); g.setFont(new Font(Font.DIALOG, Font.BOLD, 50)); g.drawString(“521”, 440, 440); g.drawString(“老詹”, y2, 390); this.setBackground(Color.black); g.fillOval(x1, y1, 100, 100); g.setColor(Color.black); g.fillOval(x1, y1, 80, 80); g.setColor(Color.cyan); g.drawRect(x3,400,x4,80); g.fillOval(400+50, 400+50, 40, 40); g.fillOval(550+50, 400+50, 40, 40); //画字符 for(int i=0;i<x12.length;i++){ g.setColor(Color.orange); g.setFont(new Font(Font.DIALOG, Font.BOLD, 60)); g.drawString("$", x12[i], y12[i]); }
} public void run(){ while(true){ //月亮移动 x1++; for(int i=0;i<x12.length;i++){ y12[i]++; if(y12[i]>=800){ y12[i]=0; } }
if(x1>=800){ x1=0; }
repaint();//重绘,重复调用paint方法 try { Thread.sleep(15); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
}