码迷,mamicode.com
首页 > 其他好文 > 详细

程序流程控制--函数方法调用

时间:2017-04-17 10:10:30      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:用户输入   rand   oid   位置   游戏程序   continue   引用类型   接收   提示   

在一个Java类中,方法的名称相同,但是方法的参数不同,这样的函数方法称为方法的重载
* 重载的区别在于参数的不同,三种不同形式:
* 参数的顺序不同;参数的类型不同;参数的个数不同

简单类型的变量只是传递变量的值,而不是地址
* 这个是最基础的值传递

* 对于复杂(引用)类型的变量传递的也是值,但是这个值的内容是一个地址
* 因此虽然说在Java中它仍然叫做值传递,但是因为值是地址,
* 所以在接收的方法中也有可能会改变原来引用类型变量的数据值

猜数字游戏程序:

public class MyClass11 {
public static void main(String[] args) {
// 生成不重复的4位数字(只有1--9)然后用户竞猜,总共最多只能猜7次
//每次都是输入4位数字,按照标准,分为数字存在(记录为B);数字存在且位置正确(记录为A)
// 例如: 生成数字1234竞猜数字: 1367(提示竞猜结论: 1A1B)
boolean running = true;
while(running){
Scanner sc = new Scanner(System.in);//sc.获得用户的输入
System.out.println("\t****** 猜数字 ******\n");
System.out.println("\t1 开始游戏 \n\t2 游戏介绍 \n\t3 退出游戏\n ");
System.out.print("\t 请输入您的选择:");
int num = sc.nextInt();//将输入转换成int类型。

if(num==1){
play(sc);
}else if(num==2){
System.out.print("\t生成一个不重复的4位数字(只有1--9)然后竞猜,总共最多只能猜7次。"
+ "\n\t每次都是输入4位数字,按照标准,分为数字存在(记录为B);数字存在且位置正确(记录为A)。"
+ "\n\t例如: 生成数字1234竞猜数字: 1367(提示竞猜结论: 1A1B)。");
System.out.println("\n\t3 确定 \n\t4取消");
int num1 = sc.nextInt();
if(num1==3||num1==4){
continue;
}
}else if(num==3){
System.exit(0);
}
}
}
public static void play(Scanner sc){
int count=0;
int b[] = new int[4];
while(count<4){
if(count==0){
b[count] = (int)(Math.random()*9)+1;
}else{
int temp = (int)(Math.random()*9+1);
b[count] = temp ;
for(int i=0; i<count; i++){
if(temp==b[i]){
count--;
break;
}
}
}
count++;
}
int a[]={b[0],b[1],b[2],b[3]};//将随机生成的4个数分别放在数组中a[]
for(int i=0;i<b.length;i++){
System.out.print(a[i]+" ");
};
int change=7;
boolean running = true;
while(running){

if(change>0){
guess(change,a ,sc);
}
}
}
public static void guess(int change, int a[], Scanner sc){
System.out.println("\n\t 系统随机数已经产生,请开始竞猜...\n");
while(change>0){
System.out.println("\n\t 请输入您竞猜的数字(每次输入的数字不能相等):");
int num = sc.nextInt();
if(num<1000){
System.out.print("请输入四位数字");
return;
}
int ai=num/1000,bi=(num%1000)/100,
ci=((num%1000)%100)/10,di=((num%1000)%100)%10;
if(ai==bi||ai==ci||ai==di||bi==ci||bi==di||ci==di){
System.out.print("请重新输入(数字不能重复,也不能为0)");
return;
}
int u[]={ai,bi,ci,di};//将用户输入的数字分解为4个数字,放在数组u[]中
int count1=0,count2=0;//count1为A的数量 count2为B的数量
if(a[0]==u[0]){
count1++;
}else if(a[0]==u[1]||a[0]==u[2]||a[0]==u[3]){
count2++;
}
if(a[1]==u[1]){
count1++;
}else if(a[1]==u[0]||a[1]==u[2]||a[1]==u[3]){
count2++;
}
if(a[2]==u[2]){
count1++;
}else if(a[2]==u[0]||a[2]==u[1]||a[2]==u[3]){
count2++;
}
if(a[3]==u[3]){
count1++;
}else if(a[3]==u[0]||a[3]==u[1]||a[3]==u[2]){
count2++;
}

if(change==7&&count1==4){
System.out.print("我的神啊!");
System.exit(0);
}
if(change==6&&count1==4){
System.out.print("您是天才啊!");
System.exit(0);
}
if(change==5&&count1==4){
System.out.print("您也是个人才!");
System.exit(0);
}
if(change==4&&count1==4){
System.out.print("不错不错啊!");
System.exit(0);
}
if(change==3&&count1==4){
System.out.print("哟,厉害了哟!");
System.exit(0);
}
if(change==2&&count1==4){
System.out.print("呵呵,很好!");
System.exit(0);
}
if(change==1&&count1==4){
System.out.print("终于对了,好难啊!");
System.exit(0);
}
else{
System.out.print("再试一次(提示):"+count1+"A"+count2+"B");
change--;
}
//int num1= sc.nextInt();
//if(num==num1){
// System.out.println("不能输入重复的数字");
//}
}
}
}

程序流程控制--函数方法调用

标签:用户输入   rand   oid   位置   游戏程序   continue   引用类型   接收   提示   

原文地址:http://www.cnblogs.com/YGZ-321/p/6721000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!