选择语句和循环语句小练
if(scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小数数据"+ f);
}else{
System.out.println("输入的不是小数数据!");
}//顺序结构
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
System.out.println("继续1" );
m++;
sum = sum + x;
}//循环结构
选择结构,成绩输出
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
if(score == 100){
System.out.println("满分");
}else if(score <100 && score>=90){
System.out.println("A");
}else if(score <90 && score>=80){
System.out.println("B");
}else if(score <80 && score>=60){
System.out.println("C");
}else if(score <60 && score>=0){
System.out.println("不及格");
}else {
System.out.println("输入的成绩不合法");
}
scanner.close();
}
}
求几位数的总和与平均数
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
double sum = 0;
//计算输入了多少个数字
int m = 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和统计
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
System.out.println("继续1" );
m++;
sum = sum + x;
}
System.out.println(m + "个数的和为:"+sum);
System.out.println(m + "个数的平均值为:"+(sum/m));
scanner.close();
}
}
打印九九乘法表
for(初始化:布尔表达式:更新){
//代码语句
}
//举例:
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+i*j+"\t\t");
}
System.out.println();
}
}
}
输出1-1000能被5整除的数,每行输出三个
public class ForDemo03 {
public static void main(String[] args) {
//输出1-1000能被5整除的数,每行输出三个
//小瑕疵:0换行了
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");
}
if (i%15 == 0) {
System.out.println();
}
}
int num=0;
for (int i = 0; i <= 1000; i++) {
if(i%5 == 0){
System.out.print(i+"\t");
num++;
}
if(num%3 == 0 && num!=0) {
System.out.println();
num = 0;
}
}
}
}
输出一个三角形
public class TestDemo01 {
static int NUM = 6;//要打印的行数
public static void main(String[] args) {
//打印三角形 X行
for(int i = 0;i<NUM;i++){
for(int j = NUM;j>i+1;j--){
System.out.print(" ");//打印三角形左半的空白
}
for (int s = 0;s<=i;s++) {
System.out.print("*");//打印三角形左边的星号
}
for(int p = 0;p<i;p++){
System.out.print("*");//打印三角形右边的星号
}
System.out.println();
}
}
}
标签:语句,scanner,sum,小练,System,循环,println,public,out
From: https://www.cnblogs.com/0720hzq/p/17328016.html