Java基本程序设计结构(2)

Java基本程序设计结构(2)

输入输出

读取输入

Java读取输入需要先构建一个Scanner对象,并与标准输入流System.in关联

Scanner in=new Scanner(System.in);

然后可以使用Scanner的各种方法进行数据读取
例如:

System.out.print("What is your name? ");
String name = in.nextLine();//读入一行

常用API
- Scanner (InputStream in)
用给定的输人流创建一个 Scanner 对象。
- String nextLine( )
读取输入的下一行内容。
- String next( )
读取输入的下一个单词(以空格作为分隔符)。
- int nextlnt( )
- double nextDouble( )
读取并转换下一个表示整数或浮点数的字符序列。
- boolean hasNext( )
检测输人中是否还有其他单词。
- boolean hasNextInt( )
- boolean hasNextDouble( )
检测是否还有表示整数或浮点数的下一个字符序列。

输出

可以使用System.out.println()方法输出数据,也可以在输出时对数据进行格式化

文件读写

需要使用File对象构造一个Scanner对象,如下:

Scanner in=new Scanner(Paths.get("test.txt"),"gbk");

在这里指定了编码方式为gbk编码

注意:
- 如果文件路径中包含反斜杠,则需要多加一个反斜杠,表示转义,例如:C:\\doc\\test.txt
- 文件读写必需要抛出IOException错误,这是必检错误

文件的写入需要构造一个PrintWriter对象

PrintWriter out = new PrintlulriterC'myfile.txt", "UTF-8") ;

接下来就可以像System.out一样使用print等方法写入数据了

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(Paths.get("E:/projects/IntelliJ/Maintest/src/test.txt"), "gbk");
        PrintWriter out=new PrintWriter("E:/projects/IntelliJ/Maintest/src/out.txt","UTF-8");
        do{
            String s=in.nextLine();
            System.out.println(s);
            out.println(s);
        }while (in.hasNext());
        out.close();
    }
}

注意:
写入结束以后必需使用`close()·方法关闭流

控制流程

条件语句if

if语句的格式为: if (condition) statement
在默认情况下,if后的第一句语句与if相关,若if后要有多行语句处理,需要使用块作用,即加上大括号

循环语句while

while语句有两种形式

第一种为

while(condition){
    statement
}

先判断条件,若为true,则进入循环

第二种为

do{
    statement
}while(condition);

先执行一次,再判断条件,是否进入下次循环

循环语句for

与C语言相同,格式如下

for(int i=0;i<10;i++){
    System.out.println("hello");
}

多重选择语句switch

与C语言相同,switch语句将从与选项值相匹配的 case 标签处开始执行直到遇到 break 语句,或者执行到switch语句的结束处为止。如果没有相匹配的 case 标签, 而有 default 子句, 就执行这个子句

Scanner in = new Scanner(System.in);
System.out.printC'Select an option (1, 2, 3, 4) M);
int choice = in.nextlnt();
switch (choice)
{
case 1:
    break;
case 2:
    break;
case 3:
    break;
case 4:
    break;
default:
    break;
}

注意:
case的标签可以是:
- char,byte,short,或者int等
- 枚举常量
- 字符串变量

中断控制语句

break语句

Java使用break语句用于跳出循环和switch语句,例如:

while (years <= 100)
{
balance += payment;
double interest = balance * interestRate / 100;
balance 扛 interest;
if (balance >= goal ) break;
years++;
}

上面的break语句的使用与C语言一致,但是不同的是,Java还提供带标签的break语句,用于跳出多重循环。
我们都知道在C语言中,跳出多重循环,我们只能笨拙的使用一个flag,然后在外层循环中多次判断!

例如:

Scanner in = new Scanner(System.in);
int n;
read_data:
while (. . .) // this loop statement is tagged with the label
for (. . .) // this inner loop is not labeled
{
Systen.out.print("Enter a number >= 0: ");
n = in.nextlntO;
if (n < 0) // should never happen-can’t go on
break read
.
data;
// break out of readjata loop
}/
/ this statement is executed immediately after the labeled break
if (n < 0) // check for bad situation
{
// deal with bad situation
}
else
{
// carry out normal processing
}

标签使用label:形式

continue

continue语句和break语句类似,唯一的区别在于,break用于跳出循环和switch,而continue用于跳出本次循环,同样也提供带标签和不带标签两种使用形式。

猜你喜欢

转载自blog.csdn.net/weixin_38312031/article/details/80783875