鍵盤輸入與輸出
輸入
在 Java 中可以使用 java.util.Scanner 類實(shí)現(xiàn)從鍵盤獲取數(shù)據(jù)
Scanner
類是 Java
預(yù)先定義好的,放在 java.util
包中盆佣,我們直接使用即可太抓,使用方法:
- 在類定義前面先使用
import
導(dǎo)入該類 - 創(chuàng)建
Scanner
對(duì)象显押,例如:- Scanner input = new Scanner(System.in);
- 調(diào)用
Scanner
對(duì)象的方法從鍵盤上讀取數(shù)據(jù)- input.next()
獲取鍵盤輸入的字符串
- input.nextInt()
獲取鍵盤輸入的整數(shù)
- input.nextDouble()
獲取鍵盤輸入的小數(shù)
- input.nextBoolean()
獲取鍵盤輸入的布爾值
- 不能從鍵盤獲取
char
類型的字符
- input.next()
/**
使用 java.util.Scanner 從鍵盤讀取數(shù)據(jù)
*/
// 第一步導(dǎo)入 Scanner 類
import java.util.Scanner;
class Example01{
public static void main(Sring[] args){
// 第二步創(chuàng)建 Scanner 的對(duì)象
// 使用 new 創(chuàng)建類的對(duì)象
// Scanner input 表示 input 這個(gè)變量是 Scanner 類型的
// System.in 表示系統(tǒng)的標(biāo)準(zhǔn)輸入柳洋,System.out 表示系統(tǒng)的標(biāo)準(zhǔn)輸出
Scanner input = new Scanner(System.in);
// 一般情況下,會(huì)給用戶一個(gè)請(qǐng)輸入的提示
System.out.println("請(qǐng)輸入一個(gè)整數(shù)");
// 第三步獲取用戶輸入
// 獲取用戶輸入的整數(shù)孙援,并保存到 num 變量中
int num = input.nextInt();
}
}
輸出
- 打印并換行
System.out.println()
- 打印不換行
System.out.print()
class Example02{
public static void main(String[] args){
String name = "answer";
int age = 18;
boolean isMan = True;
// 打印字面量
System.out.println("Hello World");
System.out.println(123);
// 打印變量的值
System.out.println(name);
System.out.println(age);
System.out.println(isMan);
// 打印表達(dá)式的值
System.out.println(1 + 1);
System.out.println(age + 1);
// 字符串拼接
System.out.println("name" + name);
System.out.println("性別男:" + isMan);
System.out.println("年齡" + age)
// 輸出結(jié)果:年齡181
System.out.println("年齡" + age + 1)
// 輸出結(jié)果:19年齡
System.out.println(age + 1 + "年齡")
}
}
流程控制
計(jì)算機(jī)的程序執(zhí)行順序有三種結(jié)構(gòu):
- 順序結(jié)構(gòu):從上向下逐條執(zhí)行語(yǔ)句
- 選擇結(jié)構(gòu):根據(jù)條件是否成立害淤,選擇執(zhí)行不同的分支
- 循環(huán)結(jié)構(gòu):重復(fù)執(zhí)行某段代碼
選擇結(jié)構(gòu)
選擇結(jié)構(gòu)的語(yǔ)句有 if
和 switch
語(yǔ)句
if 語(yǔ)句
image
/**
if 語(yǔ)句
*/
import java.util.Scanner;
class Example01{
// 判斷輸入的數(shù)字是不是正整數(shù)
public static void main(String[] args){
System.out.println("請(qǐng)輸出一個(gè)整數(shù)");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if(num > 0){
System.out.println(num + "是正整數(shù)");
}
}
}
if ...else 語(yǔ)句
image
/**
if...else 語(yǔ)句
*/
import java.util.Scanner;
class Example02{
// 判斷輸入的整數(shù)是奇數(shù)還是偶數(shù)
public static void main(String[] args){
System.out.println("請(qǐng)輸入一個(gè)整數(shù)");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
// 如果 num 可以被 2 整除,是偶數(shù)拓售,否則窥摄,是奇數(shù)
if(num % 2 == 0){
System.out.println(num + "是偶數(shù)");
}else{
System.out.println(num + "是奇數(shù)");
}
}
}
多分支 if 語(yǔ)句
image
/**
多分支 if 語(yǔ)句
*/
import java.util.Scanner;
class Example03{
// 判斷輸入的是正整數(shù),負(fù)數(shù)础淤,還是 0
public static void main(String[] args){
System.out.println("請(qǐng)輸入一個(gè)整數(shù)");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
if(num > 0){
System.out.println(num + "是正數(shù)");
}else if(num < 0){
System.out.println(num + "是負(fù)數(shù)");
}else{
System.out.println(num + "是零");
}
}
}
switch 語(yǔ)句
switch
也是一種多分支語(yǔ)句崭放,語(yǔ)法:
switch(條件表達(dá)式){
case 常量1:
代碼塊1;
break;
case 常量2:
代碼塊2;
break;
default:
代碼塊3;
}
- 條件表達(dá)式可以是
int
,short
,byte
,String
或者枚舉類型,double
,long
等類型不能做為switch
的條件表達(dá)式 - 常量值不能重復(fù)
- 如果所有的常量值都不匹配條件表達(dá)式鸽凶,則執(zhí)行
default
代碼塊 -
break
用于跳出switch
語(yǔ)句 -
case
沒有先后順序
/**
switch語(yǔ)句
*/
import java.util.Scanner;
class Example04{
public static void main(Sting[] args){
// 根據(jù)輸入的數(shù)字判斷是星期幾
Scanner.out.println("請(qǐng)輸入一個(gè)1~7范圍內(nèi)的正整數(shù)");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
switch(num){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
defaulf:
System.out.println("你輸入的數(shù)字不符合要求");
break;
}
}
}
循環(huán)結(jié)構(gòu)
循環(huán)就是重復(fù)的執(zhí)行某一段代碼币砂,主要確定兩點(diǎn):
- 重復(fù)執(zhí)行的代碼
- 重復(fù)執(zhí)行的條件
循環(huán)語(yǔ)句有 while
循環(huán),do...while
循環(huán)玻侥,for
循環(huán)
while 語(yǔ)句
image
class Example01{
public static void main(Sting[] args){
// 打印10行"我是 while"
int rows = 0;
while(rows < 10){
System.out.println("我是 while");
rows++;
}
}
}
/*
計(jì)算 1+2+3...100 的和
*/
class Example02{
public static void main(String[] args){
int sum = 0;
int x = 1;
while(x <= 100){
sum += x;
x++;
}
System.out.println("sum = " + sum);
}
}
/*
統(tǒng)計(jì)1~100范圍內(nèi)决摧,有多少可以被7整除的數(shù)
*/
class Example03{
public static void main(String[] args){
int num = 1;
int count = 0;
while(num <= 100){
if(num % 7 == 0){
count++;
}
num++;
}
System.out.print("1~100范圍內(nèi)可以被7整除的數(shù)有" + count + "個(gè)");
}
}
/*
猜數(shù)
1 生成一個(gè)1~100范圍內(nèi)的隨機(jī)整數(shù)
2 提示用戶輸入一個(gè)猜測(cè)的數(shù)字
如果輸入的數(shù)字比隨機(jī)數(shù)字大,提示大了
如果輸入的數(shù)字比隨機(jī)數(shù)字小凑兰,提示小了
如果相等掌桩,提示猜中了
3 統(tǒng)計(jì)用戶猜測(cè)的次數(shù)
*/
import java.util.Scanner;
class Example04{
public static void main(String[] args){
// 1. 生成一個(gè)1~100范圍內(nèi)的隨機(jī)整數(shù)
// Math.random() 方法返回一個(gè)0~1范圍內(nèi)的隨機(jī)小數(shù)
int dest = (int)(Math.random * 100);
Scanner input = new Scanner(System.in);
// 統(tǒng)計(jì)用戶猜測(cè)的次數(shù)
int count = 0;
System.out.println("請(qǐng)輸入你猜測(cè)的數(shù)字");
int num = input nextInt();
// 每次輸入,計(jì)數(shù)器就加1
count++;
while(num != dest){
if(num > dest){
System.out.println("大了票摇,請(qǐng)重新輸入");
}else if(num < dest){
System.out.println("小了拘鞋,請(qǐng)重新輸入");
}
// 重新輸入的數(shù)字賦值給 num
num = input.nextInt();
// 每次重新輸入,計(jì)數(shù)器就加1
count++;
}
// 循環(huán)結(jié)束矢门,說明 dest == num 即猜中了
System.out.println("恭喜你盆色,猜中了灰蛙,共猜了" + count + "次");
}
}
do...while 語(yǔ)句
image
class Example05{
public static void main(String[] args){
// 1. 打印10行字符串
int count = 0;
do{
System.out.println("Hello world");
count++;
}while(count < 10);
// 2. 累加 1+2+3...100
int num = 1;
int sum = 0;
do{
sum += num;
num++;
}while(num <= 100);
System.out.println("sum = " + sum);
}
}
/*
與 while 的區(qū)別:
如果循環(huán)條件不成立
do...while 至少執(zhí)行一次循環(huán)
while 一次循環(huán)都不會(huì)執(zhí)行
*/
class Example05{
public static void main(String[] args){
int num = 10;
// 循環(huán)條件不成立, while 一次也不執(zhí)行
while(num < 0){
System.out.println(num);
num++;
}
// 循環(huán)條件不成立隔躲,do...while 執(zhí)行一次
do{
System.out.println(num);
num++;
}while(num < 0);
}
}
for 語(yǔ)句
image
for(初始化表達(dá)式摩梧;循環(huán)條件表達(dá)式;循環(huán)體后表達(dá)式){
循環(huán)體
}
執(zhí)行過程:
- 執(zhí)行初始化表達(dá)式
- 判斷循環(huán)條件
- 如果條件成立宣旱,執(zhí)行循環(huán)體仅父,執(zhí)行循環(huán)體后表達(dá)式
- 重復(fù)2,3步浑吟,當(dāng)循環(huán)條件不成立時(shí)結(jié)束循環(huán)
一般情況下笙纤,當(dāng)循環(huán)次數(shù)確定時(shí)使用 for 循環(huán),循環(huán)次數(shù)不確定時(shí)使用 while 循環(huán)
public class Example06 {
public static void main(String[] args){
// 1.打印10次hello world
for(int i = 1; i <= 10; i++){
System.out.println("Hello World" + i);
}
// 2.計(jì)算 1+2+3...100 的和
int sum = 0;
for(int i = 1; i <= 100; i++){
sum += i;
}
System.out.println("sum=" + sum);
// 3.輸出1~100之間能被7整除的數(shù)
for(int i = 1; i <= 100; i++){
if(i % 7 == 0){
System.out.print(i + " ");
}
}
}
}
/**
循環(huán)嵌套
根據(jù)輸入的行數(shù)和個(gè)數(shù)打印星號(hào)
*/
import java.util.Scanner;
public class Example07 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入行數(shù)");
int rows = input.nextInt();
System.out.println("請(qǐng)輸入個(gè)數(shù)");
int num = input.nextInt();
// 外層循環(huán)控制行數(shù)
for(int x = 1; x <= rows; x++){
// 內(nèi)層循環(huán)控制個(gè)數(shù)
for(int i = 1; i <= num; i++){
System.out.print("*");
}
System.out.println();
}
input.close();
}
}
/**
打印以下圖形
*
**
***
****
*****
******
*/
import java.util.Scanner;
public class Example08 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("請(qǐng)輸入行數(shù)");
int num = input.nextInt();
for(int i = 1; i <= num; i++){
for(int x = 1; x <= i; x++){
System.out.print("*");
}
System.out.println();
}
input.close();
}
}
結(jié)束循環(huán)
continue
結(jié)束本次循環(huán)组力,繼續(xù)下次循環(huán)break
跳出循環(huán)結(jié)構(gòu)
/*
continue 結(jié)束本次循環(huán)省容,繼續(xù)下一次循環(huán)
*/
public class Example09{
public static void main(String[] args){
// 打印1~100范圍內(nèi)可以被7整除的數(shù)
num = 0;
while(num <= 100){
num ++;
if(num % 7 != 0){
// 如果不能被7整除就結(jié)束本次循環(huán),再去判斷循環(huán)條件
continue;
}
System.out.print(num + " ")
}
}
}
/*
break 結(jié)束循環(huán)
*/
import java.util.Scanner;
class Example10{
public static void main(String[] args){
int dest = (int)(Math.random() * 100);
Scanner input = new Scanner(System.in);
int count = 0;
int num = -1;
// 循環(huán)條件一致成立
while(true){
System.out.println("請(qǐng)輸入你猜測(cè)的數(shù)字");
num = input.nextInt();
count++
if(num > dest){
System.out.println("大了");
}else{
System.out.println("小了");
}else{
System.out.println("恭喜你燎字,猜中了腥椒,共猜了" + count + "次");
// 結(jié)束循環(huán)
break;
}
}
}
}
/*
for 循環(huán)中的 break , continue
*/
public class Example11{
public static void main(String[] args){
// 1.計(jì)算兩個(gè)整數(shù)的最大公約數(shù)
int m = 20;
int n = 30;
for(int i = m > n ? n : m; i >= 1; i--){
if(m % i == 0 && n % i == 0){
System.out.println(m + "與" + n + "的最大公約數(shù)是" + i);
// 跳出整個(gè)循環(huán)
break;
}
}
// 2. 打印1~100之間能被7整除的數(shù)
for(int i = 1; i <= 100; i++){
if(i % 7 != 0){
// 結(jié)束本次循環(huán)體,去執(zhí)行表達(dá)式3
continue;
}
System.out.print(i + " ");
}
}
}