根据输入的值,判断是星期几
如果等于1,则输出星期一
如果等于2,则输出星期二
如果等于3,则输出星期三
如果等于4,则输出星期四
如果等于5,则输出星期五
如果等于6,则输出星期六
如果等于7,则输出星期天
import java.util.Scanner;
public class HomeWork{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
System.out.println("请输入查询时间");
int time=sc.nextInt();
if(time==1){
System.out.println("今天是星期一");
}else if(time==2){
System.out.println("今天是星期二");
}else if(time==3){
System.out.println("今天是星期三");
}else if(time==4){
System.out.println("今天是星期四");
}else if(time==5){
System.out.println("今天是星期五");
}else if(time==6){
System.out.println("今天是星期六");
}else if(time==7){
System.out.println("今天是星期日");
}
}
}
2
编写代码实现如下内容:if语句实现
考试成绩分等级。
90~100 A等。
80-89 B等。
70-79 C等。
60-69 D等。
60以下 E等。
请根据给定成绩,输出对应的等级。
import java.util.Scanner;
public class HomeWork{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入成绩");
int test=sc.nextInt();
if(test>=90){
System.out.println("A等");
}else if(test>=80){
System.out.println("B等");
}else if(test>=70){
System.out.println("C等");
}else if(test>=60){
System.out.println("D等");
}else{
System.out.println("E等");
}
}
}
3
看程序写结果:请自己独立分析,先不要编译运行。
第一题
int x = 1,y = 1;
if(x++==2 & ++y==2) //
{
x =7;
}
System.out.println("x="+x+",y="+y); //
答:
x=2,y=2
---------------------------------------------------
第二题
int x = 1,y = 1;
if(x++==2 && ++y==2) //x = 2; y = 1 &&短路与,当两个条件中的第一个条件是false时,则整个结果就是false,第二个条件不再执行
{
x =7;
}
System.out.println("x="+x+",y="+y);
答:
x=2,y=1
---------------------------------------------------
第三题
int x = 1,y = 1;
if(x++==1 | ++y==1) //true | false x = 7 y = 2;
{
x =7;
}
System.out.println("x="+x+",y="+y); //x= 7,y = 2
x=7,y=2;
---------------------------------------------------
第四题
int x = 1,y = 1;
if(x++==1 || ++y==1) //true
{
x =7;
}
System.out.println("x="+x+",y="+y);
答:x=7,y=1
---------------------------------------------------
第五题
boolean b = true;
if(b=false) //true == false; //b
System.out.println("a");
else if(b)
System.out.println("b");
else if(!b)
System.out.println("c");
else
System.out.println("d");
答:c
6 求1--100之间的偶数和
public class HomeWork{
public static void main(String[] args){
int sum=0;
for(int i=1;i<=100;i++){
if(i%2==0){
sum=sum+i;
}
}
System.out.println(sum);
}
}
原文:https://www.cnblogs.com/fbbg/p/10498019.html