您的位置:首页 > 编程语言 > Java开发

Java学习(三)流程控制

2013-07-13 20:22 204 查看
程序一:编写一个剪刀石头布的游戏,系统生成一个随机数(1-3)(1剪刀 2石头 3布)你输入一个数,看看最后谁能赢。

代码:

import java.util.Scanner;
public class Won {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x =(int) (Math.random()*3)+1;
System.out.println("请输入你要输入的数:(1剪刀 2石头 3布)");
Scanner scan = new Scanner(System.in);
int y=scan.nextInt();
whoWon(x,y);
}
public static void whoWon(int x ,int y){
switch(x){
case 1:
switch(y){
case 1:System.out.println("平手");break;
case 2:System.out.println("你赢了");break;
case 3:System.out.println("你输了");break;
default :System.out.println("你的输入有误");
}
break;
case 2:
switch(y){
case 1:System.out.println("你输了");break;
case 2:System.out.println("平手");break;
case 3:System.out.println("你赢了");break;
default :System.out.println("你的输入有误");
}
break;
case 3:
switch(y){
case 1:System.out.println("你赢了");break;
case 2:System.out.println("你输了");break;
case 3:System.out.println("平手");break;
default :System.out.println("你的输入有误");
}
break;
default :System.out.println("未知结果");
}
}
}
运行结果:



程序二:改写第一题,要求必须任一方连续赢2次程序才终止。

代码:

import java.util.Scanner;

public class AgainWon {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean flag[]=new boolean[10];
for(int i=1;i<flag.length;i++){
int x =(int) (Math.random()*3)+1;
System.out.println("请输入你要输入的数:(1剪刀 2石头 3布)");
Scanner scan = new Scanner(System.in);
int y=scan.nextInt();
System.out.println("电脑出:"+x);
if(x==y){
continue;
}else{
flag[i]= isWon(x,y);
}
if(i>1&&flag[i-1]==flag[i]){
if(flag[i]){
System.out.println("你已经连赢两局了(平手不影响结果)");
}else{
System.out.println("你已经连输两局了(平手不影响结果)");
}
break;
}
}
}
public static boolean isWon(int x ,int y){
boolean flag = false;
switch(x){
case 1:
switch(y){
case 2:flag=true;break;
case 3:flag=false;break;
default :System.out.println("你的输入有误");
}
break;
case 2:
switch(y){
case 1:flag=false;break;
case 3:flag=true;break;
default :System.out.println("你的输入有误");
}
break;
case 3:
switch(y){
case 1:flag=true;break;
case 2:flag=false;break;
default :System.out.println("你的输入有误");
}
break;
default :System.out.println("未知结果");
}
return flag;
}
}
运行结果:



程序三:编写程序,提示将用户输入的一个十进制的数转换成二进制(不能使用Integer的方法)。

代码:

import java.util.Scanner;
public class switchJz {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入你要转换的十进制数:");
Scanner scan = new Scanner(System.in);
int x=scan.nextInt();
change(x);
}
public static void change(int x){
int array[]=new int[16];
array[0]=x%2;
int i=1;
while((x/2)!=0){
if(x%2==1){
array[i]=1;
}else{
array[i]=0;
}
if(x/2==1){
array[i]=1;
}
++i;
x=x/2;
}
System.out.println("转换的二进制数为:");
for(int k=array.length-1;k>=0;k--){
System.out.print(array[k]);
}
}
}
运行结果:







程序四:编写程序,提示将用户输入的一个十进制的数转换成十六进制(不能使用Integer的方法)。

代码:

import java.util.Scanner;
public class Switch16 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入你要转换的十进制数:");
Scanner scan = new Scanner(System.in);
int x=scan.nextInt();
change(x);
}
public static void change(int x){
int array[]=new int[8];
array[0]=x%16;
int i=1;
while((x/16)>16){
array[i]=1;
x=x/16;
i++;
}
array[i]=x/16;
System.out.println("转换为16进制数为:");
System.out.print("0X");
for(int k=array.length-1;k>=0;k--){
switch(array[k]){
case 10:System.out.print("A");break;
case 11:System.out.print("B");break;
case 12:System.out.print("C");break;
case 13:System.out.print("D");break;
case 14:System.out.print("E");break;
case 15:System.out.print("F");break;
default:System.out.print(array[k]);
}
}
}
}
运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: