Scanner s=new Scanner(System.in);
System.out.println("使用next方式接受");
//判斷使用者有沒有輸入字串
if(s.hasNext())
{
String s1=s.next();
System.out.println("輸出內容爲:" +s1);
}
s.close();
//使用next方式接受
//hello word!
//輸出內容爲:hello
Scanner s=new Scanner(System.in);
System.out.println("使用nextLine方式接受");
//判斷使用者是否還有輸入字串
if(s.hasNextLine())
{
String s1=s.nextLine();
System.out.println("輸出內容爲:" +s1);
}
s.close();//關閉Scanner,凡是屬於IO流的類如果不關閉會一直佔用資源
/**
使用next方式接受
hello word!
輸出內容爲:hello word!
*/
Scanner s=new Scanner(System.in);
//和
double sum=0;
//計算輸入了多少個數字
int m=0;
//通過回圈判斷是否還有輸入,並在裏面對每一次進行求和和統計
while (s.hasNextDouble()){
double x=s.nextDouble();
m = m+1;
sum = sum + x;
System.out.println("你輸入了第"+m+"個數,當前和爲"+sum);
}
System.out.println(m+"個數和爲:"+sum);
System.out.println(m+"個數平均值爲:"+(sum/m));
s.close();
/*
10
你輸入了第1個數,當前和爲10.0
20
你輸入了第2個數,當前和爲30.0
30
你輸入了第3個數,當前和爲60.0
15
你輸入了第4個數,當前和爲75.0
45
你輸入了第5個數,當前和爲120.0
50
你輸入了第6個數,當前和爲170.0
x
6個數和爲:170.0
6個數平均值爲:28.333333333333332
*/
從上到下依次進行
Scanner s=new Scanner(System.in);
System.out.println("請輸入一個內容:");
String s1=s.nextLine();
if (s1.equals("1231")){
System.out.println(s1);
}
System.out.println("end");
s.close();
/*
12 1231
end 1231
end
Scanner s=new Scanner(System.in);
System.out.println("請輸入成績:");
int s1=s.nextInt();
if (s1 >= 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
s.close();
/*
50 60
不及格 及格
Scanner s=new Scanner(System.in);
System.out.println("請輸入成績:");
double s1=s.nextDouble();
if (s1 == 100){
System.out.println("恭喜滿分");
}else if (s1 < 100 && s1 >=90){
System.out.println("A");
}else if (s1 < 90 && s1 >=70){
System.out.println("B");
}else if (s1 < 70 && s1 >=60){
System.out.println("C");
}else if (s1 < 60 && s1 >=0){
System.out.println("不及格");
}else {
System.out.println("成績不合法" +
"");
}
s.close();
//其中有一個爲true,則跳過執行
if(布爾值){
if(布爾值){
if(布爾值){
..
..
}
}
}
char grade = 'A';
switch (grade){
case 'A':
System.out.println("優");
break;//一定要加上,否則case穿透,會一直往下執行
case 'B':
System.out.println("良");
break;
case 'C':
System.out.println("中");
break;
case 'D':
System.out.println("差");
break;
default:
System.out.println("未知等級");
}
回圈結構:
while(布爾表達式){
回圈內容
}
//輸出1-100
int i = 0;
while (i < 100){
i++;
System.out.println(i);
}
//計算1+2+3+4+..+100
int i = 0;
int sum = 0;
while (i < 100){
i++;
sum = sum + i;
}System.out.println(sum);
}
回圈結構:
do{
回圈語句
}while(布爾表達式)
while 與 do…while回圈的區別
//計算1+2+3+4+..+100
int i = 0;
int sum = 0;
do{
i++;
sum = sum + i;
}while (i < 100);
System.out.println(sum);
for回圈語句是支援迭代的一種通用結構,是最有效,最靈活的回圈結構
回圈結構:
for(初始化;布爾表達式;更新){
程式碼語句
}
//輸出1-100
int a = 0;
while (a<=100){
System.out.println(a);
a+=2;
}
System.out.println("while 回圈結束");
for (int i=0;i<=100;i++){
System.out.println(i);
}
System.out.println("for回圈結束");
/**
* 關於for回圈有一下幾點說明
* 1.最先執行初始化步驟,可以宣告一種型別,但可初始化一個或多個回圈控制變量,也可以是空語句
* 2.檢測布爾表達式的值,如果爲true,回圈體被執行。如果爲false,回圈體被終止,開始執行回圈體後面的語句
* 3.執行一次回圈體後更新回圈控制變量(迭代因子控制回圈變數的增減)
* 4.再次檢測布爾表達式,回圈執行上面的過程
*/
//死回圈
for ( ; ; ) {
}
//計算1+2+3+..+100的和
int sum = 0;
for (int i=0;i<=100;i++){
sum = sum + i;
} System.out.println(sum);
//計算0-100的奇數和偶數的和
int addSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if((i % 2) != 0){
addSum+=i;
}else {
evenSum+=i;
}
}
System.out.println("奇數的和"+addSum);
System.out.println("偶數的和"+evenSum);
}
//用while或for回圈輸出1-1000之間能被5整除的數,並且每行輸出3個
int i = 0;
while (i < 1000) {
if ((i % 5) == 0) {
System.out.print(i+"\t");
}
if ((i % (5 * 3)) == 0){
System.out.println( );
}
i++;
}
for (int i1 = 0; i1 < 1000; i1++) {
if((i1 % 5) == 0){
System.out.print(i1+"\t");
}
if ((i1 % (5 * 3)) == 0){
System.out.println( );
}
}/////println 輸出完會換行
//////print 輸出完不會換行
/**
* 1. 我們先列印第一列
* 2. 把固定的1再用回圈包起來
* 3. 去掉重複項,i <= j
* 4. 調整樣式
*/
//列印九九乘法表
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j + "*" + i + "=" + (j * i)+"\t");
}
System.out.println( );
}
主要是用來遍歷陣列和集合
回圈結構:
for(宣告語句:表達式)
{
程式碼語句
}
//遍歷陣列的元素
int [ ] num= {10,20,30,40,50};//定義了一個數組
for (int x : num){
System.out.print(x+"\t");
}
for(int i = 0; i<5;i++){
System.out.println(num[i]);
}
強制退出回圈(回圈體之後的語句正常執行)
int i = 0;
while (i<100){
i++;
System.out.println(i);
if(i == 10){
break;
}
}
// 12345678910
用於終止某次回圈過程(跳過某次回圈)
int i = 0;
while (i<50){
i++;
if((i %10)==0){
System.out.println(" ");
continue;
}System.out.print(i);
}
/**
* 123456789
* 111213141516171819
* 212223242526272829
* 313233343536373839
* 414243444546474849
*/
//列印三角形
for (int i = 1; i <= 5; i++) {
for (int j = 5;j >= i;j--){
System.out.print(" ");
}
for (int j = 1;j <= i; j++){
System.out.print("*");
}
for (int j = 1;j<i;j++){
System.out.print("*");
}
System.out.println(" ");
}