Java基础进阶-if跟while语句综合使用,根据输入的数字,输入对应的星期。

/*
if,while语法综合使用,根据用户输入的数据(1<day<7)判断其对应的星期
如果输入的值不在对应的范围,提醒用户输入有误,要求用户重新输入
*/

import java.util.Scanner;
public class notepadtext2{
	public static void main(String[] args){
		//创建对象
		Scanner sr = new Scanner(System.in);
		
		//创建能多次循环输入值
		int n = 0;
		
		//输入接收对象
		System.out.println("请输入你需要查询的值:");
		int day = sr.nextInt();
		
		//判断输入的值是否是在对应的取件,如果不是要求用户重新输入
		
		//能重复输入的开关口
		while(n<=10){
			//判断是否在正确范围
			if(day>=1&&day<=7){
			//得到对应的汉字
			char str = domatch(day);
			//交给打印语句打印
			myPrint(str);
			//重复输入,并赋值;
			day = sr.nextInt();
			//n的值自增1
			n++;
		}else{
			System.out.println("输入的数值不在其范围,请重新输入...");
			//重复输入并赋值
			day = sr.nextInt();
			//n的值自增1
			n++;
		}
	
		}
		System.out.println("查询次数已经用完,请稍后再用,再见!");
		
	}
	
	//接收int输入值,判断传出对应的星期;
	public static char domatch(int intport){
		//接收传来的值
		int a = intport;
		//定义字符,用来接收判断的值
		char b;
		if(a==1){
			b='一';
		}else if(a==2){
			b='二';
		}else if(a==3){
			b='三';
		}else if(a==4){
			b='四';
		}else if(a==5){
			b='五';
		}else if(a==6){
			b='六';
		}else{
			b='日';
		}
		return b;
	}
	
	//输出打印方法
	public  static void myPrint(char a){
		System.out.println("Today is 星期"+a+";");
	}
	
}
发布了18 篇原创文章 · 获赞 16 · 访问量 399

猜你喜欢

转载自blog.csdn.net/lierenbiji21/article/details/105319201