文档注释
package com.yang.base;
/**
* @author yang
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
*
* @param name
* @return name
* @throws Exception
*/
public String getName(String name) throws Exception {
return name;
}
}
- 在当前路径下,通过命令行 javadoc -encoding utf-8 -charset UTF-8 Doc.java生成了Doc类的说明文档
- 打开index.html
Scanner输入
使用next方式接收
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
//创建一个扫描器对象,接受键盘数据
Scanner scanner=new Scanner(System.in);
System.out.println("使用next方式接收");
//判断用户有没有输入字符串
if(scanner.hasNext()){
//使用next方式接收
String str=scanner.next();
System.out.println("输出的内容为:"+str);
}
scanner.close();//使用IO流的类如果不关闭会一直占用资源
}
}
结果:
如果输入Hello World,用next方式接收只会输出Hello。因为next方式会以空格作为中断信号。
使用nextLine方式接收
public class Demo02 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("使用nextLine方式接收");
if (scanner.hasNextLine()){
String str=scanner.nextLine();
System.out.println("输出的内容为:"+str);
}
}
}
分析:
以回车为中断接收的信号,回车前的所有内容都会被接收。
实例
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//输入n个小数,求这些小数的总和和平均数
double sum=0;
double n=0;
System.out.println("请输入小数");
//当还在输入小数时就一直保持循环
while (scanner.hasNextDouble()){
double i=scanner.nextDouble();
sum=sum+i;
n++;
}
System.out.println("这些数的总和为"+sum);
System.out.println("这些数的平均数为"+sum/n);
scanner.close();
}
}
分析:
以输入作为控制,还在输入小数时就一直执行while循环。
选择结构
if选择结构
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//选择结构
Scanner scanner=new Scanner(System.in);
System.out.println("打个招呼吧");
String s=scanner.nextLine();
//if单选择结构
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("请输入成绩");
int score=scanner.nextInt();
//if双选择结构
if (score>=60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
//if多选择结构
/*
从上至下如果执行成功任意一个语句,则选择结构终止,继续程序
*/
System.out.println("请再次输入成绩(多选择)");
int j=scanner.nextInt();
if (j>100){
System.out.println("成绩不合法");
}else if (j>=90){
System.out.println("A级");
}else if (j>=80){
System.out.println("B级");
}else if (j>=70){
System.out.println("C级");
}else if(j>=60){
System.out.println("D级");
}else{
System.out.println("不合格");
}
System.out.println("end");
scanner.close();
}
}
switch选择结构
import java.util.Scanner;
public class SwitchDemo {
public static void main(String[] args) {
//switch case:JDK7新特性可以支持字符串匹配
//如果不在case后加上break,则会出现case穿透现象
System.out.println("请输入你爱人的名字");
Scanner scanner=new Scanner(System.in);
String lover=scanner.nextLine();
scanner.close();
switch(lover){
case"谢":
System.out.println("命中注定那个她");
break;
default:
System.out.println("回头是岸");
}
}
}
循环结构
while循环
public class WhileDemo {
public static void main(String[] args) {
//计算1到100的总和
int i=0;
int sum=0;
while (i<=100){
sum+=i;
i++;
}
System.out.println(sum);
}
}
do while循环
public class DoWhileDemo {
public static void main(String[] args) {
//do while和while不同的是总会先执行一次do语句
//计算1到100的总和
int i=0;
int sum=0;
do {
sum+=i;
i++;
}while(i<=100);
System.out.println(sum);
}
}
for循环
public class ForDemo {
public static void main(String[] args) {
//利用for循环打印九九乘法表
int i;
int j;
for(i=1;i<10;i++){
for (j=i;j<10;j++){
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
}
增强for循环
主要用来遍历数组
public class ForDemo {
public static void main(String[] args) {
//增强for循环,主要用来遍历数组
int[] number={1,2,3,4,5};
for(int x:number){//每次将number中的的值赋值给x
System.out.println(x);
}
}
}