Java语言程序设计 【基础篇】 【学习笔记】

 1.在使用System.in

需要使用a.close(); 否则输入流没有关闭,是不够严谨的。

package chapter02;
import java.util.Scanner;
public class Show1 {
	
	public static void main(String[] args) {
		Scanner a = new Scanner(System.in);
		double ce = a.nextDouble();
		System.out.println((9.0/5)*ce+32);
		a.close();//System.in的输入流没有关闭,这是不够严谨的
	}
}

2.Java中也有类似C语言的printf可以指定输出数据的格式化控制%.4f但是没有%lf

 

package chapter02;
import java.util.Scanner;
public class Show2 {
	public static void main(String[] args) {
		Scanner input = new Scanner (System.in);
		double hall = input.nextDouble();
		double r = input.nextDouble();
		final double PI=3.1415926;
		double area=r*r*PI;
		//System.out.printf("%.1f",r*r*PI);
		System.out.println();
		System.out.printf("area is %.4f\n",area);							
		System.out.printf("Volume is %.1f", r*r*PI*hall);
		input.close();
		System.out.println(); 
	}
}

3.javax.swing.JOptionPane 中的showMessageDialogshowInputDialog

 

package chapter02;
import javax.swing.JOptionPane;
public class Show10 {
	public static void main(String[] args) {
		JOptionPane.showMessageDialog(null,"我爱你,XZY!!","大笨蛋",JOptionPane.ERROR_MESSAGE);
		JOptionPane.showInputDialog(null,"我爱你,XZY!!","大笨蛋",JOptionPane.ERROR_MESSAGE);
		JOptionPane.showInputDialog("我爱你,XZY!!");
		JOptionPane.showMessageDialog(null,"我爱你,XZY!!");
	}
}

4.保留5位小数并且四舍五入的方法

 

package chapter02;
import java.util.Scanner;
public class Show12 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int balance = in.nextInt();
		double a = in.nextDouble();
		System.out.println((int)(balance*(a/1200)*100000+0.5)/100000.0);//+0.5四舍五入
		in.close();
	}
}

5.产生随机数Math.random();会产生0.0<=x<1.0的浮点数

Math.random()*100就会产生100一下的随机数

6.Inter.parseInt(String ); 把字符串转换为整数型

 

把“15”这个字符串转换为Int

7.System.exit(0);程序结束

 

package chapter03;
import javax.swing.JOptionPane;
public class Show4 {

	public static void main(String[] args) {
		int a,b;
		a = (int)(Math.random()*100);
		b = (int)(Math.random()*100);
		String answer = JOptionPane.showInputDialog(null,a +"+"+ b+"的值是?","question");
		int answerr = Integer.parseInt(answer);
		if(a+b==answerr)
			System.out.println("true");
		else
			System.out.println("false");
		System.exit(0);
	}
}
//这个程序只有在回答正确后 a b的值才会改变 如果 回答错误 值不会改变


8.  通常a+(Math.random()*b  返回一个aa+b(但不包括a+b)的数

 9.可变长参数列表 double...表示穿进去的是可变长参数

package chapter06;

public class VarArgsDemo {

	public static void main(String[] args) {
		printMax(34,3,3,5,64.6);
		printMax(new double[]{1,2,3,23423,566});
		
	}
	public static void printMax(double... numbers ){//这里
		if(numbers.length==0){
			System.out.println("NO");return ;
		}
		double result = numbers[0];
		for(int i=1;i<numbers.length;i++){
			if(numbers[i]>result){
				result = numbers[i];
			}	
		}
		System.out.println("max = "+result);
	}
}

10.Rondom  种子方法的使用

package chapter08;
import java.util.Random;
public class TestRandom {

	public static void main(String[] args) {
		Random random1 = new Random(3);//种子3
		Random random2 = new Random(3);
		for(int i=0;i<10;i++){
			System.out.print(random1.nextInt(1000)+" ");
		}
		System.out.println();
		for(int i=0;i<10;i++){
			System.out.print(random2.nextInt(1000)+" ");
		}
	}
}
/*
 * Random random1 = new Random(3);//种子3
 * 如果new对象random1 和 random2 都用种子3  则这两个对象的随机值是一样的
 * 这种方法有利于对某些数据进行测试
 * */
 


11.一个java文件里面只能用一个public class

12.

Public 无限定

无    默认只能在package

Private 只能在一个class里使用

13.静态变量/静态方法调用的注意事项

 

 

package chapter08_复习题;
public class Show10 {
	public static void main(String[] args) {
		F f = new F();
		System.out.println(f.i);
		//System.out.println(f.s);//改成F.s; 访问静态变量 应该用 类.变量名 而不是 对象.变量名
		f.im();
		//f.sm();//改成 F.sm(); 访问静态变量 应该用 类.变量名 而不是 对象.变量名
		//System.out.println(F.i);//改成 f.i   不能对非静态字段F.i进行静态引用
		System.out.println(F.s);
		//F.im();//改成 f.im();即可        不能对非静态方法im()进行静态引用
		F.sm();
	}
}
class  F{
	int i=3;
	static String s="asdasd";
	void im(){
	}
	static void sm(){
	}
}

14.数据域封装的优点是防止数据被篡改




  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值