2021.10.12
阿新 • • 發佈:2021-10-12
**第一
package com.yingcan.Reward;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入密碼");
if (scanner.hasNextLine()){
String str =scanner.nextLine();
System.out.println("輸出的內容為:"+str);
}
scanner.close();
}
}
**第二
package com.yingcan.Reward;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i =0;
float f =0.0f;
System.out.println("請輸入整數:");
if (scanner.hasNextInt()){
i =scanner.nextInt();
System.out.println("輸出的資料為整數"+i);
}else{
System.out.println("輸出的資料不是整數");
}
System.out.println("請輸入小數:");
if (scanner.hasNextFloat()){
f =scanner.nextFloat();
System.out.println("輸出的資料為小數"+f);
}else{
System.out.println("輸出的資料不為小數" );
}
scanner.close();
}
}
**第三
package com.yingcan.Reward;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum =0;
int m =0;
System.out.println("請輸入資料");
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m +1 ;
sum =sum +x;
System.out.println("你輸入了第"+m+"個數據,這些資料的總和為sum="+sum);
System.out.println("這些資料的平均值為"+(sum/m));
}
System.out.println(m+"這些資料的總和為"+sum);
System.out.println(m+"這些資料的平均值為"+(sum/m));
scanner.close();
}
}