Java 课堂随笔(2)

循环

           for( 声明; 循环条件 ; 循环变量的变化 ) { }有时这三个都不必填写

           while(1)--死循环 在循环里

                          可用break 来跳出当前循环 ;continue 结束本次循环,继续下一次; return 结束当前方法

           do..while()

九九乘法表

package Day2;

public class hello_word {

	public static void main(String[] args) {
		for(int i=1;i<=9;i++)
		{
		   for(int j=1;j<=i;j++)
		   {
			   /*
		   System.out.print(i);
		   System.out.print("*");
		   System.out.print(j);
		   System.out.print("=");
		   System.out.print(i*j);
		   System.out.print("\t");
		   */
		   System.out.print(j+"*"+i+"="+(i*j)+"\t");
		}
		   System.out.println();
		}
	}
}
老师讲了一下程序猿暖手宝,百度了一个代码,测试了一下,确实cpu的使用率100%

#include <process.h>
#include <windows.h>

double pi = 3.14;

void handwarmer(void *p)
{
	while (1)
	{
		//非线程同步。
		pi *= pi;
		if (pi > 1024*1024*1024)
			pi = 3.14;
	}
}

int main()
{
	SYSTEM_INFO siSysInfo;
	GetSystemInfo(&siSysInfo);

	//n核 2n线程一样hold住
	for (int i=0; i< (int)siSysInfo.dwNumberOfProcessors*2; ++i)
		_beginthread(handwarmer, 0, 0);
	system("pause");
	return 0;
}
程序 = 算法 + 数据结构

下面讲 数组

     数组,一组数据类型相同的数据集合

        int[ ] arr = new int [10] ; //数组的声明

         数组的初始化方式

              在声明的时候初始化     int [ ]  arr1 = new int [10];    用这种方式,自动初始化为0 //arr1[0,0,0,0,0,0,0,0,0,0];

                                                       int[ ] arr2 = {1,2,3,4,5,6}; 

                                                       int[ ] arr3;

                                                       arr3 = new int [ ]{1,2,3,4}; 先声明 , 后赋值

              数组不可变长的

             数组在初始化后,数组可存数的数据

              个数就固定了,不再可变。     ①  数组中数据的查询      通过数组名【下标】的形式查询数据   注:下标从0开始

                                                                   ②  数组中数据的修改      直接给数组名【下标】赋值。

                                                                   ③  数组的长度的获取       通过 数组名.lenth 获取   System.out.println(arr.lenth);

                                                                   ④  数组的遍历 for()循环       for(int  i=0;i<arr3;i++ )  System.out.println( arr3[i] );     

                                                                   ⑤  数组的赋值   api  application process interface

                                                                                               System.arracopy(a,b,c,d,e)

                                                                                               a : 源数组

                                                                                               b : 原数组的起始位置

                                                                                               c: 目标数组

                                                                                               d: 目标数组的起始位置

                                                                                               e: 复制长度

                                                                                               int[ ] src = {1,2,3,4,5,6,7,8};

                                                                                               int[ ] aim = new int[10];

                                                                     ⑥数组的输出   Arrays.Tostring(arrayName);

                                                                      ⑦ 数组下标越界异常

                                                                          Array Index Out Of Buounds Exception 

                                                                          ansychronized

                                                                      ⑧ 数组的扩容  

                                                                          Arrays.copyOf( arrName , lenth );

                                                                      ⑨数组的排序

                                                                         int[ ] unsorteArr = { 5,3,1,2,4};

                                                                         System.out.print( )

Copy
int[] src = {1,2,3,4,5,6,7,8};

        int[] aim = new int[10];
        System.out.println(Arrays.toString(src));
        System.o
ut.println(Arrays.toString(aim));
		System.arraycopy(src,3,aim,4,4);
        System.out.println(Arrays.toString(src));
        System.out.println(Arrays.toString(aim));
sort
int[ ] a = { 5,3,1,2,4};
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));

java里的“方法” 与C语言里的被调函数 相似,但是在JAVA里不能叫做函数

 方法是用来封装一段有特殊功能的代码块,在JAVA里方法是可以被反复调用的

 提高了代码的重用率,代码效率大大提升,便于维护

    1 方法的修饰(辞)

    2 方法的返回值类型

    3方法名——需满足驼峰命名法 方法名后写一对小括号(参数列表)   注:红色是一个方法必须有的

       throws 异常列表

       {方法体}                                                                    

      修饰辞   publc static

      返回值类型   int  || char ||  void(仅仅是处理,没有返回值)

      ㈠方法名  ①驼峰 ②见名知意

      ㈡参数列表: 例如C语言里的sort(int a[ ] )    形式参数  方法声明时参数列表中所声明的参数,无具体值,只起占位作用

                                                                                        实际参数  方法调用时,传进方法的有具体值,具体意义的参数                     注: Java的方法中无法修改实参的值  亦可为空

      ㈢ 异常列表 (略)

      ㈣ 方法体 花括号引起来的东西  即C 里的函数内容

package Day02;
public class hello_word {
	public static void main(String[] args) {
		System.out.println(c());
	}
	public static int  c(){
		return (1+100)*50; //方法体
	}
}
/*
 *猜字母游戏
package ddddd;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class asdfasdf {

		public static void main(String[] args) {
			System.out.println("您一共有10次机会来猜这五个都不相同的大写英文字母");
			int[] ans= new int[5];
			int[] gauss= new int[5];
			int count = 10;
			Random rdn = new Random();
			int cnt = 0,j = 0;
			for(int i=0;;i++)
			{
			   int x = (int)rdn.nextInt(26);
			   for(j = 0;j<cnt;j++)
			   {
				   if(x==ans[j]) break;
			   }
			   if(j!=cnt){i--;continue;}
			   else {ans[cnt++] = x;System.out.print((char)(x+65)+" ");}
			   if(cnt==5) break;
			}
			
			while(true){
			System.out.println("现在您还有"+(count--)+"次机会");
			Scanner cin = new Scanner(System.in);
			char[] str = new char[5];
			String sstr = cin.nextLine();
		    for(int i=0;i<5;i++)
		    {
		    	gauss[i] = (int)(sstr.charAt(i)-65); 
		    	
		    }
		    int ans1 = 0,ans2 = 0;
		    for(int i=0;i<5;i++)
		    {
		    	if(ans[i]==gauss[i])
		    		ans1++;
		    }
		    for(int i=0;i<5;i++)
		    {
		    	for(j=0;j<5;j++)
		    	{
		    		if(ans[i]==gauss[j])
		    			ans2++;
		    	}
		    }
		    System.out.println("位置和字母都猜对了有"+ans1+"个数字");
		    System.out.println("位置不对,但字母对了有"+ans2+"个数字");
		    if(ans1==5) {System.out.println("恭喜您猜对了");break;}
			if(count==0) {
				System.out.println("很遗憾答案是");
				for(int i= 0 ;i<5;i++)
                                    System.out.print((char)('A'+ ans[i]));
				break;}
			}
		}
	}
 








 

    

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值