1.switch方法和for循环要学会结合使用。
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(“恭喜通关”);
}
}
1
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();
}
}
}